Go · Batches

Batches

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

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/ucall/go/batches.md' | claude -p "How would this fit my code?"

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.

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

BatchingBatching says when a batch endpoint dispatches; zero fields take 1, 32 and 10 milliseconds.
server.go:906 ↗
type Batching struct {
    MinSize uint
    MaxSize uint
    MaxLatency time.Duration
}

Fields

MinSizeuintMinSize is how many waiting requests dispatch a batch at once.
MaxSizeuintMaxSize is how many requests one batch holds at most.
MaxLatencytime.DurationMaxLatency is how long the oldest request of a smaller batch waits before it dispatches.
BodyDeliveryBodyDelivery says when a body is handed over: once buffered, or at headers time with the body pulled chunk by chunk.
server.go:549 ↗
type BodyDelivery int32

Values

BodyBufferedBodyBuffered hands the body over once it is whole.
BodyStreamedBodyStreamed hands the request or response over once the head arrived.
StructBatchBatch holds the requests one batch callback received, in arrival order, each taken once.2
server.go:917 ↗
type Batch struct {
    // contains filtered or unexported fields
}

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

LenLen is how many requests the batch holds.
server.go:923 ↗
func (batch Batch) Len() int
TakeTake hands over request index, which its taker replies to.
server.go:926 ↗
func (batch Batch) Take(index int) *Request