Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72f039e4f7 | ||
|
|
fd495892fa |
3 changed files with 53 additions and 29 deletions
42
consumer.go
42
consumer.go
|
|
@ -28,6 +28,15 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
||||||
|
|
||||||
limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize)
|
limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize)
|
||||||
|
|
||||||
|
reconnectSignal := make(chan struct{}, 1)
|
||||||
|
reconnectTrigger := func() {
|
||||||
|
select {
|
||||||
|
case reconnectSignal <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// initial consume
|
||||||
deliveries, err := client.consume()
|
deliveries, err := client.consume()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.logger.Printf("Could not start consuming: %s\n", err)
|
client.logger.Printf("Could not start consuming: %s\n", err)
|
||||||
|
|
@ -37,18 +46,7 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
||||||
chClosedCh := make(chan *amqp.Error, 1)
|
chClosedCh := make(chan *amqp.Error, 1)
|
||||||
client.Channel.NotifyClose(chClosedCh)
|
client.Channel.NotifyClose(chClosedCh)
|
||||||
|
|
||||||
reconnectTimer := time.NewTimer(0)
|
|
||||||
defer reconnectTimer.Stop()
|
|
||||||
<-reconnectTimer.C
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if !reconnectTimer.Stop() {
|
|
||||||
select {
|
|
||||||
case <-reconnectTimer.C:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
||||||
case <-runCtx.Done():
|
case <-runCtx.Done():
|
||||||
|
|
@ -58,25 +56,33 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
case amqErr := <-chClosedCh:
|
case <-reconnectSignal:
|
||||||
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
select {
|
||||||
reconnectTimer.Reset(time.Second)
|
case <-runCtx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(client.opts.reconnectDelay):
|
||||||
|
}
|
||||||
|
|
||||||
case <-reconnectTimer.C:
|
|
||||||
deliveries, err = client.consume()
|
deliveries, err = client.consume()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.logger.Println("Error trying to consume, will try again. Retry in 5 seconds.")
|
client.logger.Printf("Consume reconnect failed, retry: %s\n", err)
|
||||||
reconnectTimer.Reset(time.Second * 5)
|
reconnectTrigger()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// re-create closing chan
|
||||||
chClosedCh = make(chan *amqp.Error, 1)
|
chClosedCh = make(chan *amqp.Error, 1)
|
||||||
client.Channel.NotifyClose(chClosedCh)
|
client.Channel.NotifyClose(chClosedCh)
|
||||||
|
client.logger.Println("Consume reconnect success")
|
||||||
|
|
||||||
|
case amqErr := <-chClosedCh:
|
||||||
|
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
||||||
|
reconnectTrigger()
|
||||||
|
|
||||||
case delivery, ok := <-deliveries:
|
case delivery, ok := <-deliveries:
|
||||||
if !ok {
|
if !ok {
|
||||||
client.logger.Println("Deliveries channel closed unexpectedly")
|
client.logger.Println("Deliveries channel closed unexpectedly")
|
||||||
reconnectTimer.Reset(time.Second)
|
reconnectTrigger()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
17
handler.go
17
handler.go
|
|
@ -13,7 +13,7 @@ import (
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
queueName string
|
queueOptions *QueueOpts
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
connection *amqp.Connection
|
connection *amqp.Connection
|
||||||
Channel *amqp.Channel
|
Channel *amqp.Channel
|
||||||
|
|
@ -36,7 +36,16 @@ type options struct {
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(address Address, queueName string, opts ...Option) (*Client, error) {
|
type QueueOpts struct {
|
||||||
|
QueueName string
|
||||||
|
Durable bool
|
||||||
|
AutoDelete bool
|
||||||
|
Exclusive bool
|
||||||
|
NoWait bool
|
||||||
|
Args amqp.Table
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(address Address, queueOpts QueueOpts, opts ...Option) (*Client, error) {
|
||||||
l := log.New(os.Stdout, "", log.LstdFlags)
|
l := log.New(os.Stdout, "", log.LstdFlags)
|
||||||
|
|
||||||
addr, err := address.makeAddr()
|
addr, err := address.makeAddr()
|
||||||
|
|
@ -44,13 +53,13 @@ func NewClient(address Address, queueName string, opts ...Option) (*Client, erro
|
||||||
return nil, errors.Join(errBadAddr, err)
|
return nil, errors.Join(errBadAddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if queueName == "" {
|
if queueOpts.QueueName == "" {
|
||||||
l.Fatal(errNoQueue)
|
l.Fatal(errNoQueue)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := Client{
|
client := Client{
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
queueName: queueName,
|
queueOptions: &queueOpts,
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
connected: make(chan struct{}),
|
connected: make(chan struct{}),
|
||||||
logger: l,
|
logger: l,
|
||||||
|
|
|
||||||
15
service.go
15
service.go
|
|
@ -78,7 +78,16 @@ func (c *Client) init(conn *amqp.Connection) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil)
|
//_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil)
|
||||||
|
_, err = ch.QueueDeclare(
|
||||||
|
c.queueOptions.QueueName,
|
||||||
|
c.queueOptions.Durable,
|
||||||
|
c.queueOptions.AutoDelete,
|
||||||
|
c.queueOptions.Exclusive,
|
||||||
|
c.queueOptions.NoWait,
|
||||||
|
c.queueOptions.Args,
|
||||||
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -120,7 +129,7 @@ func (c *Client) unsafePush(data []byte) error {
|
||||||
return c.Channel.PublishWithContext(
|
return c.Channel.PublishWithContext(
|
||||||
ctx,
|
ctx,
|
||||||
"",
|
"",
|
||||||
c.queueName,
|
c.queueOptions.QueueName,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
amqp.Publishing{
|
amqp.Publishing{
|
||||||
|
|
@ -147,7 +156,7 @@ func (c *Client) consume() (<-chan amqp.Delivery, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Channel.Consume(
|
return c.Channel.Consume(
|
||||||
c.queueName,
|
c.queueOptions.QueueName,
|
||||||
"",
|
"",
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue