forked from Azure/go-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
883 lines (760 loc) · 21.6 KB
/
conn.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
package amqp
import (
"bytes"
"crypto/tls"
"errors"
"io"
"math"
"net"
"sync"
"time"
)
// Default connection options
const (
DefaultIdleTimeout = 1 * time.Minute
DefaultMaxFrameSize = 512
DefaultMaxSessions = 65536
)
// Errors
var (
ErrTimeout = errors.New("amqp: timeout waiting for response")
// ErrConnClosed is propagated to Session and Senders/Receivers
// when Client.Close() is called.
ErrConnClosed = errors.New("amqp: connection closed")
)
// ConnOption is a function for configuring an AMQP connection.
type ConnOption func(*conn) error
// ConnServerHostname sets the hostname sent in the AMQP
// Open frame and TLS ServerName (if not otherwise set).
//
// This is useful when the AMQP connection will be established
// via a pre-established TLS connection as the server may not
// know which hostname the client is attempting to connect to.
func ConnServerHostname(hostname string) ConnOption {
return func(c *conn) error {
c.hostname = hostname
return nil
}
}
// ConnTLS toggles TLS negotiation.
//
// Default: false.
func ConnTLS(enable bool) ConnOption {
return func(c *conn) error {
c.tlsNegotiation = enable
return nil
}
}
// ConnTLSConfig sets the tls.Config to be used during
// TLS negotiation.
//
// This option is for advanced usage, in most scenarios
// providing a URL scheme of "amqps://" or ConnTLS(true)
// is sufficient.
func ConnTLSConfig(tc *tls.Config) ConnOption {
return func(c *conn) error {
c.tlsConfig = tc
c.tlsNegotiation = true
return nil
}
}
// ConnIdleTimeout specifies the maximum period between receiving
// frames from the peer.
//
// Resolution is milliseconds. A value of zero indicates no timeout.
// This setting is in addition to TCP keepalives.
//
// Default: 1 minute.
func ConnIdleTimeout(d time.Duration) ConnOption {
return func(c *conn) error {
if d < 0 {
return errorNew("idle timeout cannot be negative")
}
c.idleTimeout = d
return nil
}
}
// ConnMaxFrameSize sets the maximum frame size that
// the connection will accept.
//
// Must be 512 or greater.
//
// Default: 512.
func ConnMaxFrameSize(n uint32) ConnOption {
return func(c *conn) error {
if n < 512 {
return errorNew("max frame size must be 512 or greater")
}
c.maxFrameSize = n
return nil
}
}
// ConnConnectTimeout configures how long to wait for the
// server during connection establishment.
//
// Once the connection has been established, ConnIdleTimeout
// applies. If duration is zero, no timeout will be applied.
//
// Default: 0.
func ConnConnectTimeout(d time.Duration) ConnOption {
return func(c *conn) error { c.connectTimeout = d; return nil }
}
// ConnMaxSessions sets the maximum number of channels.
//
// n must be in the range 1 to 65536.
//
// Default: 65536.
func ConnMaxSessions(n int) ConnOption {
return func(c *conn) error {
if n < 1 {
return errorNew("max sessions cannot be less than 1")
}
if n > 65536 {
return errorNew("max sessions cannot be greater than 65536")
}
c.channelMax = uint16(n - 1)
return nil
}
}
// ConnProperty sets an entry in the connection properties map sent to the server.
//
// This option can be used multiple times.
func ConnProperty(key, value string) ConnOption {
return func(c *conn) error {
if key == "" {
return errorNew("connection property key must not be empty")
}
if c.properties == nil {
c.properties = make(map[symbol]interface{})
}
c.properties[symbol(key)] = value
return nil
}
}
// conn is an AMQP connection.
type conn struct {
net net.Conn // underlying connection
connectTimeout time.Duration // time to wait for reads/writes during conn establishment
// TLS
tlsNegotiation bool // negotiate TLS
tlsComplete bool // TLS negotiation complete
tlsConfig *tls.Config // TLS config, default used if nil (ServerName set to Client.hostname)
// SASL
saslHandlers map[symbol]stateFunc // map of supported handlers keyed by SASL mechanism, SASL not negotiated if nil
saslComplete bool // SASL negotiation complete
// local settings
maxFrameSize uint32 // max frame size to accept
channelMax uint16 // maximum number of channels to allow
hostname string // hostname of remote server (set explicitly or parsed from URL)
idleTimeout time.Duration // maximum period between receiving frames
properties map[symbol]interface{} // additional properties sent upon connection open
// peer settings
peerIdleTimeout time.Duration // maximum period between sending frames
peerMaxFrameSize uint32 // maximum frame size peer will accept
// conn state
errMu sync.Mutex // mux holds errMu from start until shutdown completes; operations are sequential before mux is started
err error // error to be returned to client
done chan struct{} // indicates the connection is done
// mux
newSession chan newSessionResp // new Sessions are requested from mux by reading off this channel
delSession chan *Session // session completion is indicated to mux by sending the Session on this channel
connErr chan error // connReader/Writer notifications of an error
closeMux chan struct{} // indicates that the mux should stop
closeMuxOnce sync.Once
// connReader
rxProto chan protoHeader // protoHeaders received by connReader
rxFrame chan frame // AMQP frames received by connReader
rxDone chan struct{}
connReaderRun chan func() // functions to be run by conn reader (set deadline on conn to run)
// connWriter
txFrame chan frame // AMQP frames to be sent by connWriter
txBuf buffer // buffer for marshaling frames before transmitting
txDone chan struct{}
}
type newSessionResp struct {
session *Session
err error
}
func newConn(netConn net.Conn, opts ...ConnOption) (*conn, error) {
c := &conn{
net: netConn,
maxFrameSize: DefaultMaxFrameSize,
peerMaxFrameSize: DefaultMaxFrameSize,
channelMax: DefaultMaxSessions - 1, // -1 because channel-max starts at zero
idleTimeout: DefaultIdleTimeout,
done: make(chan struct{}),
connErr: make(chan error, 2), // buffered to ensure connReader/Writer won't leak
closeMux: make(chan struct{}),
rxProto: make(chan protoHeader),
rxFrame: make(chan frame),
rxDone: make(chan struct{}),
connReaderRun: make(chan func(), 1), // buffered to allow queueing function before interrupt
newSession: make(chan newSessionResp),
delSession: make(chan *Session),
txFrame: make(chan frame),
txDone: make(chan struct{}),
}
// apply options
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
func (c *conn) initTLSConfig() {
// create a new config if not already set
if c.tlsConfig == nil {
c.tlsConfig = new(tls.Config)
}
// TLS config must have ServerName or InsecureSkipVerify set
if c.tlsConfig.ServerName == "" && !c.tlsConfig.InsecureSkipVerify {
c.tlsConfig.ServerName = c.hostname
}
}
func (c *conn) start() error {
// start reader
go c.connReader()
// run connection establishment state machine
for state := c.negotiateProto; state != nil; {
state = state()
}
// check if err occurred
if c.err != nil {
close(c.txDone) // close here since connWriter hasn't been started yet
_ = c.Close()
return c.err
}
// start multiplexor and writer
go c.mux()
go c.connWriter()
return nil
}
func (c *conn) Close() error {
c.closeMuxOnce.Do(func() { close(c.closeMux) })
err := c.getErr()
if err == ErrConnClosed {
return nil
}
return err
}
// close should only be called by conn.mux.
func (c *conn) close() {
close(c.done) // notify goroutines and blocked functions to exit
// wait for writing to stop, allows it to send the final close frame
<-c.txDone
err := c.net.Close()
switch {
// conn.err already set
case c.err != nil:
// conn.err not set and c.net.Close() returned a non-nil error
case err != nil:
c.err = err
// no errors
default:
c.err = ErrConnClosed
}
// check rxDone after closing net, otherwise may block
// for up to c.idleTimeout
<-c.rxDone
}
// getErr returns conn.err.
//
// Must only be called after conn.done is closed.
func (c *conn) getErr() error {
c.errMu.Lock()
defer c.errMu.Unlock()
return c.err
}
// mux is started in it's own goroutine after initial connection establishment.
// It handles muxing of sessions, keepalives, and connection errors.
func (c *conn) mux() {
var (
// allocated channels
channels = &bitmap{max: uint32(c.channelMax)}
// create the next session to allocate
nextChannel, _ = channels.next()
nextSession = newSessionResp{session: newSession(c, uint16(nextChannel))}
// map channels to sessions
sessionsByChannel = make(map[uint16]*Session)
sessionsByRemoteChannel = make(map[uint16]*Session)
)
// hold the errMu lock until error or done
c.errMu.Lock()
defer c.errMu.Unlock()
defer c.close() // defer order is important. c.errMu unlock indicates that connection is finally complete
for {
// check if last loop returned an error
if c.err != nil {
return
}
select {
// error from connReader
case c.err = <-c.connErr:
// new frame from connReader
case fr := <-c.rxFrame:
var (
session *Session
ok bool
)
switch body := fr.body.(type) {
// RemoteChannel should be used when frame is Begin
case *performBegin:
session, ok = sessionsByChannel[body.RemoteChannel]
if !ok {
break
}
session.remoteChannel = fr.channel
sessionsByRemoteChannel[fr.channel] = session
default:
session, ok = sessionsByRemoteChannel[fr.channel]
}
if !ok {
c.err = errorErrorf("unexpected frame: %#v", fr.body)
continue
}
select {
case session.rx <- fr:
case <-c.closeMux:
return
}
// new session request
//
// Continually try to send the next session on the channel,
// then add it to the sessions map. This allows us to control ID
// allocation and prevents the need to have shared map. Since new
// sessions are far less frequent than frames being sent to sessions,
// this avoids the lock/unlock for session lookup.
case c.newSession <- nextSession:
if nextSession.err != nil {
continue
}
// save session into map
ch := nextSession.session.channel
sessionsByChannel[ch] = nextSession.session
// get next available channel
next, ok := channels.next()
if !ok {
nextSession = newSessionResp{err: errorErrorf("reached connection channel max (%d)", c.channelMax)}
continue
}
// create the next session to send
nextSession = newSessionResp{session: newSession(c, uint16(next))}
// session deletion
case s := <-c.delSession:
delete(sessionsByChannel, s.channel)
delete(sessionsByRemoteChannel, s.remoteChannel)
channels.remove(uint32(s.channel))
// connection is complete
case <-c.closeMux:
return
}
}
}
// connReader reads from the net.Conn, decodes frames, and passes them
// up via the conn.rxFrame and conn.rxProto channels.
func (c *conn) connReader() {
defer close(c.rxDone)
buf := new(buffer)
var (
negotiating = true // true during conn establishment, check for protoHeaders
currentHeader frameHeader // keep track of the current header, for frames split across multiple TCP packets
frameInProgress bool // true if in the middle of receiving data for currentHeader
)
for {
if buf.len() == 0 {
buf.reset()
}
// need to read more if buf doesn't contain the complete frame
// or there's not enough in buf to parse the header
if frameInProgress || buf.len() < frameHeaderSize {
if c.idleTimeout > 0 {
_ = c.net.SetReadDeadline(time.Now().Add(c.idleTimeout))
}
err := buf.readFromOnce(c.net)
if err != nil {
select {
// check if error was due to close in progress
case <-c.done:
return
// if there is a pending connReaderRun function, execute it
case f := <-c.connReaderRun:
f()
continue
// send error to mux and return
default:
c.connErr <- err
return
}
}
}
// read more if buf doesn't contain enough to parse the header
if buf.len() < frameHeaderSize {
continue
}
// during negotiation, check for proto frames
if negotiating && bytes.Equal(buf.bytes()[:4], []byte{'A', 'M', 'Q', 'P'}) {
p, err := parseProtoHeader(buf)
if err != nil {
c.connErr <- err
return
}
// negotiation is complete once an AMQP proto frame is received
if p.ProtoID == protoAMQP {
negotiating = false
}
// send proto header
select {
case <-c.done:
return
case c.rxProto <- p:
}
continue
}
// parse the header if a frame isn't in progress
if !frameInProgress {
var err error
currentHeader, err = parseFrameHeader(buf)
if err != nil {
c.connErr <- err
return
}
frameInProgress = true
}
// check size is reasonable
if currentHeader.Size > math.MaxInt32 { // make max size configurable
c.connErr <- errorNew("payload too large")
return
}
bodySize := int64(currentHeader.Size - frameHeaderSize)
// the full frame has been received
if int64(buf.len()) < bodySize {
continue
}
frameInProgress = false
// check if body is empty (keepalive)
if bodySize == 0 {
continue
}
// parse the frame
b, ok := buf.next(bodySize)
if !ok {
c.connErr <- io.EOF
return
}
parsedBody, err := parseFrameBody(&buffer{b: b})
if err != nil {
c.connErr <- err
return
}
// send to mux
select {
case <-c.done:
return
case c.rxFrame <- frame{channel: currentHeader.Channel, body: parsedBody}:
}
}
}
func (c *conn) connWriter() {
defer close(c.txDone)
var (
// keepalives are sent at a rate of 1/2 idle timeout
keepaliveInterval = c.peerIdleTimeout / 2
// 0 disables keepalives
keepalivesEnabled = keepaliveInterval > 0
// set if enable, nil if not; nil channels block forever
keepalive <-chan time.Time
)
if keepalivesEnabled {
ticker := time.NewTicker(keepaliveInterval)
defer ticker.Stop()
keepalive = ticker.C
}
var err error
for {
if err != nil {
c.connErr <- err
return
}
select {
// frame write request
case fr := <-c.txFrame:
err = c.writeFrame(fr)
if err == nil && fr.done != nil {
close(fr.done)
}
// keepalive timer
case <-keepalive:
_, err = c.net.Write(keepaliveFrame)
// It would be slightly more efficient in terms of network
// resources to reset the timer each time a frame is sent.
// However, keepalives are small (8 bytes) and the interval
// is usually on the order of minutes. It does not seem
// worth it to add extra operations in the write path to
// avoid. (To properly reset a timer it needs to be stopped,
// possibly drained, then reset.)
// connection complete
case <-c.done:
// send close
_ = c.writeFrame(frame{
type_: frameTypeAMQP,
body: &performClose{},
})
return
}
}
}
// writeFrame writes a frame to the network, may only be used
// by connWriter after initial negotiation.
func (c *conn) writeFrame(fr frame) error {
if c.connectTimeout != 0 {
_ = c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
// writeFrame into txBuf
c.txBuf.reset()
err := writeFrame(&c.txBuf, fr)
if err != nil {
return err
}
// validate the frame isn't exceeding peer's max frame size
if uint64(c.txBuf.len()) > uint64(c.peerMaxFrameSize) {
return errorErrorf("frame larger than peer's max frame size")
}
// write to network
_, err = c.net.Write(c.txBuf.bytes())
return err
}
// writeProtoHeader writes an AMQP protocol header to the
// network
func (c *conn) writeProtoHeader(pID protoID) error {
if c.connectTimeout != 0 {
_ = c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
_, err := c.net.Write([]byte{'A', 'M', 'Q', 'P', byte(pID), 1, 0, 0})
return err
}
// keepaliveFrame is an AMQP frame with no body, used for keepalives
var keepaliveFrame = []byte{0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00}
// wantWriteFrame is used by sessions and links to send frame to
// connWriter.
func (c *conn) wantWriteFrame(fr frame) error {
select {
case c.txFrame <- fr:
return nil
case <-c.done:
return c.getErr()
}
}
// stateFunc is a state in a state machine.
//
// The state is advanced by returning the next state.
// The state machine concludes when nil is returned.
type stateFunc func() stateFunc
// negotiateProto determines which proto to negotiate next
func (c *conn) negotiateProto() stateFunc {
// in the order each must be negotiated
switch {
case c.tlsNegotiation && !c.tlsComplete:
return c.exchangeProtoHeader(protoTLS)
case c.saslHandlers != nil && !c.saslComplete:
return c.exchangeProtoHeader(protoSASL)
default:
return c.exchangeProtoHeader(protoAMQP)
}
}
type protoID uint8
// protocol IDs received in protoHeaders
const (
protoAMQP protoID = 0x0
protoTLS protoID = 0x2
protoSASL protoID = 0x3
)
// exchangeProtoHeader performs the round trip exchange of protocol
// headers, validation, and returns the protoID specific next state.
func (c *conn) exchangeProtoHeader(pID protoID) stateFunc {
// write the proto header
c.err = c.writeProtoHeader(pID)
if c.err != nil {
return nil
}
// read response header
p, err := c.readProtoHeader()
if err != nil {
c.err = err
return nil
}
if pID != p.ProtoID {
c.err = errorErrorf("unexpected protocol header %#00x, expected %#00x", p.ProtoID, pID)
return nil
}
// go to the proto specific state
switch pID {
case protoAMQP:
return c.openAMQP
case protoTLS:
return c.startTLS
case protoSASL:
return c.negotiateSASL
default:
c.err = errorErrorf("unknown protocol ID %#02x", p.ProtoID)
return nil
}
}
// readProtoHeader reads a protocol header packet from c.rxProto.
func (c *conn) readProtoHeader() (protoHeader, error) {
var deadline <-chan time.Time
if c.connectTimeout != 0 {
deadline = time.After(c.connectTimeout)
}
var p protoHeader
select {
case p = <-c.rxProto:
return p, nil
case err := <-c.connErr:
return p, err
case fr := <-c.rxFrame:
return p, errorErrorf("unexpected frame %#v", fr)
case <-deadline:
return p, ErrTimeout
}
}
// startTLS wraps the conn with TLS and returns to Client.negotiateProto
func (c *conn) startTLS() stateFunc {
c.initTLSConfig()
done := make(chan struct{})
// this function will be executed by connReader
c.connReaderRun <- func() {
_ = c.net.SetReadDeadline(time.Time{}) // clear timeout
// wrap existing net.Conn and perform TLS handshake
tlsConn := tls.Client(c.net, c.tlsConfig)
if c.connectTimeout != 0 {
_ = tlsConn.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
c.err = tlsConn.Handshake()
// swap net.Conn
c.net = tlsConn
c.tlsComplete = true
close(done)
}
// set deadline to interrupt connReader
_ = c.net.SetReadDeadline(time.Time{}.Add(1))
<-done
if c.err != nil {
return nil
}
// go to next protocol
return c.negotiateProto
}
// openAMQP round trips the AMQP open performative
func (c *conn) openAMQP() stateFunc {
// send open frame
c.err = c.writeFrame(frame{
type_: frameTypeAMQP,
body: &performOpen{
ContainerID: randString(40),
Hostname: c.hostname,
MaxFrameSize: c.maxFrameSize,
ChannelMax: c.channelMax,
IdleTimeout: c.idleTimeout,
Properties: c.properties,
},
channel: 0,
})
if c.err != nil {
return nil
}
// get the response
fr, err := c.readFrame()
if err != nil {
c.err = err
return nil
}
o, ok := fr.body.(*performOpen)
if !ok {
c.err = errorErrorf("unexpected frame type %T", fr.body)
return nil
}
// update peer settings
if o.MaxFrameSize > 0 {
c.peerMaxFrameSize = o.MaxFrameSize
}
if o.IdleTimeout > 0 {
// TODO: reject very small idle timeouts
c.peerIdleTimeout = o.IdleTimeout
}
if o.ChannelMax < c.channelMax {
c.channelMax = o.ChannelMax
}
// connection established, exit state machine
return nil
}
// negotiateSASL returns the SASL handler for the first matched
// mechanism specified by the server
func (c *conn) negotiateSASL() stateFunc {
// read mechanisms frame
fr, err := c.readFrame()
if err != nil {
c.err = err
return nil
}
sm, ok := fr.body.(*saslMechanisms)
if !ok {
c.err = errorErrorf("unexpected frame type %T", fr.body)
return nil
}
// return first match in c.saslHandlers based on order received
for _, mech := range sm.Mechanisms {
if state, ok := c.saslHandlers[mech]; ok {
return state
}
}
// no match
c.err = errorErrorf("no supported auth mechanism (%v)", sm.Mechanisms) // TODO: send "auth not supported" frame?
return nil
}
// saslOutcome processes the SASL outcome frame and return Client.negotiateProto
// on success.
//
// SASL handlers return this stateFunc when the mechanism specific negotiation
// has completed.
func (c *conn) saslOutcome() stateFunc {
// read outcome frame
fr, err := c.readFrame()
if err != nil {
c.err = err
return nil
}
so, ok := fr.body.(*saslOutcome)
if !ok {
c.err = errorErrorf("unexpected frame type %T", fr.body)
return nil
}
// check if auth succeeded
if so.Code != codeSASLOK {
c.err = errorErrorf("SASL PLAIN auth failed with code %#00x: %s", so.Code, so.AdditionalData) // implement Stringer for so.Code
return nil
}
// return to c.negotiateProto
c.saslComplete = true
return c.negotiateProto
}
// readFrame is used during connection establishment to read a single frame.
//
// After setup, conn.mux handles incoming frames.
func (c *conn) readFrame() (frame, error) {
var deadline <-chan time.Time
if c.connectTimeout != 0 {
deadline = time.After(c.connectTimeout)
}
var fr frame
select {
case fr = <-c.rxFrame:
return fr, nil
case err := <-c.connErr:
return fr, err
case p := <-c.rxProto:
return fr, errorErrorf("unexpected protocol header %#v", p)
case <-deadline:
return fr, ErrTimeout
}
}