streadway / amqp

Go client for AMQP 0.9.1
http://godoc.org/github.com/streadway/amqp
BSD 2-Clause "Simplified" License
4.88k stars 621 forks source link

Using buffered instead of non buffered channel. #484

Open tebrizetayi opened 3 years ago

tebrizetayi commented 3 years ago

deliveries is non buffered channel. When you want consume messages in go routine ,it waits for go routine to end. Would not be nice to use buffered channel here?

func (ch *Channel) Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args Table) (<-chan Delivery, error) {
    // When we return from ch.call, there may be a delivery already for the
    // consumer that hasn't been added to the consumer hash yet.  Because of
    // this, we never rely on the server picking a consumer tag for us.

    if err := args.Validate(); err != nil {
        return nil, err
    }

    if consumer == "" {
        consumer = uniqueConsumerTag()
    }

    req := &basicConsume{
        Queue:       queue,
        ConsumerTag: consumer,
        NoLocal:     noLocal,
        NoAck:       autoAck,
        Exclusive:   exclusive,
        NoWait:      noWait,
        Arguments:   args,
    }
    res := &basicConsumeOk{}

    deliveries := make(chan Delivery)

       //Suggestion is 
        deliveries := make(chan Delivery,10)

        ch.consumers.add(consumer, deliveries)

    if err := ch.call(req, res); err != nil {
        ch.consumers.cancel(consumer)
        return nil, err
    }

    return (<-chan Delivery)(deliveries), nil
}
thothothotho commented 3 years ago

In fact ch.consumers.add implement an infinite buffered channel (which could be an issue on its own: #489 ) So adding a buffer here would not change anything.