-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexchange.go
78 lines (68 loc) · 1.44 KB
/
exchange.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
// version: 0.0.1
// file to build exchange
package gobroker
import (
errors "errors"
amqp "github.com/rabbitmq/amqp091-go"
)
// exchane struct
type Exchange struct {
broker *Broker
name string
}
// exchange options
type ExchangeOptions struct {
Type string
Durable bool
AutoDelete bool
Internal bool
NoWait bool
Args amqp.Table
}
func (eo *ExchangeOptions) defaultOpts() {
// set defaults
eo.Type = "topic"
eo.Durable = true
}
// build exchange
func (b *Broker) BuildExchange(name string, opts ...*ExchangeOptions) (*Exchange, error) {
// check exchange name
if name == "" {
return nil, errors.New("invalid name")
}
// create default exchange
exchange := &Exchange{broker: b, name: name}
// setup connections publisher connection
publishConn, err := b.GetConnection(PublishConnection)
if err != nil {
return nil, err
}
// set default options
options := &ExchangeOptions{}
options.defaultOpts()
// check if options provided
if len(opts) != 0 {
options = opts[0]
}
// get a connection channel
ch, err := publishConn.GetChannel()
if err != nil {
return nil, err
}
// close this channel as we do not require this active channel until we publish
defer ch.Close()
// set exchange options
err = ch.ExchangeDeclare(
name,
options.Type,
options.Durable,
options.AutoDelete,
options.Internal,
options.NoWait,
options.Args,
)
if err != nil {
return nil, err
}
return exchange, nil
}