Skip to content

Commit 7c5d616

Browse files
committed
feat: timeout
1 parent 33a1563 commit 7c5d616

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

pkg/api/layer2.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func (s *Service) layer2WsHandler(w http.ResponseWriter, r *http.Request) {
2727
return
2828
}
2929

30+
headers := struct {
31+
KeepAlive time.Duration `map:"Swarm-Keep-Alive"`
32+
}{}
33+
if response := s.mapStructure(r.Header, &headers); response != nil {
34+
response("invalid header params", logger, w)
35+
return
36+
}
37+
3038
if s.beeMode == DevMode {
3139
logger.Warning("layer2 endpoint is disabled in dev mode")
3240
jsonhttp.BadRequest(w, errUnsupportedDevNodeOperation)
@@ -46,7 +54,12 @@ func (s *Service) layer2WsHandler(w http.ResponseWriter, r *http.Request) {
4654
return
4755
}
4856

49-
pingPeriod := 100 * time.Second //TODO pass it in header
57+
pingPeriod := headers.KeepAlive * time.Second
58+
if pingPeriod == 0 {
59+
pingPeriod = 30 * time.Second
60+
}
61+
62+
logger.Info("pingPeriod", pingPeriod)
5063
ctx, cancel := context.WithCancel(context.Background())
5164
protocolService := s.l2p2p.GetProtocol(ctx, paths.StreamName)
5265
err = s.p2p.AddProtocol(protocolService.Protocol())
@@ -58,7 +71,8 @@ func (s *Service) layer2WsHandler(w http.ResponseWriter, r *http.Request) {
5871

5972
s.wsWg.Add(1)
6073
go func() {
61-
layer2.ListeningWs(ctx, conn, layer2.WsOptions{PingPeriod: pingPeriod}, logger, protocolService)
74+
layer2.ListeningWs(ctx, conn, layer2.WsOptions{PingPeriod: pingPeriod, Cancel: cancel}, logger, protocolService)
75+
_ = conn.Close()
6276
s.wsWg.Done()
6377
cancel()
6478
}()

pkg/layer2/ws.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
type WsOptions struct {
2121
PingPeriod time.Duration
22+
Cancel context.CancelFunc
2223
}
2324

2425
type actionType uint8
@@ -75,12 +76,12 @@ func ListeningWs(ctx context.Context, conn *websocket.Conn, options WsOptions, l
7576
err = conn.SetReadDeadline(time.Now().Add(readDeadline))
7677
if err != nil {
7778
logger.Debug("L2 ws: set write deadline failed", "error", err)
78-
return
79+
break
7980
}
8081
messageType, p, err := conn.ReadMessage()
8182
if err != nil {
8283
logger.Error(err, "L2 ws read message")
83-
return
84+
break
8485
}
8586
respMessageType.Store(uint32(messageType))
8687
if messageType == 1 {
@@ -94,6 +95,7 @@ func ListeningWs(ctx context.Context, conn *websocket.Conn, options WsOptions, l
9495
conn, err := protocolService.GetConnection(ctx, overlay)
9596
if err != nil {
9697
logger.Error(err, "L2 get connection")
98+
break
9799
}
98100
err = conn.SendMessage(ctx, msg)
99101
if err != nil {
@@ -126,6 +128,7 @@ func ListeningWs(ctx context.Context, conn *websocket.Conn, options WsOptions, l
126128
}
127129
}
128130
}
131+
options.Cancel()
129132
}()
130133

131134
defer func() {
@@ -147,7 +150,6 @@ func ListeningWs(ctx context.Context, conn *websocket.Conn, options WsOptions, l
147150
}
148151
return
149152
case <-ticker.C:
150-
err = conn.SetWriteDeadline(time.Now().Add(writeDeadline))
151153
if err != nil {
152154
logger.Debug("L2 ws: set write deadline failed", "error", err)
153155
return

0 commit comments

Comments
 (0)