connection.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // Copyright (c) 2012, Sean Treadway, SoundCloud Ltd.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Source code and contact info at http://github.com/streadway/amqp
  5. package amqp
  6. import (
  7. "bufio"
  8. "crypto/tls"
  9. "io"
  10. "net"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "sync/atomic"
  16. "time"
  17. )
  18. const (
  19. maxChannelMax = (2 << 15) - 1
  20. defaultHeartbeat = 10 * time.Second
  21. defaultConnectionTimeout = 30 * time.Second
  22. defaultProduct = "https://github.com/streadway/amqp"
  23. defaultVersion = "β"
  24. // Safer default that makes channel leaks a lot easier to spot
  25. // before they create operational headaches. See https://github.com/rabbitmq/rabbitmq-server/issues/1593.
  26. defaultChannelMax = (2 << 10) - 1
  27. defaultLocale = "en_US"
  28. )
  29. // Config is used in DialConfig and Open to specify the desired tuning
  30. // parameters used during a connection open handshake. The negotiated tuning
  31. // will be stored in the returned connection's Config field.
  32. type Config struct {
  33. // The SASL mechanisms to try in the client request, and the successful
  34. // mechanism used on the Connection object.
  35. // If SASL is nil, PlainAuth from the URL is used.
  36. SASL []Authentication
  37. // Vhost specifies the namespace of permissions, exchanges, queues and
  38. // bindings on the server. Dial sets this to the path parsed from the URL.
  39. Vhost string
  40. ChannelMax int // 0 max channels means 2^16 - 1
  41. FrameSize int // 0 max bytes means unlimited
  42. Heartbeat time.Duration // less than 1s uses the server's interval
  43. // TLSClientConfig specifies the client configuration of the TLS connection
  44. // when establishing a tls transport.
  45. // If the URL uses an amqps scheme, then an empty tls.Config with the
  46. // ServerName from the URL is used.
  47. TLSClientConfig *tls.Config
  48. // Properties is table of properties that the client advertises to the server.
  49. // This is an optional setting - if the application does not set this,
  50. // the underlying library will use a generic set of client properties.
  51. Properties Table
  52. // Connection locale that we expect to always be en_US
  53. // Even though servers must return it as per the AMQP 0-9-1 spec,
  54. // we are not aware of it being used other than to satisfy the spec requirements
  55. Locale string
  56. // Dial returns a net.Conn prepared for a TLS handshake with TSLClientConfig,
  57. // then an AMQP connection handshake.
  58. // If Dial is nil, net.DialTimeout with a 30s connection and 30s deadline is
  59. // used during TLS and AMQP handshaking.
  60. Dial func(network, addr string) (net.Conn, error)
  61. }
  62. // Connection manages the serialization and deserialization of frames from IO
  63. // and dispatches the frames to the appropriate channel. All RPC methods and
  64. // asynchronous Publishing, Delivery, Ack, Nack and Return messages are
  65. // multiplexed on this channel. There must always be active receivers for
  66. // every asynchronous message on this connection.
  67. type Connection struct {
  68. destructor sync.Once // shutdown once
  69. sendM sync.Mutex // conn writer mutex
  70. m sync.Mutex // struct field mutex
  71. conn io.ReadWriteCloser
  72. rpc chan message
  73. writer *writer
  74. sends chan time.Time // timestamps of each frame sent
  75. deadlines chan readDeadliner // heartbeater updates read deadlines
  76. allocator *allocator // id generator valid after openTune
  77. channels map[uint16]*Channel
  78. noNotify bool // true when we will never notify again
  79. closes []chan *Error
  80. blocks []chan Blocking
  81. errors chan *Error
  82. Config Config // The negotiated Config after connection.open
  83. Major int // Server's major version
  84. Minor int // Server's minor version
  85. Properties Table // Server properties
  86. Locales []string // Server locales
  87. closed int32 // Will be 1 if the connection is closed, 0 otherwise. Should only be accessed as atomic
  88. }
  89. type readDeadliner interface {
  90. SetReadDeadline(time.Time) error
  91. }
  92. // DefaultDial establishes a connection when config.Dial is not provided
  93. func DefaultDial(connectionTimeout time.Duration) func(network, addr string) (net.Conn, error) {
  94. return func(network, addr string) (net.Conn, error) {
  95. conn, err := net.DialTimeout(network, addr, connectionTimeout)
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Heartbeating hasn't started yet, don't stall forever on a dead server.
  100. // A deadline is set for TLS and AMQP handshaking. After AMQP is established,
  101. // the deadline is cleared in openComplete.
  102. if err := conn.SetDeadline(time.Now().Add(connectionTimeout)); err != nil {
  103. return nil, err
  104. }
  105. return conn, nil
  106. }
  107. }
  108. // Dial accepts a string in the AMQP URI format and returns a new Connection
  109. // over TCP using PlainAuth. Defaults to a server heartbeat interval of 10
  110. // seconds and sets the handshake deadline to 30 seconds. After handshake,
  111. // deadlines are cleared.
  112. //
  113. // Dial uses the zero value of tls.Config when it encounters an amqps://
  114. // scheme. It is equivalent to calling DialTLS(amqp, nil).
  115. func Dial(url string) (*Connection, error) {
  116. return DialConfig(url, Config{
  117. Heartbeat: defaultHeartbeat,
  118. Locale: defaultLocale,
  119. })
  120. }
  121. // DialTLS accepts a string in the AMQP URI format and returns a new Connection
  122. // over TCP using PlainAuth. Defaults to a server heartbeat interval of 10
  123. // seconds and sets the initial read deadline to 30 seconds.
  124. //
  125. // DialTLS uses the provided tls.Config when encountering an amqps:// scheme.
  126. func DialTLS(url string, amqps *tls.Config) (*Connection, error) {
  127. return DialConfig(url, Config{
  128. Heartbeat: defaultHeartbeat,
  129. TLSClientConfig: amqps,
  130. Locale: defaultLocale,
  131. })
  132. }
  133. // DialConfig accepts a string in the AMQP URI format and a configuration for
  134. // the transport and connection setup, returning a new Connection. Defaults to
  135. // a server heartbeat interval of 10 seconds and sets the initial read deadline
  136. // to 30 seconds.
  137. func DialConfig(url string, config Config) (*Connection, error) {
  138. var err error
  139. var conn net.Conn
  140. uri, err := ParseURI(url)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if config.SASL == nil {
  145. config.SASL = []Authentication{uri.PlainAuth()}
  146. }
  147. if config.Vhost == "" {
  148. config.Vhost = uri.Vhost
  149. }
  150. addr := net.JoinHostPort(uri.Host, strconv.FormatInt(int64(uri.Port), 10))
  151. dialer := config.Dial
  152. if dialer == nil {
  153. dialer = DefaultDial(defaultConnectionTimeout)
  154. }
  155. conn, err = dialer("tcp", addr)
  156. if err != nil {
  157. return nil, err
  158. }
  159. if uri.Scheme == "amqps" {
  160. if config.TLSClientConfig == nil {
  161. config.TLSClientConfig = new(tls.Config)
  162. }
  163. // If ServerName has not been specified in TLSClientConfig,
  164. // set it to the URI host used for this connection.
  165. if config.TLSClientConfig.ServerName == "" {
  166. config.TLSClientConfig.ServerName = uri.Host
  167. }
  168. client := tls.Client(conn, config.TLSClientConfig)
  169. if err := client.Handshake(); err != nil {
  170. conn.Close()
  171. return nil, err
  172. }
  173. conn = client
  174. }
  175. return Open(conn, config)
  176. }
  177. /*
  178. Open accepts an already established connection, or other io.ReadWriteCloser as
  179. a transport. Use this method if you have established a TLS connection or wish
  180. to use your own custom transport.
  181. */
  182. func Open(conn io.ReadWriteCloser, config Config) (*Connection, error) {
  183. c := &Connection{
  184. conn: conn,
  185. writer: &writer{bufio.NewWriter(conn)},
  186. channels: make(map[uint16]*Channel),
  187. rpc: make(chan message),
  188. sends: make(chan time.Time),
  189. errors: make(chan *Error, 1),
  190. deadlines: make(chan readDeadliner, 1),
  191. }
  192. go c.reader(conn)
  193. return c, c.open(config)
  194. }
  195. /*
  196. LocalAddr returns the local TCP peer address, or ":0" (the zero value of net.TCPAddr)
  197. as a fallback default value if the underlying transport does not support LocalAddr().
  198. */
  199. func (c *Connection) LocalAddr() net.Addr {
  200. if conn, ok := c.conn.(interface {
  201. LocalAddr() net.Addr
  202. }); ok {
  203. return conn.LocalAddr()
  204. }
  205. return &net.TCPAddr{}
  206. }
  207. // ConnectionState returns basic TLS details of the underlying transport.
  208. // Returns a zero value when the underlying connection does not implement
  209. // ConnectionState() tls.ConnectionState.
  210. func (c *Connection) ConnectionState() tls.ConnectionState {
  211. if conn, ok := c.conn.(interface {
  212. ConnectionState() tls.ConnectionState
  213. }); ok {
  214. return conn.ConnectionState()
  215. }
  216. return tls.ConnectionState{}
  217. }
  218. /*
  219. NotifyClose registers a listener for close events either initiated by an error
  220. accompanying a connection.close method or by a normal shutdown.
  221. On normal shutdowns, the chan will be closed.
  222. To reconnect after a transport or protocol error, register a listener here and
  223. re-run your setup process.
  224. */
  225. func (c *Connection) NotifyClose(receiver chan *Error) chan *Error {
  226. c.m.Lock()
  227. defer c.m.Unlock()
  228. if c.noNotify {
  229. close(receiver)
  230. } else {
  231. c.closes = append(c.closes, receiver)
  232. }
  233. return receiver
  234. }
  235. /*
  236. NotifyBlocked registers a listener for RabbitMQ specific TCP flow control
  237. method extensions connection.blocked and connection.unblocked. Flow control is
  238. active with a reason when Blocking.Blocked is true. When a Connection is
  239. blocked, all methods will block across all connections until server resources
  240. become free again.
  241. This optional extension is supported by the server when the
  242. "connection.blocked" server capability key is true.
  243. */
  244. func (c *Connection) NotifyBlocked(receiver chan Blocking) chan Blocking {
  245. c.m.Lock()
  246. defer c.m.Unlock()
  247. if c.noNotify {
  248. close(receiver)
  249. } else {
  250. c.blocks = append(c.blocks, receiver)
  251. }
  252. return receiver
  253. }
  254. /*
  255. Close requests and waits for the response to close the AMQP connection.
  256. It's advisable to use this message when publishing to ensure all kernel buffers
  257. have been flushed on the server and client before exiting.
  258. An error indicates that server may not have received this request to close but
  259. the connection should be treated as closed regardless.
  260. After returning from this call, all resources associated with this connection,
  261. including the underlying io, Channels, Notify listeners and Channel consumers
  262. will also be closed.
  263. */
  264. func (c *Connection) Close() error {
  265. if c.IsClosed() {
  266. return ErrClosed
  267. }
  268. defer c.shutdown(nil)
  269. return c.call(
  270. &connectionClose{
  271. ReplyCode: replySuccess,
  272. ReplyText: "kthxbai",
  273. },
  274. &connectionCloseOk{},
  275. )
  276. }
  277. func (c *Connection) closeWith(err *Error) error {
  278. if c.IsClosed() {
  279. return ErrClosed
  280. }
  281. defer c.shutdown(err)
  282. return c.call(
  283. &connectionClose{
  284. ReplyCode: uint16(err.Code),
  285. ReplyText: err.Reason,
  286. },
  287. &connectionCloseOk{},
  288. )
  289. }
  290. // IsClosed returns true if the connection is marked as closed, otherwise false
  291. // is returned.
  292. func (c *Connection) IsClosed() bool {
  293. return (atomic.LoadInt32(&c.closed) == 1)
  294. }
  295. func (c *Connection) send(f frame) error {
  296. if c.IsClosed() {
  297. return ErrClosed
  298. }
  299. c.sendM.Lock()
  300. err := c.writer.WriteFrame(f)
  301. c.sendM.Unlock()
  302. if err != nil {
  303. // shutdown could be re-entrant from signaling notify chans
  304. go c.shutdown(&Error{
  305. Code: FrameError,
  306. Reason: err.Error(),
  307. })
  308. } else {
  309. // Broadcast we sent a frame, reducing heartbeats, only
  310. // if there is something that can receive - like a non-reentrant
  311. // call or if the heartbeater isn't running
  312. select {
  313. case c.sends <- time.Now():
  314. default:
  315. }
  316. }
  317. return err
  318. }
  319. func (c *Connection) shutdown(err *Error) {
  320. atomic.StoreInt32(&c.closed, 1)
  321. c.destructor.Do(func() {
  322. c.m.Lock()
  323. defer c.m.Unlock()
  324. if err != nil {
  325. for _, c := range c.closes {
  326. c <- err
  327. }
  328. }
  329. if err != nil {
  330. c.errors <- err
  331. }
  332. // Shutdown handler goroutine can still receive the result.
  333. close(c.errors)
  334. for _, c := range c.closes {
  335. close(c)
  336. }
  337. for _, c := range c.blocks {
  338. close(c)
  339. }
  340. // Shutdown the channel, but do not use closeChannel() as it calls
  341. // releaseChannel() which requires the connection lock.
  342. //
  343. // Ranging over c.channels and calling releaseChannel() that mutates
  344. // c.channels is racy - see commit 6063341 for an example.
  345. for _, ch := range c.channels {
  346. ch.shutdown(err)
  347. }
  348. c.conn.Close()
  349. c.channels = map[uint16]*Channel{}
  350. c.allocator = newAllocator(1, c.Config.ChannelMax)
  351. c.noNotify = true
  352. })
  353. }
  354. // All methods sent to the connection channel should be synchronous so we
  355. // can handle them directly without a framing component
  356. func (c *Connection) demux(f frame) {
  357. if f.channel() == 0 {
  358. c.dispatch0(f)
  359. } else {
  360. c.dispatchN(f)
  361. }
  362. }
  363. func (c *Connection) dispatch0(f frame) {
  364. switch mf := f.(type) {
  365. case *methodFrame:
  366. switch m := mf.Method.(type) {
  367. case *connectionClose:
  368. // Send immediately as shutdown will close our side of the writer.
  369. c.send(&methodFrame{
  370. ChannelId: 0,
  371. Method: &connectionCloseOk{},
  372. })
  373. c.shutdown(newError(m.ReplyCode, m.ReplyText))
  374. case *connectionBlocked:
  375. for _, c := range c.blocks {
  376. c <- Blocking{Active: true, Reason: m.Reason}
  377. }
  378. case *connectionUnblocked:
  379. for _, c := range c.blocks {
  380. c <- Blocking{Active: false}
  381. }
  382. default:
  383. c.rpc <- m
  384. }
  385. case *heartbeatFrame:
  386. // kthx - all reads reset our deadline. so we can drop this
  387. default:
  388. // lolwat - channel0 only responds to methods and heartbeats
  389. c.closeWith(ErrUnexpectedFrame)
  390. }
  391. }
  392. func (c *Connection) dispatchN(f frame) {
  393. c.m.Lock()
  394. channel := c.channels[f.channel()]
  395. c.m.Unlock()
  396. if channel != nil {
  397. channel.recv(channel, f)
  398. } else {
  399. c.dispatchClosed(f)
  400. }
  401. }
  402. // section 2.3.7: "When a peer decides to close a channel or connection, it
  403. // sends a Close method. The receiving peer MUST respond to a Close with a
  404. // Close-Ok, and then both parties can close their channel or connection. Note
  405. // that if peers ignore Close, deadlock can happen when both peers send Close
  406. // at the same time."
  407. //
  408. // When we don't have a channel, so we must respond with close-ok on a close
  409. // method. This can happen between a channel exception on an asynchronous
  410. // method like basic.publish and a synchronous close with channel.close.
  411. // In that case, we'll get both a channel.close and channel.close-ok in any
  412. // order.
  413. func (c *Connection) dispatchClosed(f frame) {
  414. // Only consider method frames, drop content/header frames
  415. if mf, ok := f.(*methodFrame); ok {
  416. switch mf.Method.(type) {
  417. case *channelClose:
  418. c.send(&methodFrame{
  419. ChannelId: f.channel(),
  420. Method: &channelCloseOk{},
  421. })
  422. case *channelCloseOk:
  423. // we are already closed, so do nothing
  424. default:
  425. // unexpected method on closed channel
  426. c.closeWith(ErrClosed)
  427. }
  428. }
  429. }
  430. // Reads each frame off the IO and hand off to the connection object that
  431. // will demux the streams and dispatch to one of the opened channels or
  432. // handle on channel 0 (the connection channel).
  433. func (c *Connection) reader(r io.Reader) {
  434. buf := bufio.NewReader(r)
  435. frames := &reader{buf}
  436. conn, haveDeadliner := r.(readDeadliner)
  437. for {
  438. frame, err := frames.ReadFrame()
  439. if err != nil {
  440. c.shutdown(&Error{Code: FrameError, Reason: err.Error()})
  441. return
  442. }
  443. c.demux(frame)
  444. if haveDeadliner {
  445. select {
  446. case c.deadlines <- conn:
  447. default:
  448. // On c.Close() c.heartbeater() might exit just before c.deadlines <- conn is called.
  449. // Which results in this goroutine being stuck forever.
  450. }
  451. }
  452. }
  453. }
  454. // Ensures that at least one frame is being sent at the tuned interval with a
  455. // jitter tolerance of 1s
  456. func (c *Connection) heartbeater(interval time.Duration, done chan *Error) {
  457. const maxServerHeartbeatsInFlight = 3
  458. var sendTicks <-chan time.Time
  459. if interval > 0 {
  460. ticker := time.NewTicker(interval)
  461. defer ticker.Stop()
  462. sendTicks = ticker.C
  463. }
  464. lastSent := time.Now()
  465. for {
  466. select {
  467. case at, stillSending := <-c.sends:
  468. // When actively sending, depend on sent frames to reset server timer
  469. if stillSending {
  470. lastSent = at
  471. } else {
  472. return
  473. }
  474. case at := <-sendTicks:
  475. // When idle, fill the space with a heartbeat frame
  476. if at.Sub(lastSent) > interval-time.Second {
  477. if err := c.send(&heartbeatFrame{}); err != nil {
  478. // send heartbeats even after close/closeOk so we
  479. // tick until the connection starts erroring
  480. return
  481. }
  482. }
  483. case conn := <-c.deadlines:
  484. // When reading, reset our side of the deadline, if we've negotiated one with
  485. // a deadline that covers at least 2 server heartbeats
  486. if interval > 0 {
  487. conn.SetReadDeadline(time.Now().Add(maxServerHeartbeatsInFlight * interval))
  488. }
  489. case <-done:
  490. return
  491. }
  492. }
  493. }
  494. // Convenience method to inspect the Connection.Properties["capabilities"]
  495. // Table for server identified capabilities like "basic.ack" or
  496. // "confirm.select".
  497. func (c *Connection) isCapable(featureName string) bool {
  498. capabilities, _ := c.Properties["capabilities"].(Table)
  499. hasFeature, _ := capabilities[featureName].(bool)
  500. return hasFeature
  501. }
  502. // allocateChannel records but does not open a new channel with a unique id.
  503. // This method is the initial part of the channel lifecycle and paired with
  504. // releaseChannel
  505. func (c *Connection) allocateChannel() (*Channel, error) {
  506. c.m.Lock()
  507. defer c.m.Unlock()
  508. if c.IsClosed() {
  509. return nil, ErrClosed
  510. }
  511. id, ok := c.allocator.next()
  512. if !ok {
  513. return nil, ErrChannelMax
  514. }
  515. ch := newChannel(c, uint16(id))
  516. c.channels[uint16(id)] = ch
  517. return ch, nil
  518. }
  519. // releaseChannel removes a channel from the registry as the final part of the
  520. // channel lifecycle
  521. func (c *Connection) releaseChannel(id uint16) {
  522. c.m.Lock()
  523. defer c.m.Unlock()
  524. delete(c.channels, id)
  525. c.allocator.release(int(id))
  526. }
  527. // openChannel allocates and opens a channel, must be paired with closeChannel
  528. func (c *Connection) openChannel() (*Channel, error) {
  529. ch, err := c.allocateChannel()
  530. if err != nil {
  531. return nil, err
  532. }
  533. if err := ch.open(); err != nil {
  534. c.releaseChannel(ch.id)
  535. return nil, err
  536. }
  537. return ch, nil
  538. }
  539. // closeChannel releases and initiates a shutdown of the channel. All channel
  540. // closures should be initiated here for proper channel lifecycle management on
  541. // this connection.
  542. func (c *Connection) closeChannel(ch *Channel, e *Error) {
  543. ch.shutdown(e)
  544. c.releaseChannel(ch.id)
  545. }
  546. /*
  547. Channel opens a unique, concurrent server channel to process the bulk of AMQP
  548. messages. Any error from methods on this receiver will render the receiver
  549. invalid and a new Channel should be opened.
  550. */
  551. func (c *Connection) Channel() (*Channel, error) {
  552. return c.openChannel()
  553. }
  554. func (c *Connection) call(req message, res ...message) error {
  555. // Special case for when the protocol header frame is sent insted of a
  556. // request method
  557. if req != nil {
  558. if err := c.send(&methodFrame{ChannelId: 0, Method: req}); err != nil {
  559. return err
  560. }
  561. }
  562. select {
  563. case err, ok := <-c.errors:
  564. if !ok {
  565. return ErrClosed
  566. }
  567. return err
  568. case msg := <-c.rpc:
  569. // Try to match one of the result types
  570. for _, try := range res {
  571. if reflect.TypeOf(msg) == reflect.TypeOf(try) {
  572. // *res = *msg
  573. vres := reflect.ValueOf(try).Elem()
  574. vmsg := reflect.ValueOf(msg).Elem()
  575. vres.Set(vmsg)
  576. return nil
  577. }
  578. }
  579. return ErrCommandInvalid
  580. }
  581. // unreachable
  582. }
  583. // Connection = open-Connection *use-Connection close-Connection
  584. // open-Connection = C:protocol-header
  585. // S:START C:START-OK
  586. // *challenge
  587. // S:TUNE C:TUNE-OK
  588. // C:OPEN S:OPEN-OK
  589. // challenge = S:SECURE C:SECURE-OK
  590. // use-Connection = *channel
  591. // close-Connection = C:CLOSE S:CLOSE-OK
  592. // / S:CLOSE C:CLOSE-OK
  593. func (c *Connection) open(config Config) error {
  594. if err := c.send(&protocolHeader{}); err != nil {
  595. return err
  596. }
  597. return c.openStart(config)
  598. }
  599. func (c *Connection) openStart(config Config) error {
  600. start := &connectionStart{}
  601. if err := c.call(nil, start); err != nil {
  602. return err
  603. }
  604. c.Major = int(start.VersionMajor)
  605. c.Minor = int(start.VersionMinor)
  606. c.Properties = Table(start.ServerProperties)
  607. c.Locales = strings.Split(start.Locales, " ")
  608. // eventually support challenge/response here by also responding to
  609. // connectionSecure.
  610. auth, ok := pickSASLMechanism(config.SASL, strings.Split(start.Mechanisms, " "))
  611. if !ok {
  612. return ErrSASL
  613. }
  614. // Save this mechanism off as the one we chose
  615. c.Config.SASL = []Authentication{auth}
  616. // Set the connection locale to client locale
  617. c.Config.Locale = config.Locale
  618. return c.openTune(config, auth)
  619. }
  620. func (c *Connection) openTune(config Config, auth Authentication) error {
  621. if len(config.Properties) == 0 {
  622. config.Properties = Table{
  623. "product": defaultProduct,
  624. "version": defaultVersion,
  625. }
  626. }
  627. config.Properties["capabilities"] = Table{
  628. "connection.blocked": true,
  629. "consumer_cancel_notify": true,
  630. }
  631. ok := &connectionStartOk{
  632. ClientProperties: config.Properties,
  633. Mechanism: auth.Mechanism(),
  634. Response: auth.Response(),
  635. Locale: config.Locale,
  636. }
  637. tune := &connectionTune{}
  638. if err := c.call(ok, tune); err != nil {
  639. // per spec, a connection can only be closed when it has been opened
  640. // so at this point, we know it's an auth error, but the socket
  641. // was closed instead. Return a meaningful error.
  642. return ErrCredentials
  643. }
  644. // When the server and client both use default 0, then the max channel is
  645. // only limited by uint16.
  646. c.Config.ChannelMax = pick(config.ChannelMax, int(tune.ChannelMax))
  647. if c.Config.ChannelMax == 0 {
  648. c.Config.ChannelMax = defaultChannelMax
  649. }
  650. c.Config.ChannelMax = min(c.Config.ChannelMax, maxChannelMax)
  651. // Frame size includes headers and end byte (len(payload)+8), even if
  652. // this is less than FrameMinSize, use what the server sends because the
  653. // alternative is to stop the handshake here.
  654. c.Config.FrameSize = pick(config.FrameSize, int(tune.FrameMax))
  655. // Save this off for resetDeadline()
  656. c.Config.Heartbeat = time.Second * time.Duration(pick(
  657. int(config.Heartbeat/time.Second),
  658. int(tune.Heartbeat)))
  659. // "The client should start sending heartbeats after receiving a
  660. // Connection.Tune method"
  661. go c.heartbeater(c.Config.Heartbeat, c.NotifyClose(make(chan *Error, 1)))
  662. if err := c.send(&methodFrame{
  663. ChannelId: 0,
  664. Method: &connectionTuneOk{
  665. ChannelMax: uint16(c.Config.ChannelMax),
  666. FrameMax: uint32(c.Config.FrameSize),
  667. Heartbeat: uint16(c.Config.Heartbeat / time.Second),
  668. },
  669. }); err != nil {
  670. return err
  671. }
  672. return c.openVhost(config)
  673. }
  674. func (c *Connection) openVhost(config Config) error {
  675. req := &connectionOpen{VirtualHost: config.Vhost}
  676. res := &connectionOpenOk{}
  677. if err := c.call(req, res); err != nil {
  678. // Cannot be closed yet, but we know it's a vhost problem
  679. return ErrVhost
  680. }
  681. c.Config.Vhost = config.Vhost
  682. return c.openComplete()
  683. }
  684. // openComplete performs any final Connection initialization dependent on the
  685. // connection handshake and clears any state needed for TLS and AMQP handshaking.
  686. func (c *Connection) openComplete() error {
  687. // We clear the deadlines and let the heartbeater reset the read deadline if requested.
  688. // RabbitMQ uses TCP flow control at this point for pushback so Writes can
  689. // intentionally block.
  690. if deadliner, ok := c.conn.(interface {
  691. SetDeadline(time.Time) error
  692. }); ok {
  693. _ = deadliner.SetDeadline(time.Time{})
  694. }
  695. c.allocator = newAllocator(1, c.Config.ChannelMax)
  696. return nil
  697. }
  698. func max(a, b int) int {
  699. if a > b {
  700. return a
  701. }
  702. return b
  703. }
  704. func min(a, b int) int {
  705. if a < b {
  706. return a
  707. }
  708. return b
  709. }
  710. func pick(client, server int) int {
  711. if client == 0 || server == 0 {
  712. return max(client, server)
  713. }
  714. return min(client, server)
  715. }