wagslane/go-rabbitmq

how to deal with confirm mode?

michale-developer opened this issue · 4 comments

Hello,
i test send data in confirm mode, the sample code see below, then my server panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x11db25c]

goroutine 1 [running]:
github.com/rabbitmq/amqp091-go.(*DeferredConfirmation).Wait(...)
/Users/michaelmacbook/go/pkg/mod/github.com/rabbitmq/amqp091-go@v1.7.0/confirms.go:203
main.testSend()
/Users/michaelmacbook/workspace/panda/srv/test/service/main/main.go:48 +0x4fc
main.main()
/Users/michaelmacbook/workspace/panda/srv/test/service/main/main.go:13 +0x17

i set the confirm mode, but Confirmation is nil, what's wrong?

func MQSend() error {

conn, err := rabbitmq.NewConn("amqp://xxxx:xxxx@127.0.0.1:5672/")
if err != nil {
	return err
}
    defer conn.Close()
args := rabbitmq.Table{
	"x-delayed-type": "direct",
}
pub, err := rabbitmq.NewPublisher(conn,
	rabbitmq.WithPublisherOptionsExchangeKind("x-delayed-message"),
	rabbitmq.WithPublisherOptionsExchangeDurable,
	rabbitmq.WithPublisherOptionsExchangeName("test.delay"),
	rabbitmq.WithPublisherOptionsExchangeArgs(args),
)
defer pub.Close()
if err != nil {
	return err
}

pub.NotifyPublish(func(p rabbitmq.Confirmation) {})
confirms, err := pub.PublishWithDeferredConfirmWithContext(context.Background(), []byte(`{"test":"a"}`), []string{"delay.queue"},
	rabbitmq.WithPublishOptionsContentType("application/json"),
	rabbitmq.WithPublishOptionsPersistentDelivery,
	rabbitmq.WithPublishOptionsExchange("test.delay"),
	rabbitmq.WithPublishOptionsTimestamp(time.Now()),
)
if err != nil {
	return err
}
for _, v := range confirms {
	if !v.Wait() {
		return errors.New("server confirm fail")
	}
}

return nil

}

then i try other way, the code below, the process block forever:

conn, err := rabbitmq.NewConn("amqp://xxx:xxxx@127.0.0.1:5672/")
if err != nil {
return err
}
args := rabbitmq.Table{
"x-delayed-type": "direct",
}
pub, err := rabbitmq.NewPublisher(conn,
rabbitmq.WithPublisherOptionsExchangeKind("x-delayed-message"),
rabbitmq.WithPublisherOptionsExchangeDurable,
rabbitmq.WithPublisherOptionsExchangeName("panda.test.delay"),
rabbitmq.WithPublisherOptionsExchangeArgs(args),
)
defer pub.Close()
if err != nil {
return err
}

confirmRes := make(chan bool, 1)
pub.NotifyPublish(func(p rabbitmq.Confirmation) {
	confirmRes <- p.Ack
	close(confirmRes)
})

err = pub.PublishWithContext(context.Background(), []byte(`{"test":"a"}`), []string{"delay.queue"},
	rabbitmq.WithPublishOptionsContentType("application/json"),
	rabbitmq.WithPublishOptionsPersistentDelivery,
	rabbitmq.WithPublishOptionsExchange("panda.test.delay"),
	rabbitmq.WithPublishOptionsTimestamp(time.Now()),
)
if err != nil {
	return err
}
for k := range confirmRes {
	if !k {
		return errors.New("server confirm fail")
	}
}

i read the code, find pub.NotifyPublish can't ensure execute before pub.PublishWithContext, so block forever ?

I have the same problem as described in the description of this issue. When I step through the code, I get to "github.com/rabbitmq/amqp091-go", channel.go:1439 which has ch.confirming equal to false which is why the DeferredConfirmation we get is false.

It looks like the code on publish.go:334 never runs, and therefore never sets the channel into Confirm Mode. @michale-developer did you ever solve this problem?

@wagslane please could you assist?

I have the same problem as described in the description of this issue. When I step through the code, I get to "github.com/rabbitmq/amqp091-go", channel.go:1439 which has ch.confirming equal to false which is why the DeferredConfirmation we get is false.

It looks like the code on publish.go:334 never runs, and therefore never sets the channel into Confirm Mode. @michale-developer did you ever solve this problem?

@wagslane please could you assist?

I found a workaround. This library ONLY sets the channel into Confirm Mode under two conditions:

  1. If it disconnects and needs to be reconnected (publish.go:117)
  2. If a NotifyPublish handler is used. (publish.go:308)
    (because those are the only two times startPublishHandler is called which invokes publisher.chanManager.ConfirmSafe(false))

So, a workaround is to create a blank NotifyPublish handler even if you don't need one. e.g:

publisher.NotifyPublish(func(p rabbitmq.Confirmation) {
	// DO NOTHING! - THIS IS JUST HERE TO MAKE SURE THE CHANNEL IS PUT INTO CONFIRM MODE
})

Fixed in the latest! You should just need to NotifyPublish OR set the publisher to confirm mode