-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.go
More file actions
65 lines (56 loc) · 1.81 KB
/
net.go
File metadata and controls
65 lines (56 loc) · 1.81 KB
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
package msgr
import (
"log"
"time"
"github.com/streadway/amqp"
)
var (
// Internal default settings.
// WARNING- these should only be modified during tests.
// Producer defaults.
publishMandatory = true // mandatory requires a queue to already be declared.
publishImmediate = false // immediate requires a consumer to be available on the other end.
// Queue defaults.
queueDurable = true // durable will survive server restarts
queueDelete = false // delete will close a queue when there are no consumers or bindings
queueExclusive = false // exclusive queues are only accessible by the connection that declares them
queueNoWait = false // no-wait will assume the queue was declared on the server
// Consumer defaults.
consumeAutoAck = false // auto-ack is false, the consumer should always call Delivery.Ack
consumeExclusive = false // exclusive ensures that this is the sole consumer of a queue
consumeNoWait = false // do not wait for the server to confirm the request and immediately begin deliveries
)
func dial(uri string) *amqp.Connection {
for {
conn, err := amqp.Dial(uri)
if err == nil {
log.Println("connected to queue")
return conn
}
log.Println("dial failed. retrying in 1s: ", err)
time.Sleep(1000 * time.Millisecond)
}
}
func channel(conn *amqp.Connection, name string) *amqp.Channel {
for {
channel, err := conn.Channel()
if err == nil {
_, err = channel.QueueDeclare(
name, // name
queueDurable, // durable
queueDelete, // delete when unused
queueExclusive, // exclusive
queueNoWait, // no-wait
nil, // arguments
)
if err == nil {
return channel
}
}
if err == amqp.ErrClosed {
return nil
}
log.Println("channel open failed. retrying in 1s: ", err)
time.Sleep(1000 * time.Millisecond)
}
}