Skip to content

Commit cda0ca8

Browse files
authored
Merge pull request #157 from blinklabs-io/fix/chainsync-autoreconnect-handshake
fix: auto-reconnect on handshake failure
2 parents 7fffc0f + e7b9401 commit cda0ca8

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

input/chainsync/chainsync.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type ChainSync struct {
5454
bulkRangeStart ocommon.Point
5555
bulkRangeEnd ocommon.Point
5656
cursorCache []ocommon.Point
57+
dialAddress string
58+
dialFamily string
5759
}
5860

5961
type ChainSyncStatus struct {
@@ -152,7 +154,6 @@ func (c *ChainSync) OutputChan() <-chan event.Event {
152154
func (c *ChainSync) setupConnection() error {
153155
// Determine connection parameters
154156
var useNtn bool
155-
var dialFamily, dialAddress string
156157
// Lookup network by name, if provided
157158
if c.network != "" {
158159
network := ouroboros.NetworkByName(c.network)
@@ -162,8 +163,8 @@ func (c *ChainSync) setupConnection() error {
162163
c.networkMagic = network.NetworkMagic
163164
// If network has well-known public root address/port, use those as our dial default
164165
if network.PublicRootAddress != "" && network.PublicRootPort > 0 {
165-
dialFamily = "tcp"
166-
dialAddress = fmt.Sprintf(
166+
c.dialFamily = "tcp"
167+
c.dialAddress = fmt.Sprintf(
167168
"%s:%d",
168169
network.PublicRootAddress,
169170
network.PublicRootPort,
@@ -173,18 +174,18 @@ func (c *ChainSync) setupConnection() error {
173174
}
174175
// Use user-provided address or socket path, if provided
175176
if c.address != "" {
176-
dialFamily = "tcp"
177-
dialAddress = c.address
177+
c.dialFamily = "tcp"
178+
c.dialAddress = c.address
178179
if c.ntcTcp {
179180
useNtn = false
180181
} else {
181182
useNtn = true
182183
}
183184
} else if c.socketPath != "" {
184-
dialFamily = "unix"
185-
dialAddress = c.socketPath
185+
c.dialFamily = "unix"
186+
c.dialAddress = c.socketPath
186187
useNtn = false
187-
} else if dialFamily == "" || dialAddress == "" {
188+
} else if c.dialFamily == "" || c.dialAddress == "" {
188189
return fmt.Errorf("you must specify a host/port, UNIX socket path, or well-known network name")
189190
}
190191
// Create connection
@@ -208,31 +209,37 @@ func (c *ChainSync) setupConnection() error {
208209
if err != nil {
209210
return err
210211
}
211-
if err := c.oConn.Dial(dialFamily, dialAddress); err != nil {
212+
if err := c.oConn.Dial(c.dialFamily, c.dialAddress); err != nil {
212213
return err
213214
}
214215
if c.logger != nil {
215-
c.logger.Infof("connected to node at %s", dialAddress)
216+
c.logger.Infof("connected to node at %s", c.dialAddress)
216217
}
217218
// Start async error handler
218219
go func() {
219220
err, ok := <-c.oConn.ErrorChan()
220221
if ok {
221222
if c.autoReconnect {
222223
if c.logger != nil {
223-
c.logger.Infof("reconnecting to %s due to error: %s", dialAddress, err)
224+
c.logger.Infof("reconnecting to %s due to error: %s", c.dialAddress, err)
224225
}
225-
// Shutdown current connection
226-
if err := c.oConn.Close(); err != nil {
227-
c.errorChan <- err
228-
return
229-
}
230-
// Set the intersect points from the cursor cache
231-
c.intersectPoints = c.cursorCache[:]
232-
// Restart the connection
233-
if err := c.Start(); err != nil {
234-
c.errorChan <- err
235-
return
226+
for {
227+
// Shutdown current connection
228+
if err := c.oConn.Close(); err != nil {
229+
if c.logger != nil {
230+
c.logger.Warnf("failed to properly close connection: %s", err)
231+
}
232+
}
233+
// Set the intersect points from the cursor cache
234+
c.intersectPoints = c.cursorCache[:]
235+
// Restart the connection
236+
if err := c.Start(); err != nil {
237+
if c.logger != nil {
238+
c.logger.Infof("reconnecting to %s due to error: %s", c.dialAddress, err)
239+
}
240+
continue
241+
}
242+
break
236243
}
237244
} else {
238245
// Pass error through our own error channel

0 commit comments

Comments
 (0)