ctx + write only chan

This commit is contained in:
nquidox 2026-02-21 15:54:50 +03:00
parent 42646a6a08
commit 40d1d3dd29

View file

@ -1,24 +1,34 @@
package rabbit
import (
"context"
"errors"
"log"
"time"
)
type Publisher interface {
Start(chanLen uint) <-chan []byte
Start(chanLen uint) chan<- []byte
}
type pubHandler struct {
client *Client
}
func (p *pubHandler) Start(chanLen uint) <-chan []byte {
func (p *pubHandler) Start(ctx context.Context, chanLen uint) chan<- []byte {
ch := make(chan []byte, chanLen)
go func() {
for {
select {
case <-ctx.Done():
for msg := range ch {
if err := p.push(msg); err != nil {
log.Printf("Error publishing message (shutdown): %s", err)
}
}
log.Println("Publisher stopped")
return
case msg := <-ch:
if err := p.push(msg); err != nil {
log.Printf("Error publishing message: %s", err)