Skip to content

feat: auto-reconnect backoff for chainsync input #236

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

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
55 changes: 34 additions & 21 deletions input/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,33 @@ import (
const (
// Size of cache for recent chainsync cursors
cursorCacheSize = 20

maxAutoReconnectDelay = 60 * time.Second
)

type ChainSync struct {
oConn *ouroboros.Connection
logger plugin.Logger
network string
networkMagic uint32
address string
socketPath string
ntcTcp bool
bulkMode bool
intersectTip bool
intersectPoints []ocommon.Point
includeCbor bool
autoReconnect bool
statusUpdateFunc StatusUpdateFunc
status *ChainSyncStatus
errorChan chan error
eventChan chan event.Event
bulkRangeStart ocommon.Point
bulkRangeEnd ocommon.Point
cursorCache []ocommon.Point
dialAddress string
dialFamily string
oConn *ouroboros.Connection
logger plugin.Logger
network string
networkMagic uint32
address string
socketPath string
ntcTcp bool
bulkMode bool
intersectTip bool
intersectPoints []ocommon.Point
includeCbor bool
autoReconnect bool
autoReconnectDelay time.Duration
statusUpdateFunc StatusUpdateFunc
status *ChainSyncStatus
errorChan chan error
eventChan chan event.Event
bulkRangeStart ocommon.Point
bulkRangeEnd ocommon.Point
cursorCache []ocommon.Point
dialAddress string
dialFamily string
}

type ChainSyncStatus struct {
Expand Down Expand Up @@ -220,10 +223,20 @@ func (c *ChainSync) setupConnection() error {
err, ok := <-c.oConn.ErrorChan()
if ok {
if c.autoReconnect {
c.autoReconnectDelay = 0
if c.logger != nil {
c.logger.Infof("reconnecting to %s due to error: %s", c.dialAddress, err)
}
for {
if c.autoReconnectDelay > 0 {
c.logger.Infof("waiting %s to reconnect", c.autoReconnectDelay)
time.Sleep(c.autoReconnectDelay)
// Double current reconnect delay up to maximum
c.autoReconnectDelay = min(c.autoReconnectDelay*2, maxAutoReconnectDelay)
} else {
// Set initial reconnect delay
c.autoReconnectDelay = 1 * time.Second
}
// Shutdown current connection
if err := c.oConn.Close(); err != nil {
if c.logger != nil {
Expand Down