Refactor dataloaders into a general-purpose batching struct #1511
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What's new?
I occasionally find myself wanting a reusable batcher that can do what dataloaders do: open a batch with a max size and timeout, wait for requests to come in, submit all the queued up requests when the batch closes, etc. I've reused this pattern in our push notifications handling, and wanted to use it again for Kafka event handling.
This PR refactors the
Dataloader[TKey, TResult]
type to be a thin wrapper around a reusablebatch.Batcher[TParam, TResult]
class that can be reused in any other context where we want batching.The most notable change is that the
Load
functions have been renamed toDo
, since we don't really know if a generic batch is "loading" things or just executing commands and receiving a result status. TheDataloader
wrapper maintains the existingLoad
function names, since those feel appropriate and idiomatic for a dataloader.