refactor: improve queues#1453
Merged
LinkinStars merged 2 commits intoapache:devfrom Jan 14, 2026
Merged
Conversation
Contributor
ferhatelmas
commented
Dec 9, 2025
- fix race condition for registering handler
- add close method
- use generics to reduce duplication
- rename packages to drop underscore for go convention
- rename interface to drop stutter with package name
* fix race condition for registering handler * add close method * use generics to reduce duplication * rename packages to drop underscore for go convention * rename interface to drop stutter with package name Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
LinkinStars
requested changes
Dec 12, 2025
Comment on lines
+52
to
+68
| func (q *Queue[T]) Send(ctx context.Context, msg T) { | ||
| q.mu.RLock() | ||
| closed := q.closed | ||
| q.mu.RUnlock() | ||
|
|
||
| if closed { | ||
| log.Warnf("[%s] queue is closed, dropping message", q.name) | ||
| return | ||
| } | ||
|
|
||
| select { | ||
| case q.queue <- msg: | ||
| log.Debugf("[%s] enqueued message: %+v", q.name, msg) | ||
| case <-ctx.Done(): | ||
| log.Warnf("[%s] context cancelled while sending message", q.name) | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
This function may have a small issue. After determining the closed state, it immediately unlocks the channel and sends a message. If the channel is closed after unlocking, sending the message may result in a “send on closed channel” error.
Contributor
Author
There was a problem hiding this comment.
yes, good catch. Updated deferring to release the lock
Comment on lines
+27
to
+31
| type Service = *queue.Queue[*schema.ActivityMsg] | ||
|
|
||
| func NewService() Service { | ||
| return queue.New[*schema.ActivityMsg]("activity", 128) | ||
| } |
Member
There was a problem hiding this comment.
We believe defining an interface would be more user-friendly. Such as
type Service interface {
Send(ctx context.Context, msg *schema.ActivityMsg)
RegisterHandler(handler func(ctx context.Context, msg *schema.ActivityMsg) error)
Close()
}
func NewService() Service {
return queue.New[*schema.ActivityMsg]("activity", 128)
}
Contributor
Author
There was a problem hiding this comment.
Put interface parametric into queue package and aliased it into specific packages for code reuse. For example, following how it will be seen while using:
type Service queue.Service[*schema.NotificationMsg]
func (queue.Service[*schema.NotificationMsg]) Close()
func (queue.Service[*schema.NotificationMsg]) RegisterHandler(handler func(ctx context.Context, msg *schema.NotificationMsg) error)
func (queue.Service[*schema.NotificationMsg]) Send(ctx context.Context, msg *schema.NotificationMsg)Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
fa9305d to
75c8a3f
Compare
LinkinStars
approved these changes
Jan 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.