Skip to content

Commit 2624c64

Browse files
committed
Merge branch 'master' of github.com:The-K-R-O-K/flow-go into illia-malachyn/6635-tests-for-websocket-controller
2 parents a4e07a3 + ed6a2c4 commit 2624c64

File tree

13 files changed

+999
-128
lines changed

13 files changed

+999
-128
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ generate-mocks: install-mock-generators
205205
mockery --name 'BlockTracker' --dir="./engine/access/subscription" --case=underscore --output="./engine/access/subscription/mock" --outpkg="mock"
206206
mockery --name 'DataProvider' --dir="./engine/access/rest/websockets/data_providers" --case=underscore --output="./engine/access/rest/websockets/data_providers/mock" --outpkg="mock"
207207
mockery --name 'DataProviderFactory' --dir="./engine/access/rest/websockets/data_providers" --case=underscore --output="./engine/access/rest/websockets/data_providers/mock" --outpkg="mock"
208+
mockery --name 'WebsocketConnection' --dir="./engine/access/rest/websockets" --case=underscore --output="./engine/access/rest/websockets/mock" --outpkg="mock"
208209
mockery --name 'ExecutionDataTracker' --dir="./engine/access/subscription" --case=underscore --output="./engine/access/subscription/mock" --outpkg="mock"
209210
mockery --name 'ConnectionFactory' --dir="./engine/access/rpc/connection" --case=underscore --output="./engine/access/rpc/connection/mock" --outpkg="mock"
210211
mockery --name 'Communicator' --dir="./engine/access/rpc/backend" --case=underscore --output="./engine/access/rpc/backend/mock" --outpkg="mock"

engine/access/rest/websockets/config.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ import (
44
"time"
55
)
66

7+
const (
8+
// PingPeriod defines the interval at which ping messages are sent to the client.
9+
// This value must be less than pongWait, cause it that case the server ensures it sends a ping well before the PongWait
10+
// timeout elapses. Each new pong message resets the server's read deadline, keeping the connection alive as long as
11+
// the client is responsive.
12+
//
13+
// Example:
14+
// At t=9, the server sends a ping, initial read deadline is t=10 (for the first message)
15+
// At t=10, the client responds with a pong. The server resets its read deadline to t=20.
16+
// At t=18, the server sends another ping. If the client responds with a pong at t=19, the read deadline is extended to t=29.
17+
//
18+
// In case of failure:
19+
// If the client stops responding, the server will send a ping at t=9 but won't receive a pong by t=10. The server then closes the connection.
20+
PingPeriod = (PongWait * 9) / 10
21+
22+
// PongWait specifies the maximum time to wait for a pong response message from the peer
23+
// after sending a ping
24+
PongWait = 10 * time.Second
25+
26+
// WriteWait specifies a timeout for the write operation. If the write
27+
// isn't completed within this duration, it fails with a timeout error.
28+
// SetWriteDeadline ensures the write operation does not block indefinitely
29+
// if the client is slow or unresponsive. This prevents resource exhaustion
30+
// and allows the server to gracefully handle timeouts for delayed writes.
31+
WriteWait = 10 * time.Second
32+
)
33+
734
type Config struct {
835
MaxSubscriptionsPerConnection uint64
936
MaxResponsesPerSecond uint64
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
11
package websockets
22

33
import (
4+
"time"
5+
46
"github.com/gorilla/websocket"
57
)
68

7-
// We wrap gorilla's websocket connection with interface
8-
// to be able to mock it in order to test the types dependent on it
9-
109
type WebsocketConnection interface {
1110
ReadJSON(v interface{}) error
1211
WriteJSON(v interface{}) error
12+
WriteControl(messageType int, deadline time.Time) error
1313
Close() error
14+
SetReadDeadline(deadline time.Time) error
15+
SetWriteDeadline(deadline time.Time) error
16+
SetPongHandler(h func(string) error)
1417
}
1518

16-
type GorillaWebsocketConnection struct {
19+
type WebsocketConnectionImpl struct {
1720
conn *websocket.Conn
1821
}
1922

20-
func NewGorillaWebsocketConnection(conn *websocket.Conn) *GorillaWebsocketConnection {
21-
return &GorillaWebsocketConnection{
23+
func NewWebsocketConnection(conn *websocket.Conn) *WebsocketConnectionImpl {
24+
return &WebsocketConnectionImpl{
2225
conn: conn,
2326
}
2427
}
2528

26-
var _ WebsocketConnection = (*GorillaWebsocketConnection)(nil)
29+
var _ WebsocketConnection = (*WebsocketConnectionImpl)(nil)
30+
31+
func (c *WebsocketConnectionImpl) ReadJSON(v interface{}) error {
32+
return c.conn.ReadJSON(v)
33+
}
34+
35+
func (c *WebsocketConnectionImpl) WriteJSON(v interface{}) error {
36+
return c.conn.WriteJSON(v)
37+
}
38+
39+
func (c *WebsocketConnectionImpl) WriteControl(messageType int, deadline time.Time) error {
40+
return c.conn.WriteControl(messageType, nil, deadline)
41+
}
2742

28-
func (m *GorillaWebsocketConnection) ReadJSON(v interface{}) error {
29-
return m.conn.ReadJSON(v)
43+
func (c *WebsocketConnectionImpl) Close() error {
44+
return c.conn.Close()
3045
}
3146

32-
func (m *GorillaWebsocketConnection) WriteJSON(v interface{}) error {
33-
return m.conn.WriteJSON(v)
47+
func (c *WebsocketConnectionImpl) SetReadDeadline(deadline time.Time) error {
48+
return c.conn.SetReadDeadline(deadline)
3449
}
3550

36-
func (m *GorillaWebsocketConnection) SetCloseHandler(handler func(code int, text string) error) {
37-
m.conn.SetCloseHandler(handler)
51+
func (c *WebsocketConnectionImpl) SetWriteDeadline(deadline time.Time) error {
52+
return c.conn.SetWriteDeadline(deadline)
3853
}
3954

40-
func (m *GorillaWebsocketConnection) Close() error {
41-
return m.conn.Close()
55+
func (c *WebsocketConnectionImpl) SetPongHandler(h func(string) error) {
56+
c.conn.SetPongHandler(h)
4257
}

0 commit comments

Comments
 (0)