Compare commits
20 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72f039e4f7 | ||
|
|
fd495892fa | ||
|
|
9b9d8a3064 | ||
|
|
154320d288 | ||
|
|
a2862350b3 | ||
|
|
44d99a4ba5 | ||
|
|
e58b052251 | ||
|
|
4810a8666e | ||
|
|
f23b388689 | ||
|
|
5e8661cfcd | ||
|
|
8f967361e8 | ||
|
|
ecda63761c | ||
|
|
d62d41b41a | ||
|
|
9d837385b1 | ||
|
|
46e07b7577 | ||
|
|
c8a6a7f8fa | ||
|
|
40d1d3dd29 | ||
|
|
42646a6a08 | ||
|
|
13ee3230e3 | ||
|
|
2846c1eda0 |
7 changed files with 232 additions and 50 deletions
34
address.go
Normal file
34
address.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package rabbit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Username string
|
||||
Password string
|
||||
Host string
|
||||
Port uint16
|
||||
Vhost string
|
||||
}
|
||||
|
||||
func (a *Address) makeAddr() (string, error) {
|
||||
if a.Host == "" {
|
||||
return "", errors.New("no host provided")
|
||||
}
|
||||
|
||||
if a.Vhost == "" {
|
||||
return "", errors.New("no vhost provided")
|
||||
}
|
||||
|
||||
//"amqp://username:password@host:port/vhost"
|
||||
return fmt.Sprintf("amqp://%v:%v@%v:%v/%v",
|
||||
url.QueryEscape(a.Username),
|
||||
url.QueryEscape(a.Password),
|
||||
a.Host,
|
||||
a.Port,
|
||||
url.PathEscape(a.Vhost),
|
||||
), nil
|
||||
}
|
||||
76
consumer.go
76
consumer.go
|
|
@ -4,22 +4,20 @@ import (
|
|||
"context"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"golang.org/x/time/rate"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Consumer interface {
|
||||
Start() chan []byte
|
||||
Start(ctx context.Context, chanLen uint) <-chan []byte
|
||||
}
|
||||
|
||||
type consumeHandler struct {
|
||||
ctx context.Context
|
||||
client *Client
|
||||
chanLen int
|
||||
}
|
||||
|
||||
func (c *consumeHandler) Start() chan []byte {
|
||||
msgCh := make(chan []byte, c.chanLen)
|
||||
go runConsumer(c.ctx, c.client, msgCh)
|
||||
func (c *consumeHandler) Start(ctx context.Context, chanLen uint) <-chan []byte {
|
||||
msgCh := make(chan []byte, chanLen)
|
||||
go runConsumer(ctx, c.client, msgCh)
|
||||
|
||||
return msgCh
|
||||
}
|
||||
|
|
@ -30,9 +28,18 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
|||
|
||||
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()
|
||||
if err != nil {
|
||||
log.Printf("Could not start consuming: %s\n", err)
|
||||
client.logger.Printf("Could not start consuming: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -41,33 +48,66 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
|||
|
||||
for {
|
||||
select {
|
||||
|
||||
case <-runCtx.Done():
|
||||
err = client.Close()
|
||||
if err != nil {
|
||||
log.Printf("Close failed: %s\n", err)
|
||||
client.logger.Printf("Close failed: %s\n", err)
|
||||
}
|
||||
return
|
||||
|
||||
case amqErr := <-chClosedCh:
|
||||
log.Printf("AMQP Channel closed due to: %s\n", amqErr)
|
||||
case <-reconnectSignal:
|
||||
select {
|
||||
case <-runCtx.Done():
|
||||
return
|
||||
case <-time.After(client.opts.reconnectDelay):
|
||||
}
|
||||
|
||||
deliveries, err = client.consume()
|
||||
if err != nil {
|
||||
log.Println("Error trying to consume, will try again")
|
||||
client.logger.Printf("Consume reconnect failed, retry: %s\n", err)
|
||||
reconnectTrigger()
|
||||
continue
|
||||
}
|
||||
|
||||
// re-create closing chan
|
||||
chClosedCh = make(chan *amqp.Error, 1)
|
||||
client.Channel.NotifyClose(chClosedCh)
|
||||
client.logger.Println("Consume reconnect success")
|
||||
|
||||
case delivery := <-deliveries:
|
||||
msgCh <- delivery.Body
|
||||
log.Printf("Received message: %s\n", delivery.Body)
|
||||
case amqErr := <-chClosedCh:
|
||||
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
||||
reconnectTrigger()
|
||||
|
||||
if err = delivery.Ack(false); err != nil {
|
||||
log.Printf("Error acknowledging message: %s\n", err)
|
||||
case delivery, ok := <-deliveries:
|
||||
if !ok {
|
||||
client.logger.Println("Deliveries channel closed unexpectedly")
|
||||
reconnectTrigger()
|
||||
continue
|
||||
}
|
||||
|
||||
if err = limiter.Wait(runCtx); err != nil {
|
||||
client.logger.Printf("Wait limiter failed: %s\n", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-runCtx.Done():
|
||||
if err = delivery.Nack(false, true); err != nil {
|
||||
client.logger.Printf("Error nacking message: %s\n", err)
|
||||
}
|
||||
|
||||
err = client.Close()
|
||||
if err != nil {
|
||||
client.logger.Printf("Close failed: %s\n", err)
|
||||
}
|
||||
return
|
||||
|
||||
case msgCh <- delivery.Body:
|
||||
client.logger.Printf("Received message: %s\n", delivery.Body)
|
||||
if err = delivery.Ack(false); err != nil {
|
||||
client.logger.Printf("Error acknowledging message: %s\n", err)
|
||||
}
|
||||
}
|
||||
limiter.Wait(runCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
85
handler.go
85
handler.go
|
|
@ -1,8 +1,10 @@
|
|||
package rabbit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
|
@ -11,7 +13,7 @@ import (
|
|||
|
||||
type Client struct {
|
||||
mutex *sync.Mutex
|
||||
queueName string
|
||||
queueOptions *QueueOpts
|
||||
logger *log.Logger
|
||||
connection *amqp.Connection
|
||||
Channel *amqp.Channel
|
||||
|
|
@ -21,44 +23,68 @@ type Client struct {
|
|||
notifyConfirm chan amqp.Confirmation
|
||||
isReady bool
|
||||
opts options
|
||||
connected chan struct{}
|
||||
}
|
||||
|
||||
type options struct {
|
||||
connectTimeout time.Duration
|
||||
reconnectDelay time.Duration
|
||||
reInitDelay time.Duration
|
||||
resendDelay time.Duration
|
||||
consumerRateLimit time.Duration
|
||||
consumerBurstSize int
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
|
||||
if addr == "" {
|
||||
return nil, errNoAddr
|
||||
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)
|
||||
|
||||
addr, err := address.makeAddr()
|
||||
if err != nil {
|
||||
return nil, errors.Join(errBadAddr, err)
|
||||
}
|
||||
|
||||
if queueName == "" {
|
||||
return nil, errNoQueue
|
||||
if queueOpts.QueueName == "" {
|
||||
l.Fatal(errNoQueue)
|
||||
}
|
||||
|
||||
client := Client{
|
||||
mutex: &sync.Mutex{},
|
||||
logger: log.New(os.Stdout, "", log.LstdFlags),
|
||||
queueName: queueName,
|
||||
queueOptions: &queueOpts,
|
||||
done: make(chan bool),
|
||||
connected: make(chan struct{}),
|
||||
logger: l,
|
||||
}
|
||||
|
||||
o := options{
|
||||
reconnectDelay: 5,
|
||||
reInitDelay: 2,
|
||||
resendDelay: 5,
|
||||
connectTimeout: 15 * time.Second,
|
||||
reconnectDelay: 5 * time.Second,
|
||||
reInitDelay: 2 * time.Second,
|
||||
resendDelay: 5 * time.Second,
|
||||
consumerRateLimit: time.Millisecond * 500,
|
||||
consumerBurstSize: 10,
|
||||
logger: log.New(io.Discard, "", 0),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
|
||||
client.logger = o.logger
|
||||
|
||||
if err = client.connectAndSignal(addr, o.connectTimeout); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
|
||||
go client.handleReconnect(addr)
|
||||
|
||||
return &client, nil
|
||||
|
|
@ -68,6 +94,37 @@ func NewPublisher(client *Client) Publisher {
|
|||
return &pubHandler{client: client}
|
||||
}
|
||||
|
||||
func NewConsumer(ctx context.Context, client *Client, chanLen int) Consumer {
|
||||
return &consumeHandler{ctx: ctx, client: client, chanLen: chanLen}
|
||||
func NewConsumer(client *Client) Consumer {
|
||||
return &consumeHandler{client: client}
|
||||
}
|
||||
|
||||
func (c *Client) connectAndSignal(addr string, timeout time.Duration) error {
|
||||
type result struct {
|
||||
conn *amqp.Connection
|
||||
err error
|
||||
}
|
||||
resCh := make(chan result, 1)
|
||||
|
||||
go func() {
|
||||
conn, err := amqp.Dial(addr)
|
||||
resCh <- result{conn, err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
return fmt.Errorf("connection timeout after %v", timeout)
|
||||
case res := <-resCh:
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
|
||||
c.changeConnection(res.conn)
|
||||
if err := c.init(res.conn); err != nil {
|
||||
res.conn.Close()
|
||||
return fmt.Errorf("init failed: %w", err)
|
||||
}
|
||||
|
||||
close(c.connected)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package rabbit
|
|||
import "errors"
|
||||
|
||||
var (
|
||||
errNoAddr = errors.New("no address")
|
||||
errBadAddr = errors.New("bad address")
|
||||
errNoQueue = errors.New("no queue")
|
||||
errNotConnected = errors.New("not connected to a server")
|
||||
errAlreadyClosed = errors.New("already closed: not connected to the server")
|
||||
|
|
|
|||
27
options.go
27
options.go
|
|
@ -1,9 +1,20 @@
|
|||
package rabbit
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Option func(*options)
|
||||
|
||||
func WithConnectTimeout(t time.Duration) Option {
|
||||
return func(op *options) {
|
||||
op.connectTimeout = t
|
||||
}
|
||||
}
|
||||
|
||||
func WithReconnectDelay(t time.Duration) Option {
|
||||
return func(op *options) {
|
||||
op.reconnectDelay = t
|
||||
|
|
@ -33,3 +44,17 @@ func WithConsumerBurstSize(t int) Option {
|
|||
op.consumerBurstSize = t
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(l *log.Logger) Option {
|
||||
return func(op *options) { op.logger = l }
|
||||
}
|
||||
|
||||
func WithLogging(enabled bool) Option {
|
||||
return func(op *options) {
|
||||
if enabled {
|
||||
op.logger = log.New(os.Stdout, "", log.LstdFlags)
|
||||
} else {
|
||||
op.logger = log.New(io.Discard, "", 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
publisher.go
29
publisher.go
|
|
@ -1,19 +1,44 @@
|
|||
package rabbit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
Push(data []byte) error
|
||||
Start(ctx context.Context, chanLen uint) chan<- []byte
|
||||
}
|
||||
|
||||
type pubHandler struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (p *pubHandler) Push(data []byte) error {
|
||||
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 {
|
||||
p.client.logger.Printf("Error publishing message (shutdown): %s", err)
|
||||
}
|
||||
}
|
||||
p.client.logger.Println("Publisher stopped")
|
||||
return
|
||||
|
||||
case msg := <-ch:
|
||||
if err := p.push(msg); err != nil {
|
||||
p.client.logger.Printf("Error publishing message: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
func (p *pubHandler) push(data []byte) error {
|
||||
p.client.mutex.Lock()
|
||||
if !p.client.isReady {
|
||||
p.client.mutex.Unlock()
|
||||
|
|
|
|||
23
service.go
23
service.go
|
|
@ -8,10 +8,6 @@ import (
|
|||
|
||||
func (c *Client) handleReconnect(addr string) {
|
||||
for {
|
||||
c.mutex.Lock()
|
||||
c.isReady = false
|
||||
c.mutex.Unlock()
|
||||
|
||||
c.logger.Println("Connecting to server...")
|
||||
|
||||
conn, err := c.connect(addr)
|
||||
|
|
@ -45,10 +41,6 @@ func (c *Client) connect(addr string) (*amqp.Connection, error) {
|
|||
|
||||
func (c *Client) handleReInit(conn *amqp.Connection) bool {
|
||||
for {
|
||||
c.mutex.Lock()
|
||||
c.isReady = false
|
||||
c.mutex.Unlock()
|
||||
|
||||
if err := c.init(conn); err != nil {
|
||||
c.logger.Printf("Failed to initialize connection: %s", err)
|
||||
|
||||
|
|
@ -86,7 +78,16 @@ func (c *Client) init(conn *amqp.Connection) error {
|
|||
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 {
|
||||
return err
|
||||
}
|
||||
|
|
@ -128,7 +129,7 @@ func (c *Client) unsafePush(data []byte) error {
|
|||
return c.Channel.PublishWithContext(
|
||||
ctx,
|
||||
"",
|
||||
c.queueName,
|
||||
c.queueOptions.QueueName,
|
||||
false,
|
||||
false,
|
||||
amqp.Publishing{
|
||||
|
|
@ -155,7 +156,7 @@ func (c *Client) consume() (<-chan amqp.Delivery, error) {
|
|||
}
|
||||
|
||||
return c.Channel.Consume(
|
||||
c.queueName,
|
||||
c.queueOptions.QueueName,
|
||||
"",
|
||||
false,
|
||||
false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue