Skip to content

Commit 94d0321

Browse files
authored
feat: auto-reconnect backoff for chainsync input (#236)
Fixes #126
1 parent ba12ccb commit 94d0321

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

input/chainsync/chainsync.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,33 @@ import (
3232
const (
3333
// Size of cache for recent chainsync cursors
3434
cursorCacheSize = 20
35+
36+
maxAutoReconnectDelay = 60 * time.Second
3537
)
3638

3739
type ChainSync struct {
38-
oConn *ouroboros.Connection
39-
logger plugin.Logger
40-
network string
41-
networkMagic uint32
42-
address string
43-
socketPath string
44-
ntcTcp bool
45-
bulkMode bool
46-
intersectTip bool
47-
intersectPoints []ocommon.Point
48-
includeCbor bool
49-
autoReconnect bool
50-
statusUpdateFunc StatusUpdateFunc
51-
status *ChainSyncStatus
52-
errorChan chan error
53-
eventChan chan event.Event
54-
bulkRangeStart ocommon.Point
55-
bulkRangeEnd ocommon.Point
56-
cursorCache []ocommon.Point
57-
dialAddress string
58-
dialFamily string
40+
oConn *ouroboros.Connection
41+
logger plugin.Logger
42+
network string
43+
networkMagic uint32
44+
address string
45+
socketPath string
46+
ntcTcp bool
47+
bulkMode bool
48+
intersectTip bool
49+
intersectPoints []ocommon.Point
50+
includeCbor bool
51+
autoReconnect bool
52+
autoReconnectDelay time.Duration
53+
statusUpdateFunc StatusUpdateFunc
54+
status *ChainSyncStatus
55+
errorChan chan error
56+
eventChan chan event.Event
57+
bulkRangeStart ocommon.Point
58+
bulkRangeEnd ocommon.Point
59+
cursorCache []ocommon.Point
60+
dialAddress string
61+
dialFamily string
5962
}
6063

6164
type ChainSyncStatus struct {
@@ -220,10 +223,20 @@ func (c *ChainSync) setupConnection() error {
220223
err, ok := <-c.oConn.ErrorChan()
221224
if ok {
222225
if c.autoReconnect {
226+
c.autoReconnectDelay = 0
223227
if c.logger != nil {
224228
c.logger.Infof("reconnecting to %s due to error: %s", c.dialAddress, err)
225229
}
226230
for {
231+
if c.autoReconnectDelay > 0 {
232+
c.logger.Infof("waiting %s to reconnect", c.autoReconnectDelay)
233+
time.Sleep(c.autoReconnectDelay)
234+
// Double current reconnect delay up to maximum
235+
c.autoReconnectDelay = min(c.autoReconnectDelay*2, maxAutoReconnectDelay)
236+
} else {
237+
// Set initial reconnect delay
238+
c.autoReconnectDelay = 1 * time.Second
239+
}
227240
// Shutdown current connection
228241
if err := c.oConn.Close(); err != nil {
229242
if c.logger != nil {

0 commit comments

Comments
 (0)