项目作者: cmeeren

项目描述 :
Call batched functions as normal functions
高级语言: F#
项目地址: git://github.com/cmeeren/BatchIt.git
创建时间: 2019-09-17T12:16:54Z
项目社区:https://github.com/cmeeren/BatchIt

开源协议:MIT License

下载


BatchIt

BatchIt is a very simple F# library that allows you to write batched async functions and consume them as if they were
non-batched.

It’s useful if you have DB queries that are executed very frequently. This may for example be the case when using
GraphQL resolvers or similar (e.g. JSON-API with Felicity). BatchIt will also
transparently take care of deduplicating your input arguments, so that the batched function receives a distinct set of
inputs.

Installation

Install the BatchIt package from NuGet.

Quick start

BatchIt centers around these two function signatures:

  • Normal: 'a -> Async<'b>
    • Accepts a single argument 'a (which can be a tuple or a record if you need multiple values) and returns a single
      value 'b
  • Batched: 'a [] -> Async<('a * 'b) []>'
    • Accepts an array of 'a (all the values to use for a single batch) and returns an array of output values 'b
      together with their corresponding input values (so that BatchIt can correlate inputs with outputs).

Your job is to implement the batched function, and then you use BatchIt to configure timing and convert it to the normal
version. Any calls to the normal version will then be batched using your configuration and your batched function.

Example

  1. type Person = { Name: string }
  2. type FindArgs = { PersonId: Guid; TenantId: Guid }
  3. let getPersonBatched (args: FindArgs []) : Async<(FindArgs * Person option) []> =
  4. async {
  5. // BatchIt is not concerned with what actually happens inside this function.
  6. // Here you can e.g. query a DB with all the args. If using SQL, you might
  7. // use TVP or XML to get the args into the query and use INNER JOIN instead
  8. // of WHERE to filter your results.
  9. // Note that the query must return the input args for each row along with the
  10. // output values, so that you can create the FindArgs to return, too.
  11. // Example:
  12. let! rows = args |> ... // get rows for all input args
  13. return
  14. rows
  15. |> Seq.toArray
  16. |> Array.groupBy (fun r ->
  17. { PersonId = r.inputPersonId; TenantId = r.inputTenantId })
  18. |> Array.map (fun (batchKey, rows) ->
  19. batchKey,
  20. rows |> Array.tryHead |> Option.map (fun r -> { Name = r.name }))
  21. }
  22. // Now create the "normal" function using BatchIt:
  23. open BatchIt
  24. let getPerson : PersonArgs -> Async<Person option> =
  25. Batch.Create(getPersonBatched, 50, 100, 1000)
  26. // Now any calls to the seemingly normal getPerson function will be batched.
  27. // Each call will reset wait time to 50ms, and it will wait at least 100ms
  28. // and at most 1000ms.

Don’t add arguments to the non-batched function

The non-batched function needs to be defined without arguments (as a module-level function-typed value), as shown above.
Don’t do the following:

  1. // Don't do this!
  2. let getPerson args = Batch.Create(getPersonBatched, 50, 100, 1000) args

The above would result in a new batched version to be created for every invocation, and no batching would take place (
all batch sizes would be 1, plus there’d be a big overhead for each call).

If you want to make the non-batched function prettier (e.g. to name the arguments), simply wrap it in another function:

  1. let private getPerson' = Batch.Create(getPersonBatched, 50, 100, 1000)
  2. let getPerson args = getPerson' args

Configuration

BatchIt’s only public API is the Batch.Create overloads. You have the following options:

  • Throttle-like: Execute the batch X ms after the first call
    • Optionally also limit max batch size
  • Debounce-like: Execute the batch X ms after the last call (timer resets when new calls come in before X ms), but wait
    at least Y ms and at most Z ms
    • Optionally also limit max batch size
  • Specify the value the “normal” function should return if the batched output did not contain an item corresponding to
    the supplied input
    • For option, voption, list, and array, will automatically use None, ValueNone, List.empty,
      and Array.empty, respectively
    • For other types, you must supply the missing value yourself

Extra parameters to the batched function (e.g. connection strings)

BatchIt supports functions that accept one extra parameter in addition to the argument array. This can be used e.g. for
passing a connection string. (To pass multiple values, use a tuple or record.) The extra argument will be passed through
from the non-batched function returned by Batch.Create.

Example:

  1. let getPersonBatched
  2. (connStr: string)
  3. (args: FindArgs array)
  4. : Async<(FindArgs * Person option) array> =
  5. ... // like shown previously, but obviously uses the connection string somewhere
  6. open BatchIt
  7. let getPerson : string -> PersonArgs -> Async<Person option> =
  8. Batch.Create(getPersonBatched, 50, 100, 1000)

If the non-batched function is called with different values for the extra argument, all values are still collected and
run as part of the same batch (regarding timing, max size, etc.), but the batched arguments are grouped by the
corresponding extra argument value and the batched function is then invoked once for each unique extra value (with the
corresponding batched arguments).

Contributing

Contributions and ideas are welcome! Please
see Contributing.md for details.