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

How to limit the number of merge requests #6

Closed
ben-nguyen-dev opened this issue Oct 6, 2023 · 7 comments · Fixed by #9
Closed

How to limit the number of merge requests #6

ben-nguyen-dev opened this issue Oct 6, 2023 · 7 comments · Fixed by #9

Comments

@ben-nguyen-dev
Copy link

I want to limit the number of merge requests.

Ex: I want to get details of 100 posts, and in scheduler =10ms, batshit will batch about 20-30 requests into one request. But I want to In a batch, it only batches up to 8 requests at a time, instead of combining all requests within 10ms.

Then instead of having 1 request with 20 postIds, I will have 3 requests with

  • Request 1: 8 postId,
  • Request 2: 8 postId,
  • Request 3: 4 postId
@yornaath
Copy link
Owner

yornaath commented Oct 9, 2023

Do you mean that if you set up one batcher and call .fetch on it a 100 times within 10ms that it makes roughly 12 requests with a batch size of 8 for each request?

Not sure I understand your requirements totally, but it might be out of the scope of the package. If you want to stagger the requesting a bit you might want to delay the fetching in your components as they render based on the index in your list.

@yornaath
Copy link
Owner

yornaath commented Oct 9, 2023

You could do something like this to stagger the batches, this approach should do one request pr 10 posts ish:

const postFetcher = batshit.create({ ... });

const PostList = (props: {postIds: number[]}) => {
  return (<>
  {
    props.postIds.map((postId, index) => 
      <Post key={postId} postId={postId} index={index}/>
    )
  }
</>)
}

const Post = (props: {postId: number, index: number}) => {
  const {data: post, isFetching} = useQuery(["post", props.postId], async () => {
    await delay(index)
    return postFetcher.fetch(props.postId)
  });

  return <div>
    {
      isFetching ? 
        <div>loading...</div> 
      : <h1>{post.title}</h1>
    }
  </div>
}

@yornaath
Copy link
Owner

@linhleedom Did you find a solution?

@tbowmo
Copy link

tbowmo commented Nov 2, 2023

I'm also up for a more elegant solution to limit number of "entityIds" that we submit in a single request. We have identified one example where we have 32.000 GUIDs in one api request (and the number is prone to grow further in the future).

Could we add an option to batshit, so that one could define a max length of the id array, at which it fires off the api request, and start a new (10ms) timer?

@yornaath
Copy link
Owner

yornaath commented Nov 10, 2023

Linked a possible solution in the pr mentioned above this comment.
Please let me know what you think @tbowmo @linhleedom

PR: #9

Note: changes are not yet published to npm.

@tbowmo
Copy link

tbowmo commented Nov 10, 2023

@yornaath looks good for me :)

(In the mean time I found out that it's react-query that can't handle 32.000 ids, it simply crashes the browser, so a batch limit wont help here)

@ben-nguyen-dev
Copy link
Author

@yornaath looks good to me. Can you publish the change to npm?

I had to copy your logic code into the project and overwrite it. This should not be done.
Thanks for the update

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 a pull request may close this issue.

3 participants