Skip to content

Commit

Permalink
Extract constants into its own file and add delay for reconnection in…
Browse files Browse the repository at this point in the history
… stomp
  • Loading branch information
kazeborja committed Jan 19, 2022
1 parent 3acda19 commit 2fe9e46
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
11 changes: 0 additions & 11 deletions amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ type Session struct {
isReady bool
}

const (
// When reconnecting to the server after connection failure
reconnectDelay = 5 * time.Second

// When setting up the channel after a channel exception
reInitDelay = 2 * time.Second

// When resending messages the server didn't confirm
resendDelay = 5 * time.Second
)

var (
errNotConnected = errors.New("not connected to a server")
errAlreadyClosed = errors.New("already closed: not connected to the server")
Expand Down
16 changes: 16 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"time"
)

const (
// When reconnecting to the server after connection failure
reconnectDelay = 5 * time.Second

// When setting up the channel after a channel exception
reInitDelay = 2 * time.Second

// When resending messages the server didn't confirm
resendDelay = 5 * time.Second
)
5 changes: 4 additions & 1 deletion stomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/url"
"time"

stomp "github.com/go-stomp/stomp/v3"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -78,13 +79,15 @@ sendMessageLoop:
msg)

if err != nil {
log.Errorln("Failed to publish message:", err)
reconnectLoop:
for {
reconnectError := session.handleReconnect()
if reconnectError == nil {
break reconnectLoop
} else {
log.Errorln("Failed to reconnect:", reconnectError.Error())
log.Errorln("Failed to reconnect, retrying:", reconnectError.Error())
<-time.After(reconnectDelay)
}
}
} else {
Expand Down

0 comments on commit 2fe9e46

Please sign in to comment.