Revert "ws: Add LastAcknowledgedBeat"

This reverts commit 660d9893e1.

The commit introduces regular incorrect reconnections. It will be
tracked in another branch and re-merged later once fixed.
This commit is contained in:
diamondburned 2022-04-12 10:55:42 -07:00
parent bd0369136f
commit c71f48c163
No known key found for this signature in database
GPG Key ID: D78C4471CE776659
3 changed files with 3 additions and 37 deletions

View File

@ -417,14 +417,6 @@ func (g *gatewayImpl) SendHeartbeat(ctx context.Context) {
} }
} }
// LastAcknowledgedBeat returns the last acknowledged beat.
func (g *gatewayImpl) LastAcknowledgedBeat() time.Time {
g.beatMutex.Lock()
defer g.beatMutex.Unlock()
return g.echoBeat
}
// Close closes the state. // Close closes the state.
func (g *gatewayImpl) Close() error { func (g *gatewayImpl) Close() error {
g.retryTimer.Stop() g.retryTimer.Stop()

View File

@ -11,10 +11,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// ErrHeartbeatTimeout is returned if the server fails to acknowledge our heart
// beat in time.
var ErrHeartbeatTimeout = errors.New("server timed out replying to heartbeat")
// ConnectionError is given to the user if the gateway fails to connect to the // ConnectionError is given to the user if the gateway fails to connect to the
// gateway for any reason, including during an initial connection or a // gateway for any reason, including during an initial connection or a
// reconnection. To check for this error, use the errors.As function. // reconnection. To check for this error, use the errors.As function.
@ -101,7 +97,6 @@ type Gateway struct {
reconnect chan struct{} reconnect chan struct{}
heart lazytime.Ticker heart lazytime.Ticker
heartRate time.Duration
srcOp <-chan Op // from WS srcOp <-chan Op // from WS
outer outerState outer outerState
lastError error lastError error
@ -130,9 +125,6 @@ type Handler interface {
// SendHeartbeat is called by the gateway event loop everytime a heartbeat // SendHeartbeat is called by the gateway event loop everytime a heartbeat
// needs to be sent over. // needs to be sent over.
SendHeartbeat(context.Context) SendHeartbeat(context.Context)
// LastAcknowledgedBeat returns the last time that the server acknowledged
// our heart beat.
LastAcknowledgedBeat() time.Time
// Close closes the handler. // Close closes the handler.
Close() error Close() error
} }
@ -271,7 +263,6 @@ func (g *Gateway) QueueReconnect() {
// ResetHeartbeat resets the heartbeat to be the given duration. // ResetHeartbeat resets the heartbeat to be the given duration.
func (g *Gateway) ResetHeartbeat(d time.Duration) { func (g *Gateway) ResetHeartbeat(d time.Duration) {
g.heart.Reset(d) g.heart.Reset(d)
g.heartRate = d
} }
// SendError sends the given error wrapped in a BackgroundErrorEvent into the // SendError sends the given error wrapped in a BackgroundErrorEvent into the
@ -336,12 +327,7 @@ func (g *Gateway) spin(ctx context.Context, h Handler) {
g.lastError = nil g.lastError = nil
case <-g.heart.C: case <-g.heart.C:
if h.LastAcknowledgedBeat().Add(2 * g.heartRate).Before(time.Now()) { h.SendHeartbeat(ctx)
g.SendError(ErrHeartbeatTimeout)
g.QueueReconnect()
} else {
h.SendHeartbeat(ctx)
}
case <-g.reconnect: case <-g.reconnect:
// Close the previous connection if it's not already. Ignore the // Close the previous connection if it's not already. Ignore the

View File

@ -45,9 +45,8 @@ type Gateway struct {
gateway *ws.Gateway gateway *ws.Gateway
state State // constant state State // constant
mutex sync.RWMutex mutex sync.RWMutex
beatAck time.Time ready *ReadyEvent
ready *ReadyEvent
} }
// DefaultGatewayOpts contains the default options to be used for connecting to // DefaultGatewayOpts contains the default options to be used for connecting to
@ -188,10 +187,6 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
g.gateway.QueueReconnect() g.gateway.QueueReconnect()
} }
} }
case *HeartbeatAckEvent:
g.mutex.Lock()
g.beatAck = time.Now()
g.mutex.Unlock()
case *ReadyEvent: case *ReadyEvent:
g.mutex.Lock() g.mutex.Lock()
g.ready = data g.ready = data
@ -209,13 +204,6 @@ func (g *gatewayImpl) SendHeartbeat(ctx context.Context) {
} }
} }
func (g *gatewayImpl) LastAcknowledgedBeat() time.Time {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.beatAck
}
func (g *gatewayImpl) Close() error { func (g *gatewayImpl) Close() error {
return nil return nil
} }