Skip to content

Commit

Permalink
Add proper handling of connection when publish fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeborja committed Jan 19, 2022
1 parent b2f2bf9 commit 8d6463e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions stomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
log "github.com/sirupsen/logrus"
)

func StartStomp(config *Config, queue *ConfirmationQueue) error {
func StartStomp(config *Config, queue *ConfirmationQueue) {

// TODO: Get the username, password, server, topic from the config
stompUser := config.StompUser
Expand All @@ -27,7 +27,15 @@ func StartStomp(config *Config, queue *ConfirmationQueue) error {
}
stompSession.publish(msg)
if err != nil {
return err
reconnectLoop:
for {
reconnectError := stompSession.handleReconnect()
if reconnectError == nil {
break reconnectLoop
} else {
log.Errorln("Failed to reconnect:", reconnectError.Error())
}
}
}
}

Expand All @@ -49,23 +57,24 @@ func NewStompConnection(username string, password string,
stompUrl: stompUrl,
topic: topic,
}
go session.handleReconnect()
session.handleReconnect()
return &session
}

// handleReconnect reconnects to the stomp server
func (session *StompSession) handleReconnect() {
func (session *StompSession) handleReconnect() error {
// Close the current session
session.conn.Disconnect()

// Start a new session
conn, err := stomp.Dial("tcp", session.stompUrl.String(),
stomp.ConnOpt.Login(session.username, session.password))
if err != nil {
log.Errorln("Failed to connect. Retrying:", err.Error)
return err
}

session.conn = conn
return nil
}

// publish will send the message to the stomp message bus
Expand Down

0 comments on commit 8d6463e

Please sign in to comment.