Skip to content

Commit 0480cd0

Browse files
committed
apply functional pattern to receive and send
Signed-off-by: Jose Silva <[email protected]>
1 parent d173978 commit 0480cd0

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

protocol/amqp/v2/options.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
// Option is the function signature required to be considered an amqp.Option.
1313
type Option func(*Protocol) error
1414

15-
// WithConnOpt sets a connection option for amqp
15+
type SendOption func(sender *sender)
16+
type ReceiveOption func(receiver *receiver)
17+
18+
// WithConnOpts sets a connection option for amqp
1619
func WithConnOpts(opt *amqp.ConnOptions) Option {
1720
return func(t *Protocol) error {
1821
t.connOpts = opt
@@ -26,43 +29,59 @@ func WithConnSASLPlain(opt *amqp.ConnOptions, username, password string) Option
2629
return WithConnOpts(opt)
2730
}
2831

29-
// WithSessionOpt sets a session option for amqp
32+
// WithSessionOpts sets a session option for amqp
3033
func WithSessionOpts(opt *amqp.SessionOptions) Option {
3134
return func(t *Protocol) error {
3235
t.sessionOpts = opt
3336
return nil
3437
}
3538
}
3639

37-
// WithSenderLinkOption sets a link option for amqp
40+
// WithSenderOpts sets a link option for amqp
3841
func WithSenderOpts(opt *amqp.SenderOptions) Option {
3942
return func(t *Protocol) error {
4043
t.senderOpts = opt
4144
return nil
4245
}
4346
}
4447

45-
// WithReceiverLinkOption sets a link option for amqp
48+
// WithReceiverOpts sets a link option for amqp
4649
func WithReceiverOpts(opt *amqp.ReceiverOptions) Option {
4750
return func(t *Protocol) error {
4851
t.receiverOpts = opt
4952
return nil
5053
}
5154
}
5255

53-
func WithReceiveOpts(opt amqp.ReceiveOptions) Option {
56+
// WithReceiveOpts sets a receive option for amqp
57+
func WithReceiveOpts(opt *amqp.ReceiveOptions) Option {
5458
return func(t *Protocol) error {
5559
t.receiveOpts = opt
5660
return nil
5761
}
5862
}
5963

64+
// WithSendOpts sets a send option for amqp
6065
func WithSendOpts(opt *amqp.SendOptions) Option {
6166
return func(t *Protocol) error {
6267
t.sendOpts = opt
6368
return nil
6469
}
6570
}
6671

72+
// WithSendOptions sets send options for amqp
73+
func WithSendOptions(opts *amqp.SendOptions) SendOption {
74+
return func(t *sender) {
75+
t.options = opts
76+
}
77+
}
78+
79+
// WithReceiveOptions sets receive options for amqp
80+
func WithReceiveOptions(opts *amqp.ReceiveOptions) ReceiveOption {
81+
return func(t *receiver) {
82+
t.options = opts
83+
}
84+
}
85+
6786
// SenderOptionFunc is the type of amqp.Sender options
6887
type SenderOptionFunc func(sender *sender)

protocol/amqp/v2/protocol.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Protocol struct {
2626
senderOpts *amqp.SenderOptions
2727
receiverOpts *amqp.ReceiverOptions
2828
sendOpts *amqp.SendOptions
29-
receiveOpts amqp.ReceiveOptions
29+
receiveOpts *amqp.ReceiveOptions
3030

3131
// Sender
3232
Sender *sender
@@ -60,14 +60,14 @@ func NewProtocolFromClient(
6060
_ = session.Close(context.Background())
6161
return nil, err
6262
}
63-
t.Sender = NewSender(amqpSender, t.sendOpts).(*sender)
63+
t.Sender = NewSender(amqpSender, WithSendOptions(t.sendOpts)).(*sender)
6464
t.SenderContextDecorators = []func(context.Context) context.Context{}
6565

6666
amqpReceiver, err := t.Session.NewReceiver(ctx, t.Node, t.receiverOpts)
6767
if err != nil {
6868
return nil, err
6969
}
70-
t.Receiver = NewReceiver(amqpReceiver, t.receiveOpts).(*receiver)
70+
t.Receiver = NewReceiver(amqpReceiver, WithReceiveOptions(t.receiveOpts)).(*receiver)
7171
return t, nil
7272
}
7373

@@ -124,7 +124,8 @@ func NewSenderProtocolFromClient(
124124
_ = session.Close(context.Background())
125125
return nil, err
126126
}
127-
t.Sender = NewSender(amqpSender, t.sendOpts).(*sender)
127+
t.Sender = NewSender(amqpSender).(*sender)
128+
128129
t.SenderContextDecorators = []func(context.Context) context.Context{}
129130

130131
return t, nil
@@ -152,7 +153,7 @@ func NewReceiverProtocolFromClient(
152153
if err != nil {
153154
return nil, err
154155
}
155-
t.Receiver = NewReceiver(amqpReceiver, t.receiveOpts).(*receiver)
156+
t.Receiver = NewReceiver(amqpReceiver, WithReceiveOptions(t.receiveOpts)).(*receiver)
156157
return t, nil
157158
}
158159

protocol/amqp/v2/receiver.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const serverDown = "session ended by server"
2121
// receiver wraps an amqp.Receiver as a binding.Receiver
2222
type receiver struct {
2323
amqp *amqp.Receiver
24-
options amqp.ReceiveOptions
24+
options *amqp.ReceiveOptions
2525
}
2626

2727
func (r *receiver) Receive(ctx context.Context) (binding.Message, error) {
28-
m, err := r.amqp.Receive(ctx, &r.options)
28+
m, err := r.amqp.Receive(ctx, r.options)
2929
if err != nil {
3030
if err == ctx.Err() {
3131
return nil, io.EOF
@@ -41,6 +41,15 @@ func (r *receiver) Receive(ctx context.Context) (binding.Message, error) {
4141
}
4242

4343
// NewReceiver create a new Receiver which wraps an amqp.Receiver in a binding.Receiver
44-
func NewReceiver(amqp *amqp.Receiver, options amqp.ReceiveOptions) protocol.Receiver {
45-
return &receiver{amqp: amqp, options: options}
44+
func NewReceiver(amqp *amqp.Receiver, opts ...ReceiveOption) protocol.Receiver {
45+
r := &receiver{amqp: amqp, options: nil}
46+
applyReceiveOptions(r, opts...)
47+
return r
48+
}
49+
50+
func applyReceiveOptions(s *receiver, opts ...ReceiveOption) *receiver {
51+
for _, o := range opts {
52+
o(s)
53+
}
54+
return s
4655
}

protocol/amqp/v2/sender.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ func (s *sender) Send(ctx context.Context, in binding.Message, transformers ...b
3939
}
4040

4141
// NewSender creates a new Sender which wraps an amqp.Sender in a binding.Sender
42-
func NewSender(amqpSender *amqp.Sender, options *amqp.SendOptions) protocol.Sender {
43-
s := &sender{amqp: amqpSender, options: options}
42+
func NewSender(amqpSender *amqp.Sender, opts ...SendOption) protocol.Sender {
43+
s := &sender{amqp: amqpSender, options: nil}
44+
applySenderOptions(s, opts...)
45+
return s
46+
}
4447

48+
func applySenderOptions(s *sender, opts ...SendOption) *sender {
49+
for _, o := range opts {
50+
o(s)
51+
}
4552
return s
4653
}

test/integration/amqp_binding/amqp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/url"
1212
"os"
13+
"strings"
1314
"testing"
1415

1516
"github.com/Azure/go-amqp"
@@ -95,7 +96,7 @@ func testClient(t testing.TB) (client *amqp.Conn, session *amqp.Session, addr st
9596
addr = "test"
9697
s := os.Getenv("TEST_AMQP_URL")
9798
if u, err := url.Parse(s); err == nil && u.Path != "" {
98-
addr = u.Path
99+
addr = strings.TrimPrefix(u.Path, "/")
99100
}
100101
client, err := amqp.Dial(context.Background(), s, &amqp.ConnOptions{})
101102
if err != nil {
@@ -105,7 +106,6 @@ func testClient(t testing.TB) (client *amqp.Conn, session *amqp.Session, addr st
105106
require.NoError(t, err)
106107

107108
return client, session, addr
108-
109109
}
110110

111111
func testSenderReceiver(t testing.TB) (io.Closer, bindings.Sender, bindings.Receiver) {
@@ -114,7 +114,7 @@ func testSenderReceiver(t testing.TB) (io.Closer, bindings.Sender, bindings.Rece
114114
require.NoError(t, err)
115115
s, err := ss.NewSender(context.Background(), a, nil)
116116
require.NoError(t, err)
117-
return c, protocolamqp.NewSender(s, nil), protocolamqp.NewReceiver(r, amqp.ReceiveOptions{})
117+
return c, protocolamqp.NewSender(s), protocolamqp.NewReceiver(r)
118118
}
119119

120120
func BenchmarkSendReceive(b *testing.B) {

0 commit comments

Comments
 (0)