Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement websocket keepalive #64

Merged
merged 2 commits into from
Dec 27, 2024

Conversation

muzzammilshahid
Copy link
Member

No description provided.

@om26er om26er changed the title Implement websocket ping pong Implement websocket keepalive Dec 24, 2024
@muzzammilshahid muzzammilshahid force-pushed the ping-pong branch 2 times, most recently from 3fe558a to a32f138 Compare December 26, 2024 14:59
@om26er om26er self-assigned this Dec 26, 2024
acceptor.go Outdated
@@ -69,10 +70,11 @@ func (w *WebSocketAcceptor) Spec(subProtocol string) (serializers.Serializer, er
return serializer, nil
}

func (w *WebSocketAcceptor) Accept(conn net.Conn) (BaseSession, error) {
func (w *WebSocketAcceptor) Accept(conn net.Conn, keepaliveInterval time.Duration,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two parameters should be moved into WebSocketServerConfig and Accept should accept that.

acceptor_test.go Outdated
@@ -25,7 +26,7 @@ func TestAccept(t *testing.T) {
require.NotNil(t, conn)

acceptor := xconn.WebSocketAcceptor{}
session, err := acceptor.Accept(conn)
session, err := acceptor.Accept(conn, time.Duration(0), time.Duration(0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would then just become

Suggested change
session, err := acceptor.Accept(conn, time.Duration(0), time.Duration(0))
session, err := acceptor.Accept(conn, nil)

client.go Outdated
Comment on lines 17 to 18
KeepaliveInterval time.Duration
KeepaliveTimeout time.Duration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
KeepaliveInterval time.Duration
KeepaliveTimeout time.Duration
KeepAliveInterval time.Duration
KeepAliveTimeout time.Duration

joiner.go Outdated
@@ -22,7 +22,8 @@ type WebSocketJoiner struct {
DialTimeout time.Duration
}

func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string) (BaseSession, error) {
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, keepaliveInterval time.Duration,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those new parameters should be moved inside WSDialerConfig struct. And this function should signature should that as last argument

peer.go Outdated
wsReader, wsWriter, err = ServerSideWSReaderWriter(binary)
} else {
wsReader, wsWriter, err = ClientSideWSReaderWriter(binary)
func NewWebSocketPeer(conn net.Conn, protocol string, binary, server bool, keepaliveInterval time.Duration,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a new WSPeerConfig struct and move all parameters of this function there (except for conn)

peer.go Outdated
}

if err != nil {
return nil, err
if keepaliveInterval != 0*time.Second {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if keepaliveInterval != 0*time.Second {
if keepaliveInterval != 0 {

peer.go Outdated
ticker := time.NewTicker(keepaliveInterval)
defer ticker.Stop()

if keepaliveTimeout == 0*time.Second {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if keepaliveTimeout == 0*time.Second {
if keepaliveTimeout == 0 {

peer.go Outdated
select {
case <-c.pingCh:
case <-time.After(keepaliveTimeout):
log.Println("pong timeout, closing connection")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Println("pong timeout, closing connection")
log.Println("ping timeout, closing connection")

peer.go Outdated
case ws.OpText, ws.OpBinary:
return payload, nil
case ws.OpPing:
err = c.writeOpFunc(c.conn, ws.OpPong, payload)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use if in this line

server.go Outdated
}

func NewServer(router *Router, authenticator auth.ServerAuthenticator, throttle *internal.Throttle) *Server {
func NewServer(router *Router, authenticator auth.ServerAuthenticator, throttle *internal.Throttle,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a new ServerConfig struct

joiner.go Outdated
}

func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string) (BaseSession, error) {
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, dialConfig WSDialerConfig) (BaseSession, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, dialConfig WSDialerConfig) (BaseSession, error) {
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, config *WSDialerConfig) (BaseSession, error) {

we should also change DialWebSocket to accept pointer of config

peer.go Outdated
for {
<-ticker.C
// Send a ping
err := c.writeOpFunc(c.conn, ws.OpPing, []byte("ping"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of sending "ping" please send 4 random bytes

acceptor.go Show resolved Hide resolved
@muzzammilshahid muzzammilshahid merged commit c2b4b5e into xconnio:main Dec 27, 2024
1 check passed
@muzzammilshahid muzzammilshahid deleted the ping-pong branch December 27, 2024 14:27
@muzzammilshahid muzzammilshahid restored the ping-pong branch December 27, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants