connection.go 20 KB

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