Skip to content

Conversation

@ferhatelmas
Copy link
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 <[email protected]>
@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
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.

Comment on lines +27 to +31
type Service = *queue.Queue[*schema.ActivityMsg]

func NewService() Service {
return queue.New[*schema.ActivityMsg]("activity", 128)
}
Copy link
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)
}

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