forked from Azure/go-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
215 lines (187 loc) · 5.54 KB
/
client.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package amqp
import (
"encoding/base64"
"errors"
"fmt"
"math/rand"
"net"
"sync"
"time"
"github.com/Azure/go-amqp/internal/encoding"
"github.com/Azure/go-amqp/internal/frames"
)
var (
// ErrSessionClosed is propagated to Sender/Receivers
// when Session.Close() is called.
ErrSessionClosed = errors.New("amqp: session closed")
// ErrLinkClosed is returned by send and receive operations when
// Sender.Close() or Receiver.Close() are called.
ErrLinkClosed = errors.New("amqp: link closed")
// ErrLinkDetached is returned by operations when the
// link is in a detached state.
ErrLinkDetached = errors.New("amqp: link detached")
)
// Client is an AMQP client connection.
type Client struct {
conn *conn
}
// Dial connects to an AMQP server.
//
// If the addr includes a scheme, it must be "amqp" or "amqps".
// If no port is provided, 5672 will be used for "amqp" and 5671 for "amqps".
//
// If username and password information is not empty it's used as SASL PLAIN
// credentials, equal to passing ConnSASLPlain option.
func Dial(addr string, opts ...ConnOption) (*Client, error) {
c, err := dialConn(addr, opts...)
if err != nil {
return nil, err
}
err = c.Start()
if err != nil {
return nil, err
}
return &Client{conn: c}, nil
}
// New establishes an AMQP client connection over conn.
func New(conn net.Conn, opts ...ConnOption) (*Client, error) {
c, err := newConn(conn, opts...)
if err != nil {
return nil, err
}
err = c.Start()
return &Client{conn: c}, err
}
// Close disconnects the connection.
func (c *Client) Close() error {
return c.conn.Close()
}
// NewSession opens a new AMQP session to the server.
// Returns ErrConnClosed if the underlying connection has been closed.
func (c *Client) NewSession(opts ...SessionOption) (*Session, error) {
// get a session allocated by Client.mux
var sResp newSessionResp
select {
case <-c.conn.Done:
return nil, c.conn.Err()
case sResp = <-c.conn.NewSession:
}
if sResp.err != nil {
return nil, sResp.err
}
s := sResp.session
for _, opt := range opts {
err := opt(s)
if err != nil {
// deallocate session on error. we can't call
// s.Close() as the session mux hasn't started yet.
c.conn.DelSession <- s
return nil, err
}
}
// send Begin to server
begin := &frames.PerformBegin{
NextOutgoingID: 0,
IncomingWindow: s.incomingWindow,
OutgoingWindow: s.outgoingWindow,
HandleMax: s.handleMax,
}
debug(1, "TX (NewSession): %s", begin)
_ = s.txFrame(begin, nil)
// wait for response
var fr frames.Frame
select {
case <-c.conn.Done:
return nil, c.conn.Err()
case fr = <-s.rx:
}
debug(1, "RX (NewSession): %s", fr.Body)
begin, ok := fr.Body.(*frames.PerformBegin)
if !ok {
// this codepath is hard to hit (impossible?). if the response isn't a PerformBegin and we've not
// yet seen the remote channel number, the default clause in conn.mux will protect us from that.
// if we have seen the remote channel number then it's likely the session.mux for that channel will
// either swallow the frame or blow up in some other way, both causing this call to hang.
// deallocate session on error. we can't call
// s.Close() as the session mux hasn't started yet.
c.conn.DelSession <- s
return nil, fmt.Errorf("unexpected begin response: %+v", fr.Body)
}
// start Session multiplexor
go s.mux(begin)
return s, nil
}
// Default session options
const (
DefaultMaxLinks = 4294967296
DefaultWindow = 1000
)
// SessionOption is an function for configuring an AMQP session.
type SessionOption func(*Session) error
// SessionIncomingWindow sets the maximum number of unacknowledged
// transfer frames the server can send.
func SessionIncomingWindow(window uint32) SessionOption {
return func(s *Session) error {
s.incomingWindow = window
return nil
}
}
// SessionOutgoingWindow sets the maximum number of unacknowledged
// transfer frames the client can send.
func SessionOutgoingWindow(window uint32) SessionOption {
return func(s *Session) error {
s.outgoingWindow = window
return nil
}
}
// SessionMaxLinks sets the maximum number of links (Senders/Receivers)
// allowed on the session.
//
// n must be in the range 1 to 4294967296.
//
// Default: 4294967296.
func SessionMaxLinks(n int) SessionOption {
return func(s *Session) error {
if n < 1 {
return errors.New("max sessions cannot be less than 1")
}
if int64(n) > 4294967296 {
return errors.New("max sessions cannot be greater than 4294967296")
}
s.handleMax = uint32(n - 1)
return nil
}
}
// lockedRand provides a rand source that is safe for concurrent use.
type lockedRand struct {
mu sync.Mutex
src *rand.Rand
}
func (r *lockedRand) Read(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
return r.src.Read(p)
}
// package scoped rand source to avoid any issues with seeding
// of the global source.
var pkgRand = &lockedRand{
src: rand.New(rand.NewSource(time.Now().UnixNano())),
}
// randBytes returns a base64 encoded string of n bytes.
func randString(n int) string {
b := make([]byte, n)
// from math/rand, cannot fail
_, _ = pkgRand.Read(b)
return base64.RawURLEncoding.EncodeToString(b)
}
// linkKey uniquely identifies a link on a connection by name and direction.
//
// A link can be identified uniquely by the ordered tuple
// (source-container-id, target-container-id, name)
// On a single connection the container ID pairs can be abbreviated
// to a boolean flag indicating the direction of the link.
type linkKey struct {
name string
role encoding.Role // Local role: sender/receiver
}
const maxTransferFrameHeader = 66 // determined by calcMaxTransferFrameHeader