+
Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func newClient(endpoint string, isProtobuf bool, config Config) *Client {
subs: make(map[string]*Subscription),
serverSubs: make(map[string]*serverSub),
requests: make(map[uint32]request),
reconnectStrategy: defaultBackoffReconnect,
reconnectStrategy: newBackoffReconnect(config.MinReconnectDelay, config.MaxReconnectDelay),
delayPing: make(chan struct{}, 32),
events: newEventHub(),
connectFutures: make(map[uint64]connectFuture),
Expand Down
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ type Config struct {
// MaxServerPingDelay used to set maximum delay of ping from server.
// Zero value means 10 * time.Second.
MaxServerPingDelay time.Duration
// MinReconnectDelay is the minimum delay between reconnection attempts.
// This delay is jittered.
// Zero value means 200 * time.Millisecond.
MinReconnectDelay time.Duration
// MaxReconnectDelay is the maximum delay between reconnection attempts.
// Zero value means 20 * time.Second.
MaxReconnectDelay time.Duration
// TLSConfig specifies the TLS configuration to use with tls.Client.
// If nil, the default configuration is used.
TLSConfig *tls.Config
Expand Down
24 changes: 17 additions & 7 deletions reconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ type backoffReconnect struct {
MaxDelay time.Duration
}

var defaultBackoffReconnect = &backoffReconnect{
MinDelay: 200 * time.Millisecond,
MaxDelay: 20 * time.Second,
Factor: 2,
Jitter: true,
}

func (r *backoffReconnect) timeBeforeNextAttempt(attempt int) time.Duration {
b := &backoff.Backoff{
Min: r.MinDelay,
Expand All @@ -37,3 +30,20 @@ func (r *backoffReconnect) timeBeforeNextAttempt(attempt int) time.Duration {
}
return b.ForAttempt(float64(attempt))
}

// newBackoffReconnect creates a new backoff reconnect strategy with custom min and max delays.
// If minDelay or maxDelay is zero, it uses the default values.
func newBackoffReconnect(minDelay, maxDelay time.Duration) reconnectStrategy {
if minDelay == 0 {
minDelay = 200 * time.Millisecond
}
if maxDelay == 0 {
maxDelay = 20 * time.Second
}
return &backoffReconnect{
MinDelay: minDelay,
MaxDelay: maxDelay,
Factor: 2,
Jitter: true,
}
}
16 changes: 15 additions & 1 deletion subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,30 @@ type SubscriptionConfig struct {
JoinLeave bool
// Delta allows to specify delta type for the subscription. By default, no delta is used.
Delta DeltaType
// MinResubscribeDelay is the minimum delay between resubscription attempts.
// This delay is jittered.
// Zero value means 200 * time.Millisecond.
MinResubscribeDelay time.Duration
// MaxResubscribeDelay is the maximum delay between resubscription attempts.
// Zero value means 20 * time.Second.
MaxResubscribeDelay time.Duration
}

func newSubscription(c *Client, channel string, config ...SubscriptionConfig) *Subscription {
var resubscribeStrategy reconnectStrategy
var minResubscribeDelay, maxResubscribeDelay time.Duration
if len(config) == 1 {
minResubscribeDelay = config[0].MinResubscribeDelay
maxResubscribeDelay = config[0].MaxResubscribeDelay
}
resubscribeStrategy = newBackoffReconnect(minResubscribeDelay, maxResubscribeDelay)
s := &Subscription{
Channel: channel,
centrifuge: c,
state: SubStateUnsubscribed,
events: newSubscriptionEventHub(),
subFutures: make(map[uint64]subFuture),
resubscribeStrategy: defaultBackoffReconnect,
resubscribeStrategy: resubscribeStrategy,
}
if len(config) == 1 {
cfg := config[0]
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载