Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple gateway closes without blocking forever #339

Merged
merged 1 commit into from
Mar 29, 2024
Merged
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
20 changes: 10 additions & 10 deletions gateway/gateway_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type gatewayImpl struct {
closeHandlerFunc CloseHandlerFunc
token string

conn *websocket.Conn
connMu sync.Mutex
heartbeatChan chan struct{}
status Status
conn *websocket.Conn
connMu sync.Mutex
heartbeatCancel context.CancelFunc
status Status

heartbeatInterval time.Duration
lastHeartbeatSent time.Time
Expand Down Expand Up @@ -132,9 +132,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
}

func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message string) {
if g.heartbeatChan != nil {
if g.heartbeatCancel != nil {
g.config.Logger.Debug("closing heartbeat goroutines...")
g.heartbeatChan <- struct{}{}
g.heartbeatCancel()
}

g.connMu.Lock()
Expand Down Expand Up @@ -234,16 +234,16 @@ func (g *gatewayImpl) reconnect() {
}

func (g *gatewayImpl) heartbeat() {
if g.heartbeatChan == nil {
g.heartbeatChan = make(chan struct{})
}
ctx, cancel := context.WithCancel(context.Background())
g.heartbeatCancel = cancel

heartbeatTicker := time.NewTicker(g.heartbeatInterval)
defer heartbeatTicker.Stop()
defer g.config.Logger.Debug("exiting heartbeat goroutine")

for {
select {
case <-g.heartbeatChan:
case <-ctx.Done():
return

case <-heartbeatTicker.C:
Expand Down
Loading