Amqp: Exception "channel/connection is not open" when amqp.Publishing has UserId

Created on 11 Mar 2016  路  9Comments  路  Source: streadway/amqp

When Publishing to a channel and the amqp.Publishing has UserId attached, after a while I get the exception "channel/connection is not open". When I remove the the UserId from amqp.Publishing everything is ok. Any ideas what is happening and how to fix it?

This is the code I'm running.

package main

import (
    "github.com/streadway/amqp"
    "log"
)

const exchangeKey = "test-exchange"
const exchangeType = "topic"

func main() {
    //set up queue handling
    conn, err := amqp.Dial("amqp://guest:guest@localhost")
    if err != nil {
        panic(err)
    }

    messageChannel, err := conn.Channel()
    if err != nil {
        panic(err)
    }

    if err := messageChannel.ExchangeDeclare(
        exchangeKey,  // name
        exchangeType, // type
        true,         // durable
        false,        // auto-deleted
        false,        // internal
        false,        // no-wait
        nil,          // arguments
    ); err != nil {
        panic(err)
    }

    defer messageChannel.Close()
    defer conn.Close()
    var count int64

    publish := func(body []byte, userId string) {
        count += 1
        if err := messageChannel.Publish(
            exchangeKey, // exchange
            "test",      // routing key
            false,       // mandatory
            false,       // immediate
            amqp.Publishing{
                ContentType:  "application/json",
                Body:         body,
                DeliveryMode: amqp.Persistent,
                UserId:       userId,
            },
        ); err != nil {
            log.Println("COUNT:", count)
            panic(err)
        }
    }

    for {
        publish([]byte(`{"foo":"bar"}`), "123")
    }

}

Most helpful comment

hey @dmportella - the project isn't abandoned... just sleeping. I haven't reviewed the PRs for a while.

The UserId field comes from the AMQP spec and is passed to rabbit without being touched. There may be an exception being asynchronously returned which would close the channel/connection because RabbitMQ may not support that part of the spec.

What do the server logs show for publishings that use a UserId?

All 9 comments

Hi @janika

I was just looking at this and i dont think this project is supported anymore. @streadway could you confirm?

I will try your code and see if i get the same issue.

Hi @dmportella
It would be great if you could confirm this bug or propose any solutions.
But yeah, I started looking around after posting this issue and this project seems to be a bit abandoned :(

@janika
I figured out what is the problem. The User Id must be a user that exists in rabbitmq, you rabbit instance probably doesnt have the user 123 so it doesnt work. On a default instance of rabbit if you use guest it does work. Might be a amqp protocol thing that if you specify a user id it wont let you connect if that user doesnt exist.

I am half tempted to fork this project and work on it.

hey @dmportella - the project isn't abandoned... just sleeping. I haven't reviewed the PRs for a while.

The UserId field comes from the AMQP spec and is passed to rabbit without being touched. There may be an exception being asynchronously returned which would close the channel/connection because RabbitMQ may not support that part of the spec.

What do the server logs show for publishings that use a UserId?

RabbitMQ does support the user id field and even performs validation on it. See server logs for details.

You are right. Thanks for the clarification. It makes totally sense that "123" doesn't work, because the user doesn't exist in RabbitMQ. I was trying to use the field with the wrong purpose. It would help, if the error message would be a bit clearer, but my mistake for not getting more into the documentation.

Thanks for your time and effort @dmportella, @streadway, @michaelklishin!

@janika to clarify: it does not work because the effective user (the one your connection authenticated as) is not "123". You can label the user with the "impersonator" tag to turn off validation. See the docs above.

Thanks, @michaelklishin it's good info. Although I think I will find another solution. I was trying to use UserId field to pass on the actual user's id whose request to the server initiated the action I'm passing on in RabbitMQ. I think adding it to the message body makes more sense now.

cool @streadway

glad its working for you now @janika

Was this page helpful?
0 / 5 - 0 ratings

Related issues

auyer picture auyer  路  4Comments

Unlym picture Unlym  路  10Comments

pierrre picture pierrre  路  22Comments

kpurdon picture kpurdon  路  22Comments

pnuz3n picture pnuz3n  路  17Comments