BatchDispatcher is useful for batching values while doing I/O. For example, if you are launching multiple tasks which take a single id and each task fetches from database, you can use BatchDispatcher to batch those ids and do a single query requesting all those ids.

Synopsis

Declared in <folly/fibers/BatchDispatcher.h>

template<
    typename ValueT,
    typename ResultT,
    typename ExecutorT>
class BatchDispatcher;

Description

To use this, create a BatchDispatcher with a dispatch function which consumes a vector of values and returns a vector of results in the same order. Add values to BatchDispatcher using add function, which returns a future to the result set in your dispatch function.

Implementation Logic:

  • using FiberManager as executor example, user creates a thread_local BatchDispatcher, on which user calls add(value).

  • add(value) adds the value in a vector and also schedules a new task(BatchDispatchFunction) which will read the vector of values and call user's DispatchFunction() on it.

  • assuming the executor queues all the task and runs them in order of their creation time, then BatchDispatcher will run later than all the tasks already created. Depending on this, all the values were added in these tasks would be picked up by BatchDispatchFunction()

Example:

  • User schedules Task1, Task2, Task3 each of them calls BatchDispatch.add() with id1, id2, id3 respectively.

  • Executor's state {Task1, Task2, Task3}, BatchDispatchers state {}

  • After Task1 calls BatchDispatcher.add(): Executor's state {Task2, Task3, BatchDispatchFunction}, BatchDispatcher's state {id1}

  • After Task2 calls BatchDispatcher.add(): Executor's state {Task3, BatchDispatchFunction}, BatchDispatcher's state {id1, id2}

  • After Task3 calls BatchDispatcher.add(): Executor's state {BatchDispatchFunction}, BatchDispatcher's state {id1, id2, id3}

  • Now BatchDispatcher calls user's Dispatch function with {id1, id2, id3}

Note:

  • This only works with executors which runs the tasks in order of their schedule time.

  • BatchDispatcher is not thread safe.

Type Aliases

Name

Description

DispatchFunctionT

User‐provided function that maps a batch of values to a batch of results.

PromiseBatchT

Batch of promises fulfilled with the dispatch results.

ResultBatchT

Batch of results returned by the dispatch function.

ValueBatchT

Batch of input values collected before a dispatch.

Member Functions

Name

Description

BatchDispatcher [constructor]

Constructs a dispatcher that batches values and runs them on an executor.

add

Adds a value to the current batch and schedules dispatch if needed.

Created with MrDocs