Skip to content

Commit b3bc2cc

Browse files
committed
feat(ws): add WithReconnectCount client option
Add a new functional option to configure the maximum number of automatic reconnection attempts for the WebSocket client. This provides users with control over reconnection behavior, allowing them to set a limit or use the default of unlimited retries (-1).
1 parent c065273 commit b3bc2cc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

ws/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ func WithAutoReconnect(b bool) ClientOption {
7575
}
7676
}
7777

78+
func WithReconnectCount(n int) ClientOption {
79+
return func(cli *Client) {
80+
cli.reconnectCount = n
81+
}
82+
}
83+
7884
func WithDomain(domain string) ClientOption {
7985
return func(cli *Client) {
8086
cli.domain = domain

ws/client_option_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ws
2+
3+
import "testing"
4+
5+
func TestWithReconnectCount_SetsValue(t *testing.T) {
6+
cli := NewClient("appID", "appSecret", WithReconnectCount(3))
7+
if cli.reconnectCount != 3 {
8+
t.Fatalf("expected reconnectCount=3, got %d", cli.reconnectCount)
9+
}
10+
}
11+
12+
func TestNewClient_DefaultReconnectCount(t *testing.T) {
13+
cli := NewClient("appID", "appSecret")
14+
if cli.reconnectCount != -1 {
15+
t.Fatalf("expected default reconnectCount=-1, got %d", cli.reconnectCount)
16+
}
17+
}

0 commit comments

Comments
 (0)