Example #1
0
func (me *AmqpConnection) Channel() (ch *AmqpChannel, err error) {
	ok := true

	select {
	case ch, ok = <-me.channels:
		break
	default:
		// with this,
		// we can skip checking of mutex.
		if me.noMore {
			ch, ok = <-me.channels
			break
		}

		me.cLock.Lock()
		defer me.cLock.Unlock()

		if me.cntChannels < me.maxChannel && !me.noMore {
			var c *amqp.Channel
			c, err = me.conn.Channel()

			// enable publisher confirm
			err = c.Confirm(false)
			if err != nil {
				break
			}

			ch = &AmqpChannel{
				Channel: c,
				Confirm: c.NotifyPublish(make(chan amqp.Confirmation, 1)),
				Cancel:  c.NotifyCancel(make(chan string, 1)),
			}
			me.cntChannels++

		} else {
			me.noMore = true
			ch, ok = <-me.channels
			break
		}
	}

	if !ok {
		err = errors.New("channel pool is closed")
	}

	return
}