fix issue with writing getting caught up under very rare conditions
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandlePing(ctx context.Context, env *runtime.Env, envelope map[string]interface{}) error {
|
func HandlePing(ctx context.Context, env *runtime.Env, envelope map[string]interface{}) error {
|
||||||
env.SetLastPong(time.Now().UnixMilli())
|
|
||||||
|
|
||||||
ts := extractTimestamp(envelope["ts"])
|
ts := extractTimestamp(envelope["ts"])
|
||||||
if ts == 0 {
|
if ts == 0 {
|
||||||
|
|||||||
@@ -589,6 +589,7 @@ func runSession(ctx context.Context, cancel context.CancelFunc, conn *websocket.
|
|||||||
last := env.LastPong()
|
last := env.LastPong()
|
||||||
if !last.IsZero() && time.Since(last) > grace {
|
if !last.IsZero() && time.Since(last) > grace {
|
||||||
log.Printf("ping: no pong for %s, forcing reconnect", time.Since(last))
|
log.Printf("ping: no pong for %s, forcing reconnect", time.Since(last))
|
||||||
|
conn.Close(websocket.StatusGoingAway, "pong timeout")
|
||||||
cancel()
|
cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,33 @@ package wire
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"time"
|
||||||
|
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const writeTimeout = 30 * time.Second
|
||||||
|
|
||||||
type SafeWriter struct {
|
type SafeWriter struct {
|
||||||
mu sync.Mutex
|
sem chan struct{}
|
||||||
w Writer
|
w Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSafeWriter(w Writer) *SafeWriter {
|
func NewSafeWriter(w Writer) *SafeWriter {
|
||||||
return &SafeWriter{w: w}
|
sem := make(chan struct{}, 1)
|
||||||
|
sem <- struct{}{}
|
||||||
|
return &SafeWriter{sem: sem, w: w}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SafeWriter) Write(ctx context.Context, messageType websocket.MessageType, p []byte) error {
|
func (s *SafeWriter) Write(ctx context.Context, messageType websocket.MessageType, p []byte) error {
|
||||||
s.mu.Lock()
|
select {
|
||||||
defer s.mu.Unlock()
|
case <-s.sem:
|
||||||
return s.w.Write(ctx, messageType, p)
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
defer func() { s.sem <- struct{}{} }()
|
||||||
|
|
||||||
|
writeCtx, cancel := context.WithTimeout(ctx, writeTimeout)
|
||||||
|
defer cancel()
|
||||||
|
return s.w.Write(writeCtx, messageType, p)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user