-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducer.go
executable file
·66 lines (54 loc) · 1.22 KB
/
producer.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
package nsqclient
import (
"errors"
"time"
"github.com/goapt/nsqclient/internal/pool"
)
type Producer interface {
Publish(topic string, body []byte) error
MultiPublish(topic string, body [][]byte) error
DeferredPublish(topic string, delay time.Duration, body []byte) error
}
var _ Producer = (*producer)(nil)
type producer struct {
pool pool.Pool
}
func NewProducer(name string) (*producer, error) {
p, ok := Client(name)
if !ok {
return nil, errors.New("nsq producer config not found")
}
return &producer{
pool: p,
}, nil
}
func (p *producer) Publish(topic string, body []byte) error {
nsq, err := p.pool.Get()
if err != nil {
return err
}
defer nsq.Close()
return retry(2, func() error {
return nsq.Publish(topic, body)
})
}
func (p *producer) MultiPublish(topic string, body [][]byte) error {
nsq, err := p.pool.Get()
if err != nil {
return err
}
defer nsq.Close()
return retry(2, func() error {
return nsq.MultiPublish(topic, body)
})
}
func (p *producer) DeferredPublish(topic string, delay time.Duration, body []byte) error {
nsq, err := p.pool.Get()
if err != nil {
return err
}
defer nsq.Close()
return retry(2, func() error {
return nsq.DeferredPublish(topic, delay, body)
})
}