Compare commits
No commits in common. "main" and "v0.1.17" have entirely different histories.
4 changed files with 30 additions and 53 deletions
42
consumer.go
42
consumer.go
|
|
@ -28,15 +28,6 @@ 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)
|
||||||
|
|
@ -46,7 +37,18 @@ 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():
|
||||||
|
|
@ -56,33 +58,25 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
case <-reconnectSignal:
|
case amqErr := <-chClosedCh:
|
||||||
select {
|
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
||||||
case <-runCtx.Done():
|
reconnectTimer.Reset(time.Second)
|
||||||
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.Printf("Consume reconnect failed, retry: %s\n", err)
|
client.logger.Println("Error trying to consume, will try again. Retry in 5 seconds.")
|
||||||
reconnectTrigger()
|
reconnectTimer.Reset(time.Second * 5)
|
||||||
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")
|
||||||
reconnectTrigger()
|
reconnectTimer.Reset(time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
25
handler.go
25
handler.go
|
|
@ -13,7 +13,7 @@ import (
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
queueOptions *QueueOpts
|
queueName string
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
connection *amqp.Connection
|
connection *amqp.Connection
|
||||||
Channel *amqp.Channel
|
Channel *amqp.Channel
|
||||||
|
|
@ -36,16 +36,7 @@ type options struct {
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueueOpts struct {
|
func NewClient(address Address, queueName string, opts ...Option) (*Client, error) {
|
||||||
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()
|
||||||
|
|
@ -53,16 +44,16 @@ func NewClient(address Address, queueOpts QueueOpts, opts ...Option) (*Client, e
|
||||||
return nil, errors.Join(errBadAddr, err)
|
return nil, errors.Join(errBadAddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if queueOpts.QueueName == "" {
|
if queueName == "" {
|
||||||
l.Fatal(errNoQueue)
|
l.Fatal(errNoQueue)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := Client{
|
client := Client{
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
queueOptions: &queueOpts,
|
queueName: queueName,
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
connected: make(chan struct{}),
|
connected: make(chan struct{}),
|
||||||
logger: l,
|
logger: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
o := options{
|
o := options{
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package rabbit
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
15
service.go
15
service.go
|
|
@ -78,16 +78,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|
@ -129,7 +120,7 @@ func (c *Client) unsafePush(data []byte) error {
|
||||||
return c.Channel.PublishWithContext(
|
return c.Channel.PublishWithContext(
|
||||||
ctx,
|
ctx,
|
||||||
"",
|
"",
|
||||||
c.queueOptions.QueueName,
|
c.queueName,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
amqp.Publishing{
|
amqp.Publishing{
|
||||||
|
|
@ -156,7 +147,7 @@ func (c *Client) consume() (<-chan amqp.Delivery, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Channel.Consume(
|
return c.Channel.Consume(
|
||||||
c.queueOptions.QueueName,
|
c.queueName,
|
||||||
"",
|
"",
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue