# Batches

> A pooled batch endpoint gathers matching requests from every event loop into one queue and hands them to its handler in batches, which suits work that is cheaper per request in bulk, like GPU inference.

`import "github.com/unum-cloud/ucall"` 1.0.0-rc.1 · b7577426

A pooled batch endpoint gathers matching requests from every event loop into one queue and hands them to its handler in batches, which suits work that is cheaper per request in bulk, like GPU inference.

```go
network := ucall.NewInterface("127.0.0.1:0")
network.PipelinedRequests = 8
server, err := ucall.NewServer([]ucall.Interface{network}, nil)
if err != nil {
	return err
}
batching := ucall.Batching{MinSize: 16, MaxSize: 64, MaxLatency: 2 * time.Millisecond}
infer, err := server.PooledBatch("/infer", ucall.VerbPost, batching, func(batch ucall.Batch) {
	for index := range batch.Len() {
		request := batch.Take(index)
		body, err := request.Body()
		if err != nil {
			_ = request.ReplyError(ucall.HTTPStatusBadRequest, err.Error())
			continue
		}
		_ = request.Reply(ucall.HTTPStatusOK, nil, []byte(strconv.Itoa(len(body))))
	}
})
if err != nil {
	return err
}
go server.TakeCalls(0)
go infer.TakeBatches()
```

`Batching` dispatches as soon as `MinSize` requests wait, never more than `MaxSize` at once, and a smaller batch once its oldest request waited `MaxLatency`; zero fields mean 1, 32 and 10 milliseconds. The handler runs on the goroutine polling the endpoint through `TakeBatch` or `TakeBatches`, never on an event loop, so a slow batch never stalls the network. A `Batch` holds its requests in arrival order, and `Take` hands one over, to be replied to then or later from any goroutine; a request never taken stays unanswered until the server closes. The queue holds as many requests as the interfaces admit at once, connections times pipelined requests, and refuses more with 503. `PooledBatchEndpoint.Statistics` counts batches and requests, and whether each was dispatched on reaching `MaxSize`, on reaching `MinSize`, or once `MaxLatency` passed. `Server.LocalBatch` takes the same handler and `Batching`, but each event loop fires its own batches inside `TakeCall` or `TakeCalls`, so no goroutine drains them and the handler may reply there; its `LocalBatchEndpoint` offers only `Statistics`.

## API

### Types

#### `Batching`

Batching says when a batch endpoint dispatches; zero fields take 1, 32 and 10 milliseconds.

[server.go:906 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L906)

```go
type Batching struct {
    MinSize uint
    MaxSize uint
    MaxLatency time.Duration
}
```

#### Fields

| `MinSize` | `uint` | MinSize is how many waiting requests dispatch a batch at once. |
| - | - | - |
| `MaxSize` | `uint` | MaxSize is how many requests one batch holds at most. |
| `MaxLatency` | `time.Duration` | MaxLatency is how long the oldest request of a smaller batch waits before it dispatches. |

#### `BodyDelivery`

BodyDelivery says when a body is handed over: once buffered, or at headers time with the body pulled chunk by chunk.

[server.go:549 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L549)

```go
type BodyDelivery int32
```

#### Values

| `BodyBuffered` | | BodyBuffered hands the body over once it is whole. |
| - | - | - |
| `BodyStreamed` | | BodyStreamed hands the request or response over once the head arrived. |

#### Struct `Batch`

Batch holds the requests one batch callback received, in arrival order, each taken once.2

[server.go:917 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L917)

```go
type Batch struct {
    // contains filtered or unexported fields
}
```

A request never taken stays unanswered until the server is closed.

##### `Len`

Len is how many requests the batch holds.

[server.go:923 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L923)

```go
func (batch Batch) Len() int
```

##### `Take`

Take hands over request index, which its taker replies to.

[server.go:926 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L926)

```go
func (batch Batch) Take(index int) *Request
```
