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

Optimize enqueueing tasks performance #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

yousifh
Copy link
Member

@yousifh yousifh commented Oct 9, 2024

A different take than #10

Use a sync.Map which is optimized for use case of write once, ready many times which is what we have here.

If the queue is in the map then skip calling sadd. Otherwise add the queue name to the set. This means on startup, the web clients will add the queue to the set once and then skip the call until next restart.

I don't think there is any issue with that. The set asynq:queues is left alone, we don't use it for anything, and nothing is deleting from it. SO it's fine to only try to add to it on startup.

The code is also simpler in this PR since we don't have to deal with time expiration.

@yousifh yousifh requested a review from pior October 9, 2024 06:58
@@ -112,8 +114,11 @@ func (r *RDB) Enqueue(ctx context.Context, msg *base.TaskMessage) error {
if err != nil {
return errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this for atomicity:

Suggested change
if _, ok := r.m.Load(msg.Queue); !ok {
if previous, _ := r.m.Swap(msg.Queue, true); previous == nil {

Since we have many nodes, we probably don't need to check the error.

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.

2 participants