@@ -54,6 +54,8 @@ type ChainSync struct {
54
54
bulkRangeStart ocommon.Point
55
55
bulkRangeEnd ocommon.Point
56
56
cursorCache []ocommon.Point
57
+ dialAddress string
58
+ dialFamily string
57
59
}
58
60
59
61
type ChainSyncStatus struct {
@@ -152,7 +154,6 @@ func (c *ChainSync) OutputChan() <-chan event.Event {
152
154
func (c * ChainSync ) setupConnection () error {
153
155
// Determine connection parameters
154
156
var useNtn bool
155
- var dialFamily , dialAddress string
156
157
// Lookup network by name, if provided
157
158
if c .network != "" {
158
159
network := ouroboros .NetworkByName (c .network )
@@ -162,8 +163,8 @@ func (c *ChainSync) setupConnection() error {
162
163
c .networkMagic = network .NetworkMagic
163
164
// If network has well-known public root address/port, use those as our dial default
164
165
if network .PublicRootAddress != "" && network .PublicRootPort > 0 {
165
- dialFamily = "tcp"
166
- dialAddress = fmt .Sprintf (
166
+ c . dialFamily = "tcp"
167
+ c . dialAddress = fmt .Sprintf (
167
168
"%s:%d" ,
168
169
network .PublicRootAddress ,
169
170
network .PublicRootPort ,
@@ -173,18 +174,18 @@ func (c *ChainSync) setupConnection() error {
173
174
}
174
175
// Use user-provided address or socket path, if provided
175
176
if c .address != "" {
176
- dialFamily = "tcp"
177
- dialAddress = c .address
177
+ c . dialFamily = "tcp"
178
+ c . dialAddress = c .address
178
179
if c .ntcTcp {
179
180
useNtn = false
180
181
} else {
181
182
useNtn = true
182
183
}
183
184
} else if c .socketPath != "" {
184
- dialFamily = "unix"
185
- dialAddress = c .socketPath
185
+ c . dialFamily = "unix"
186
+ c . dialAddress = c .socketPath
186
187
useNtn = false
187
- } else if dialFamily == "" || dialAddress == "" {
188
+ } else if c . dialFamily == "" || c . dialAddress == "" {
188
189
return fmt .Errorf ("you must specify a host/port, UNIX socket path, or well-known network name" )
189
190
}
190
191
// Create connection
@@ -208,31 +209,37 @@ func (c *ChainSync) setupConnection() error {
208
209
if err != nil {
209
210
return err
210
211
}
211
- if err := c .oConn .Dial (dialFamily , dialAddress ); err != nil {
212
+ if err := c .oConn .Dial (c . dialFamily , c . dialAddress ); err != nil {
212
213
return err
213
214
}
214
215
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 )
216
217
}
217
218
// Start async error handler
218
219
go func () {
219
220
err , ok := <- c .oConn .ErrorChan ()
220
221
if ok {
221
222
if c .autoReconnect {
222
223
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 )
224
225
}
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
236
243
}
237
244
} else {
238
245
// Pass error through our own error channel
0 commit comments