@@ -50,8 +50,7 @@ const (
50
50
Version = "1.24.0"
51
51
DefaultURL = "nats://127.0.0.1:4222"
52
52
DefaultPort = 4222
53
- DefaultMaxReconnect = 60
54
- DefaultReconnectWait = 2 * time .Second
53
+ DefaultMaxReconnect = - 1
55
54
DefaultReconnectJitter = 100 * time .Millisecond
56
55
DefaultReconnectJitterTLS = time .Second
57
56
DefaultTimeout = 2 * time .Second
@@ -62,6 +61,10 @@ const (
62
61
RequestChanLen = 8
63
62
DefaultDrainTimeout = 30 * time .Second
64
63
LangString = "go"
64
+
65
+ // DEPRECATED: Client now uses [nats.DefaultReconnectBackoffHandler] to
66
+ // handle default reconnect wait time.
67
+ DefaultReconnectWait = 2 * time .Second
65
68
)
66
69
67
70
const (
@@ -143,17 +146,17 @@ var (
143
146
// GetDefaultOptions returns default configuration options for the client.
144
147
func GetDefaultOptions () Options {
145
148
return Options {
146
- AllowReconnect : true ,
147
- MaxReconnect : DefaultMaxReconnect ,
148
- ReconnectWait : DefaultReconnectWait ,
149
- ReconnectJitter : DefaultReconnectJitter ,
150
- ReconnectJitterTLS : DefaultReconnectJitterTLS ,
151
- Timeout : DefaultTimeout ,
152
- PingInterval : DefaultPingInterval ,
153
- MaxPingsOut : DefaultMaxPingOut ,
154
- SubChanLen : DefaultMaxChanLen ,
155
- ReconnectBufSize : DefaultReconnectBufSize ,
156
- DrainTimeout : DefaultDrainTimeout ,
149
+ AllowReconnect : true ,
150
+ MaxReconnect : DefaultMaxReconnect ,
151
+ ReconnectJitter : DefaultReconnectJitter ,
152
+ ReconnectJitterTLS : DefaultReconnectJitterTLS ,
153
+ Timeout : DefaultTimeout ,
154
+ PingInterval : DefaultPingInterval ,
155
+ MaxPingsOut : DefaultMaxPingOut ,
156
+ SubChanLen : DefaultMaxChanLen ,
157
+ ReconnectBufSize : DefaultReconnectBufSize ,
158
+ DrainTimeout : DefaultDrainTimeout ,
159
+ IgnoreAuthErrorAbort : true ,
157
160
}
158
161
}
159
162
@@ -470,6 +473,7 @@ type Options struct {
470
473
471
474
// IgnoreAuthErrorAbort - if set to true, client opts out of the default connect behavior of aborting
472
475
// subsequent reconnect attempts if server returns the same auth error twice (regardless of reconnect policy).
476
+ // DEPRECATED: This option will be removed in future releases.
473
477
IgnoreAuthErrorAbort bool
474
478
475
479
// SkipHostLookup skips the DNS lookup for the server hostname.
@@ -1260,13 +1264,22 @@ func CustomInboxPrefix(p string) Option {
1260
1264
1261
1265
// IgnoreAuthErrorAbort opts out of the default connect behavior of aborting
1262
1266
// subsequent reconnect attempts if server returns the same auth error twice.
1267
+ // DEPRECATED: This option is now set to 'true' by default, therefore this option will be removed in future releases.
1263
1268
func IgnoreAuthErrorAbort () Option {
1264
1269
return func (o * Options ) error {
1265
1270
o .IgnoreAuthErrorAbort = true
1266
1271
return nil
1267
1272
}
1268
1273
}
1269
1274
1275
+ // AbortOnAuthErrors causes the client to bail out after 2 subsequent auth connection errors.
1276
+ func AbortOnAuthErrors () Option {
1277
+ return func (o * Options ) error {
1278
+ o .IgnoreAuthErrorAbort = false
1279
+ return nil
1280
+ }
1281
+ }
1282
+
1270
1283
// SkipHostLookup is an Option to skip the host lookup when connecting to a server.
1271
1284
func SkipHostLookup () Option {
1272
1285
return func (o * Options ) error {
@@ -2559,6 +2572,28 @@ func (nc *Conn) stopPingTimer() {
2559
2572
}
2560
2573
}
2561
2574
2575
+ // DefaultReconnectBackoffHandler returns a default reconnect exponential backoff interval.
2576
+ // Base reconnect wait is 10ms, with x2 multiplier. Max wait time is 2 minutes.
2577
+ // 10ms, 20ms, 40ms, 80ms...2m
2578
+ // A random jitter is added to the result.
2579
+ func DefaultReconnectBackoffHandler (jitter time.Duration ) ReconnectDelayHandler {
2580
+ return func (attempts int ) time.Duration {
2581
+ // base interval is 10ms
2582
+ backoff := 10 * time .Millisecond
2583
+ for i := 0 ; i < attempts - 1 ; i ++ {
2584
+ backoff *= 2
2585
+ if backoff > 2 * time .Minute {
2586
+ backoff = 2 * time .Minute
2587
+ break
2588
+ }
2589
+ }
2590
+ if jitter > 0 {
2591
+ jitter = time .Duration (rand .Int63n (int64 (jitter )))
2592
+ }
2593
+ return backoff + jitter
2594
+ }
2595
+ }
2596
+
2562
2597
// Try to reconnect using the option parameters.
2563
2598
// This function assumes we are allowed to reconnect.
2564
2599
func (nc * Conn ) doReconnect (err error ) {
@@ -2596,18 +2631,19 @@ func (nc *Conn) doReconnect(err error) {
2596
2631
var wlf int
2597
2632
2598
2633
var jitter time.Duration
2599
- var rw time.Duration
2600
2634
// If a custom reconnect delay handler is set, this takes precedence.
2601
2635
crd := nc .Opts .CustomReconnectDelayCB
2602
- if crd == nil {
2603
- rw = nc . Opts . ReconnectWait
2636
+ rw := nc . Opts . ReconnectWait
2637
+ if crd == nil && rw == 0 {
2604
2638
// TODO: since we sleep only after the whole list has been tried, we can't
2605
2639
// rely on individual *srv to know if it is a TLS or non-TLS url.
2606
2640
// We have to pick which type of jitter to use, for now, we use these hints:
2607
2641
jitter = nc .Opts .ReconnectJitter
2608
2642
if nc .Opts .Secure || nc .Opts .TLSConfig != nil {
2609
2643
jitter = nc .Opts .ReconnectJitterTLS
2610
2644
}
2645
+
2646
+ crd = DefaultReconnectBackoffHandler (jitter )
2611
2647
}
2612
2648
2613
2649
for i := 0 ; len (nc .srvPool ) > 0 ; {
0 commit comments