-
Notifications
You must be signed in to change notification settings - Fork 18
/
publisherpool.go
141 lines (117 loc) · 3.57 KB
/
publisherpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package rabbit
import (
"context"
"encoding/json"
"errors"
pool "github.com/jolestar/go-commons-pool/v2"
"github.com/makasim/amqpextra"
"github.com/makasim/amqpextra/publisher"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/circleci/ex/o11y"
)
type PublisherPool struct {
pool *pool.ObjectPool
name string
}
func NewPublisherPool(ctx context.Context, name string, dialer *amqpextra.Dialer,
opts ...publisher.Option) *PublisherPool {
opts = append(opts,
publisher.WithConfirmation(10),
publisher.WithInitFunc(func(conn publisher.AMQPConnection) (publisher.AMQPChannel, error) {
ch, err := conn.(*amqp.Connection).Channel()
if err != nil {
return nil, err
}
ch.NotifyReturn(newReturnedMessageHandler(ctx))
return ch, nil
}),
)
create := func(ctx context.Context) (interface{}, error) {
return dialer.Publisher(opts...)
}
destroy := func(ctx context.Context, object *pool.PooledObject) error {
pub := object.Object.(*publisher.Publisher)
pub.Close()
return nil
}
factory := pool.NewPooledObjectFactory(create, destroy, nil, nil, nil)
poolConfig := pool.NewDefaultPoolConfig()
poolConfig.MaxTotal = 16
poolConfig.MaxIdle = 16
return &PublisherPool{
name: name,
pool: pool.NewObjectPool(context.Background(), factory, poolConfig),
}
}
func (p *PublisherPool) Close(ctx context.Context) (err error) {
ctx, span := o11y.StartSpan(ctx, "pool: close")
defer o11y.End(span, &err)
p.pool.Close(ctx)
return nil
}
func (p *PublisherPool) Publish(ctx context.Context, msg publisher.Message) (err error) {
ctx, span := o11y.StartSpan(ctx, "pool: publish")
defer o11y.End(span, &err)
span.AddField("exchange", msg.Exchange)
span.AddField("key", msg.Key)
span.AddField("content_type", msg.Publishing.ContentType)
return p.publish(ctx, msg)
}
const JSON = "application/json; charset=utf-8"
func (p *PublisherPool) PublishJSON(ctx context.Context, msg publisher.Message, v interface{}) (err error) {
ctx, span := o11y.StartSpan(ctx, "pool: publish_json")
defer o11y.End(span, &err)
span.AddField("exchange", msg.Exchange)
span.AddField("key", msg.Key)
span.AddField("content_type", JSON)
msg.Publishing.ContentType = JSON
msg.Publishing.Body, err = json.Marshal(v)
if err != nil {
return err
}
return p.publish(ctx, msg)
}
func (p *PublisherPool) publish(ctx context.Context, msg publisher.Message) error {
msg.Mandatory = true
msg.Context = ctx
obj, err := p.pool.BorrowObject(ctx)
if err != nil {
return err
}
pub := obj.(*publisher.Publisher)
defer func() {
err := p.pool.ReturnObject(ctx, obj)
if err != nil {
o11y.LogError(ctx, "pool: error returning publisher to pool", err)
pub.Close()
}
}()
return pub.Publish(msg)
}
func (p *PublisherPool) MetricName() string {
return p.name
}
func (p *PublisherPool) Gauges(_ context.Context) map[string]float64 {
return map[string]float64{
"active": float64(p.pool.GetNumActive()),
"idle": float64(p.pool.GetNumIdle()),
"destroyed": float64(p.pool.GetDestroyedCount()),
"max_total": float64(p.pool.Config.MaxTotal),
"max_idle": float64(p.pool.Config.MaxIdle),
"min_idle": float64(p.pool.Config.MinIdle),
}
}
func newReturnedMessageHandler(ctx context.Context) chan amqp.Return {
returnNotify := make(chan amqp.Return)
go func() {
// The Go channel will be closed by the AMQP channel on shutdown
for r := range returnNotify {
o11y.LogError(ctx, "pool: returned message", errors.New(r.ReplyText),
o11y.Field("reply_code", r.ReplyCode),
o11y.Field("exchange", r.Exchange),
o11y.Field("routing_key", r.RoutingKey),
)
}
}()
return returnNotify
}