Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a windowed batch shcheduler #9

Merged
merged 7 commits into from
Nov 13, 2023
Merged

Conversation

yornaath
Copy link
Owner

@yornaath yornaath commented Nov 10, 2023

Fixes #6

Implement helper function

windowedFiniteBatchScheduler(config: {
  windowMs: number;
  maxBatchSize: number;
})

This example batcher will batch all fetch calls made within 10 ms OR if the number of fetch calls(batch size) exceeds 100 items.

const batcher = batshit.create({
  fetcher: async (ids: number[]) => {
    return fetchAllUsersByIds(ids);
  },
  scheduler: windowedFiniteBatchScheduler({
    windowMs: 10,
    maxBatchSize: 100,
  }),
  resolver: keyResolver("id"),
});

The behaviour of the scheduler could also be manually implemented since the BatcherScheduler function is now provided a third argument of batchSize: number.

const maxBatchSize = 100
const windowSize = 10

const batcher = batshit.create({
  fetcher: async (ids: number[]) => {
    return fetchAllUsersByIds(ids);
  },
  scheduler: (start, latest, batchSize) => {
    if (batchSize >= maxBatchSize) return "immediate";
    const spent = latest - start;
    return windowSize - spent;
  },
  resolver: keyResolver("id"),
});

Ref:

/**
 * A function to schedule batch execution timing
 */
export type BatcherScheduler = {
  /**
   * A scheduler function.
   *
   * @param start number - time stamp when the current batch started queuing fetches.
   * @param latest number - time stamp of the latest queued fetch.
   * @returns number - the number of ms to wait from latest queued fetch until executing batchh fetch call.
   */
  (start: number, latest: number, batchSize: number): Schedule;
};

/**
 * A schedule for when to execute a batched fetch call.
 */
export type Schedule = number | "immediate";

Copy link

vercel bot commented Nov 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
batshit-example ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 13, 2023 9:56am

@yornaath
Copy link
Owner Author

I have also added another helper maxBatchSizeScheduler that will not batch within a window of time but only wait for the batch size to be met:

const batcher = batshit.create({
  fetcher: async (ids: number[]) => {
    return fetchAllUsersByIds(ids);
  },
  scheduler: maxBatchSizeScheduler({
    maxBatchSize: 100,
  }),
  resolver: keyResolver("id"),
});

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should probably be added to the .gitignore file?

Copy link

@tbowmo tbowmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good from my point of view

@yornaath yornaath merged commit d634df3 into master Nov 13, 2023
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to limit the number of merge requests
2 participants