Skip to content

refactor: improve queues#1453

Merged
LinkinStars merged 2 commits intoapache:devfrom
ferhatelmas:ferhat/queue
Jan 14, 2026
Merged

refactor: improve queues#1453
LinkinStars merged 2 commits intoapache:devfrom
ferhatelmas:ferhat/queue

Conversation

@ferhatelmas
Copy link
Copy Markdown
Contributor

  • 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>
@ferhatelmas ferhatelmas changed the title feat(ui): add min values for inputs and context-based keyboards for i… refactor: improve queues Dec 9, 2025
@LinkinStars LinkinStars self-requested a review December 9, 2025 11:55
@LinkinStars LinkinStars self-assigned this Dec 9, 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)
}
}
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)
}
Copy link
Copy Markdown
Member

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)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@LinkinStars LinkinStars merged commit 5ff6106 into apache:dev Jan 14, 2026
@ferhatelmas ferhatelmas deleted the ferhat/queue branch January 18, 2026 19:36
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