connection.go 20 KB

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