Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix set channel in Confirm mode not immediately after creating #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewConsumer(
return nil, errors.New("connection manager can't be nil")
}

chanManager, err := channelmanager.NewChannelManager(conn.connectionManager, options.Logger, conn.connectionManager.ReconnectInterval)
chanManager, err := channelmanager.NewChannelManager(conn.connectionManager, false, options.Logger, conn.connectionManager.ReconnectInterval)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion internal/channelmanager/channel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ type ChannelManager struct {
reconnectionCount uint
reconnectionCountMu *sync.Mutex
dispatcher *dispatcher.Dispatcher
inConfirmMode bool
}

// NewChannelManager creates a new connection manager
func NewChannelManager(connManager *connectionmanager.ConnectionManager, log logger.Logger, reconnectInterval time.Duration) (*ChannelManager, error) {
func NewChannelManager(connManager *connectionmanager.ConnectionManager, confirmMode bool, log logger.Logger, reconnectInterval time.Duration) (*ChannelManager, error) {
ch, err := getNewChannel(connManager)
if err != nil {
return nil, err
Expand All @@ -39,6 +40,7 @@ func NewChannelManager(connManager *connectionmanager.ConnectionManager, log log
reconnectionCount: 0,
reconnectionCountMu: &sync.Mutex{},
dispatcher: dispatcher.NewDispatcher(),
inConfirmMode: confirmMode,
}
go chanManager.startNotifyCancelOrClosed()
return &chanManager, nil
Expand Down Expand Up @@ -119,6 +121,12 @@ func (chanManager *ChannelManager) reconnect() error {
if err != nil {
return err
}
// chaneel creating and setting confirm mode should be in the same mutex Lock interval
if chanManager.inConfirmMode {
if err = newChannel.Confirm(false); err != nil {
return err
}
}

if err = chanManager.channel.Close(); err != nil {
chanManager.logger.Warnf("error closing channel while reconnecting: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewPublisher(conn *Conn, optionFuncs ...func(*PublisherOptions)) (*Publishe
return nil, errors.New("connection manager can't be nil")
}

chanManager, err := channelmanager.NewChannelManager(conn.connectionManager, options.Logger, conn.connectionManager.ReconnectInterval)
chanManager, err := channelmanager.NewChannelManager(conn.connectionManager, options.ConfirmMode, options.Logger, conn.connectionManager.ReconnectInterval)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -272,6 +272,7 @@ func (publisher *Publisher) PublishWithDeferredConfirmWithContext(
}
deferredConfirmations = append(deferredConfirmations, conf)
}

return deferredConfirmations, nil
}

Expand Down