forked from mantzas/patron
/
option.go
55 lines (47 loc) · 1.17 KB
/
option.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
package amqp
import (
"errors"
"net"
"time"
"github.com/streadway/amqp"
)
// OptionFunc definition for configuring the consumer in a functional way.
type OptionFunc func(*consumer) error
// Buffer option for adjusting the incoming messages buffer.
func Buffer(buf int) OptionFunc {
return func(c *consumer) error {
if buf < 0 {
return errors.New("buffer must greater or equal than 0")
}
c.buffer = buf
return nil
}
}
// Timeout option for adjusting the timeout of the connection.
func Timeout(timeout time.Duration) OptionFunc {
return func(c *consumer) error {
c.cfg = amqp.Config{
Dial: func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
},
}
return nil
}
}
// Requeue option for adjusting the requeue policy of a message.
func Requeue(requeue bool) OptionFunc {
return func(c *consumer) error {
c.requeue = requeue
return nil
}
}
// Bindings option for providing custom exchange-queue bindings.
func Bindings(bindings ...string) OptionFunc {
return func(c *consumer) error {
if len(bindings) == 0 {
return errors.New("provided bindings cannot be empty")
}
c.bindings = bindings
return nil
}
}