mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-17 10:00:46 -04:00
ws keepalive (brownout mitigation)
This commit is contained in:
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
+31
-2
@@ -49,7 +49,9 @@ func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, even
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WSClient) Run(ctx context.Context) error {
|
func (c *WSClient) Run(ctx context.Context) error {
|
||||||
conn, _, err := websocket.Dial(ctx, c.cfg.ServerURL, nil)
|
dialCtx, cancel := context.WithTimeout(ctx, dialTimeout)
|
||||||
|
conn, _, err := websocket.Dial(dialCtx, c.cfg.ServerURL, nil)
|
||||||
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.markDisconnected()
|
c.markDisconnected()
|
||||||
return err
|
return err
|
||||||
@@ -65,11 +67,16 @@ func (c *WSClient) Run(ctx context.Context) error {
|
|||||||
c.log.Printf("sensor stream init failed: %v", err)
|
c.log.Printf("sensor stream init failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 2)
|
||||||
c.startTTSWorker(ctx)
|
c.startTTSWorker(ctx)
|
||||||
go func() {
|
go func() {
|
||||||
errCh <- c.readLoop(ctx, conn)
|
errCh <- c.readLoop(ctx, conn)
|
||||||
}()
|
}()
|
||||||
|
go func() {
|
||||||
|
if err := c.keepalive(ctx, conn); err != nil {
|
||||||
|
errCh <- err
|
||||||
|
}
|
||||||
|
}()
|
||||||
go c.forwardSensors(ctx, conn)
|
go c.forwardSensors(ctx, conn)
|
||||||
go c.forwardEvents(ctx, conn)
|
go c.forwardEvents(ctx, conn)
|
||||||
|
|
||||||
@@ -352,6 +359,28 @@ func (c *WSClient) ensureSensorStream() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const disconnectSeekDelay = time.Minute
|
const disconnectSeekDelay = time.Minute
|
||||||
|
const dialTimeout = 10 * time.Second
|
||||||
|
const pingInterval = 15 * time.Second
|
||||||
|
const pingTimeout = 5 * time.Second
|
||||||
|
|
||||||
|
func (c *WSClient) keepalive(ctx context.Context, conn *websocket.Conn) error {
|
||||||
|
ticker := time.NewTicker(pingInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-ticker.C:
|
||||||
|
pingCtx, cancel := context.WithTimeout(ctx, pingTimeout)
|
||||||
|
err := conn.Ping(pingCtx)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *WSClient) markConnected() {
|
func (c *WSClient) markConnected() {
|
||||||
c.connMu.Lock()
|
c.connMu.Lock()
|
||||||
|
|||||||
Reference in New Issue
Block a user