Skip to content

Conversation

proost
Copy link
Contributor

@proost proost commented Aug 31, 2025

tiny improvement about the issue

This PR doesn't solve above issue. To solve the issue, I think rewriting ring buffer is needed. But That's quite huge.

This PR improves two things.

  1. Do not use command if context is done.
  2. do not make waiting coroutine if context is done.

I think this tiny improvement is helpful for very rare situation, that buffer is full and lots of requests are waiting other requests.

ring.go Outdated
Comment on lines 58 to 70
n := &r.store[atomic.AddUint32(&r.write, 1)&r.mask]
n.c1.L.Lock()
for n.mark != 0 {
if ctxCh := ctx.Done(); ctxCh != nil {
select {
case <-ctxCh:
n.c1.L.Unlock()
n.c1.Signal()
return nil, ctx.Err()
default:
}
}
n.c1.Wait()
Copy link
Collaborator

@rueian rueian Sep 1, 2025

Choose a reason for hiding this comment

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

Hi @proost, thanks! I think we are close, but 2 things:

  1. We should find a way to make the condition variable context aware, n.c1.Wait(ctx), instead of a precheck. A precheck has already been done at the beginning of the pipe.Do().
  2. Since we did atomic.AddUint32(&r.write, 1), the r.write has been incremented, and we may leave the corresponding r.store slot empty if the ctx is errored. Do we handle the case correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should find a way to make the condition variable context aware, n.c1.Wait(ctx), instead of a precheck. A precheck has already been done at the beginning of the pipe.Do().

I knew that point. I already said this PR doesn't solve the issue completely. This PR mitigates overhead when buffer is full and some requests are already canceled or timeout. Do you want solve problem completely? If then i need some time. because bigger code change is needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know you know, but since we have already done a precheck at the beginning of pipe.Do(), a second precheck in the ring doesn't really help much. However, you did a great job on changing the ring interface. 👍

Copy link
Contributor Author

@proost proost Sep 1, 2025

Choose a reason for hiding this comment

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

I know you know, but since we have already done a precheck at the beginning of pipe.Do(), a second precheck in the ring doesn't really help much.

I'm not sure, but issue i mentioned, looks like happening when after pipe.Do(), so it might helpful the situation.

Regardless of second precheckiong, complete solution is needed I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrite the queue.

e6d4105

@proost proost marked this pull request as draft September 1, 2025 08:15
@proost proost requested a review from rueian September 7, 2025 12:51
@proost proost changed the title perf: put aware context before set command refactor: rewrite the queue to make context aware Sep 7, 2025
@proost proost marked this pull request as ready for review September 7, 2025 12:52
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