-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher_options.go
95 lines (83 loc) · 3.26 KB
/
publisher_options.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
86
87
88
89
90
91
92
93
94
95
package grabbit
import "context"
// PublisherUsageOptions defines parameters for driving the publishers
// behavior and indicating to the supporting channel that publishing
// operations are enabled.
type PublisherUsageOptions struct {
ConfirmationCount int // size of publishing confirmations over the amqp channel
ConfirmationNoWait bool // publisher confirmation mode parameter
IsPublisher bool // indicates if this chan is used for publishing
}
// PublisherOptions defines publisher specific parameters. Mostly used as defaults for
// sending messages and inner channel functionality.
type PublisherOptions struct {
PublisherUsageOptions
Context context.Context // controlling environment
Exchange string // routing exchange
Key string // routing key (usually queue name)
Mandatory bool // delivery is mandatory
Immediate bool // delivery is immediate
}
// DefaultPublisherOptions creates some sane defaults for publishing messages.
// Note: The Message/payload itself must still be an amqp.Publishing object,
// fully under application's control.
func DefaultPublisherOptions() PublisherOptions {
return PublisherOptions{
PublisherUsageOptions: PublisherUsageOptions{
ConfirmationCount: 10,
ConfirmationNoWait: false,
IsPublisher: true,
},
Context: context.TODO(),
Exchange: "",
Key: "",
Mandatory: false,
Immediate: false,
}
}
// WithConfirmationNoWait sets publisher's confirmation mode.
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithConfirmationNoWait(confNoWait bool) *PublisherOptions {
opt.ConfirmationNoWait = confNoWait
return opt
}
// WithContext sets the publisher's context.
//
// This context is specific to publishing operations and may be different than the supporting channel's context.
// We still recommended using the same value for both though unless you want strict control of e.g.
// [Publish, PublishWithOptions, PublishDeferredConfirm, PublishDeferredConfirmWithOptions, AwaitDeferredConfirmation].
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithContext(ctx context.Context) *PublisherOptions {
opt.Context = ctx
return opt
}
// WithExchange sets the publisher's routing exchange.
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithExchange(exchange string) *PublisherOptions {
opt.Exchange = exchange
return opt
}
// WithKey sets the publisher's routing key.
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithKey(key string) *PublisherOptions {
opt.Key = key
return opt
}
// WithMandatory sets the deliveries being mandatory flag.
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithMandatory(mandatory bool) *PublisherOptions {
opt.Mandatory = mandatory
return opt
}
// WithImmediate sets the deliveries being immediate flag.
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithImmediate(immediate bool) *PublisherOptions {
opt.Immediate = immediate
return opt
}
// WithConfirmationsCount sets the number of confirmations required (capacity of amqp.Confirmation).
// Returns the updated PublisherOptions.
func (opt *PublisherOptions) WithConfirmationsCount(count int) *PublisherOptions {
opt.ConfirmationCount = count
return opt
}