-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (77 loc) · 1.54 KB
/
client.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
// Package msgr is the latest iteration of the message queueing package.
package msgr
import (
"log"
"strings"
"sync"
"github.com/streadway/amqp"
)
type (
// Client defines the base message queueing behavior
Client interface {
Dial() error
Close()
}
// Producer defines the base message producing behavior
Producer interface {
Post([]byte) bool
Close()
}
// Consumer defines the base message consuming behavior
Consumer interface {
Accept() (bool, <-chan amqp.Delivery)
Close()
}
)
type (
// Config is the queue configuration settings.
Config struct {
URI string
Channel string
}
// QueueProducer implements Producer.
QueueProducer struct {
*QueueClient
}
// QueueConsumer implements Consumer.
QueueConsumer struct {
*QueueClient
}
// QueueClient implements the client behavior.
QueueClient struct {
conn *amqp.Connection
channel *amqp.Channel
mu sync.Mutex
conf *Config
}
)
// ConnectP returns a producer.
func ConnectP(conf *Config) *QueueProducer {
c := &QueueClient{
conf: conf,
}
c.Dial()
return &QueueProducer{c}
}
// ConnectC returns a consumer.
func ConnectC(conf *Config) *QueueConsumer {
c := &QueueClient{
conf: conf,
}
c.Dial()
return &QueueConsumer{c}
}
// Dial makes a connection.
func (c *QueueClient) Dial() {
c.conn = dial(c.conf.URI)
}
// Close closes a connection.
func (c *QueueClient) Close() {
err := c.conn.Close()
if err != nil {
// Ignore 504 - channel/connection is not open
if !strings.Contains(err.Error(), "Exception (504)") {
log.Println(err)
}
}
}