1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-02 11:43:40 +02:00

api/redis/subscriber.go: in worker() no need to spawn a goroutine when sending to the channel.

This commit is contained in:
Henrik Hautakoski 2024-02-07 17:29:54 +01:00
parent b854c1dfa7
commit d789b6a294

View file

@ -48,16 +48,6 @@ func NewSubscriber(ctx context.Context, client *redis.Client, ns Namespace, opti
return sub return sub
} }
// forward forwards a message to the channel.
// as writes to a unbuffered channel will block until it's read.
// We run select on it and discard the message if no read happends during timeout
func forward(msg redis.Message, ch chan<- []byte, timeout time.Duration) {
select {
case <-time.After(timeout):
case ch <- []byte(msg.Payload):
}
}
// worker reads messages from redis pubsub and forwards them to // worker reads messages from redis pubsub and forwards them to
// correct channels. // correct channels.
func (s *Subscriber) worker() { func (s *Subscriber) worker() {
@ -65,7 +55,10 @@ func (s *Subscriber) worker() {
// Route message to correct channel. // Route message to correct channel.
s.mu.RLock() s.mu.RLock()
if ch, ok := s.channels[msg.Channel]; ok { if ch, ok := s.channels[msg.Channel]; ok {
go forward(*msg, ch, s.timeout) select {
case <-time.After(s.timeout):
case ch <- []byte(msg.Payload):
}
} }
s.mu.RUnlock() s.mu.RUnlock()
} }