-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: improve queues #1453
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
base: dev
Are you sure you want to change the base?
refactor: improve queues #1453
Conversation
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 <[email protected]>
| 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| type Service = *queue.Queue[*schema.ActivityMsg] | ||
|
|
||
| func NewService() Service { | ||
| return queue.New[*schema.ActivityMsg]("activity", 128) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
}