# Batches

> UCall for Mojo

```mojo
def score(batch: Batch) raises:
    for index in range(len(batch)):
        var request = batch.take(index)
        var length = String(len(request.body()))
        request^.reply(HTTPStatus.OK, length.as_bytes())

var scores = server.batch["/score", score](Verb.POST, Batching(2, 32, 10_000))
scores.take_batches()  # on its own thread, until server.stop()
```

Requests from every event loop share one queue per endpoint, drained by whichever thread polls it; requests the handler does not take answer 500 once it returns.

`from ucall import Batching, Batch, BodyDelivery`

## Batching

When a batch endpoint hands its queue to the handler; zero fields take 1, 32 and 10 milliseconds.

```mojo
struct Batching
```

- `min_size` `Int`: Dispatch as soon as this many requests wait.
- `max_size` `Int`: Never hand the handler more than this many requests at once.
- `max_latency_microseconds` `Int`: Dispatch a smaller batch once its oldest request waited this long.

Conforms to: `Copyable`, `Defaultable`, `Deinitable`, `ImplicitlyCopyable`, `Movable`, `RegisterPassable`, `TrivialRegisterPassable`

## __init__

Every default.

```mojo
def __init__() -> Self
```

## Batch

The requests of one due batch, from every event loop, lent to the handler in arrival order.

```mojo
struct Batch
```

Each request is taken at most once; whatever the handler leaves answers 500 once it returns.

Conforms to: `Deinitable`, `Movable`, `Sized`

## __init__

```mojo
def __init__(out self, state: Int, requests: Pointer[Int, ImmUntrackedOrigin], count: Int)
```

## __len__

Requests in the batch, taken or not.

```mojo
def __len__(self) -> Int
```

## take

Takes request `index` out of the batch, to reply to it here or on another thread. Binding-only.

```mojo
def take(self, index: Int) -> DetachedRequest
```

- `index`: The request's position in arrival order.

Raises: [`Error`](https://www.unum.cloud/docs/ucall/mojo/errors.md#Error) with [`INVALID_ARGUMENT`](https://www.unum.cloud/docs/ucall/mojo/errors.md#INVALID_ARGUMENT) past the last request, or [`ALREADY_EXISTS`](https://www.unum.cloud/docs/ucall/mojo/errors.md#ALREADY_EXISTS) once taken.

## BodyDelivery

When an endpoint's handler runs, `ucall_body_delivery_t` in C.

```mojo
struct BodyDelivery
```

- `BUFFERED` = `BodyDelivery(Int32(0))`: Once the whole body is buffered.
- `STREAMED` = `BodyDelivery(Int32(1))`: At headers time, the body pulled through [`DetachedRequest.body_chunk`](https://www.unum.cloud/docs/ucall/mojo/requests.md#body_chunk).
- `code` `c_int`: The raw `ucall_body_delivery_t` value the ABI exchanges.

Conforms to: `Copyable`, `Deinitable`, `Equatable`, `ImplicitlyCopyable`, `Movable`, `RegisterPassable`, `TrivialRegisterPassable`

