Skip to content

Commit

Permalink
websocket: Use io.Writer interface
Browse files Browse the repository at this point in the history
The SendData function type and the Endpoint.Write methods are very close
to what the io.Writer interface provides. Let's use it instead.
  • Loading branch information
cfergeau committed Feb 20, 2023
1 parent 0bce019 commit df6daa5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
7 changes: 5 additions & 2 deletions pkg/crc/api/websocket/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package websocket

import (
"io"
"sync"
)

type Endpoint interface {
addClient(client *wsClient)
deleteClient(client *wsClient)
setHandler(handler *EndpointHandler)
Write([]byte)
io.Writer
}

type endpoint struct {
Expand Down Expand Up @@ -46,7 +47,7 @@ func (e *endpoint) deleteClient(client *wsClient) {
}

// send data bytes to clients
func (e *endpoint) Write(data []byte) {
func (e *endpoint) Write(data []byte) (int, error) {
e.clientsMutex.Lock()
defer e.clientsMutex.Unlock()

Expand All @@ -58,6 +59,8 @@ func (e *endpoint) Write(data []byte) {
go client.closeSlow()
}
}

return len(data), nil
}

func (e *endpoint) setHandler(handler *EndpointHandler) {
Expand Down
20 changes: 11 additions & 9 deletions pkg/crc/api/websocket/handler.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package websocket

import (
"io"
)

type EndpointHandler struct {
listeners []ConnectionListener
sendData SendData
listeners []ConnectionListener
dataSender io.Writer
}

type SendData func([]byte)

type ConnectionListener interface {
// called when first client connected
start(sendData SendData)
start(dataSender io.Writer)
// called when all clients close connections
stop()
}

func NewEndpointHandler(data SendData) *EndpointHandler {
func NewEndpointHandler(dataSender io.Writer) *EndpointHandler {
handler := &EndpointHandler{
listeners: make([]ConnectionListener, 0),
sendData: data,
listeners: make([]ConnectionListener, 0),
dataSender: dataSender,
}

return handler
}

func (h *EndpointHandler) hasClient() {
for _, l := range h.listeners {
l.start(h.sendData)
l.start(h.dataSender)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/api/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func writeTimeout(ctx goContext.Context, timeout time.Duration, c *websocket.Con

func createStatusEndpoint(machine machine.Client) Endpoint {
statusEndpoint := NewEndpoint()
statusEndpointHandler := NewEndpointHandler(statusEndpoint.Write)
statusEndpointHandler := NewEndpointHandler(statusEndpoint)
statusEndpoint.setHandler(statusEndpointHandler)
statusEndpointHandler.addListener(NewStatusListener(machine))
return statusEndpoint
Expand Down
8 changes: 6 additions & 2 deletions pkg/crc/api/websocket/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package websocket

import (
"encoding/json"
"io"
"time"

"github.com/crc-org/crc/pkg/crc/logging"
Expand All @@ -20,7 +21,7 @@ func NewStatusListener(machine machine.Client) ConnectionListener {
}
}

func (s StatusConnectionListener) start(sendData SendData) {
func (s StatusConnectionListener) start(dataSender io.Writer) {

ticker := time.NewTicker(2000 * time.Millisecond)
go func() {
Expand All @@ -41,7 +42,10 @@ func (s StatusConnectionListener) start(sendData SendData) {
logging.Errorf("unexpected error during status object to JSON conversion: %v", err)
continue
}
sendData(bytes)
_, err = dataSender.Write(bytes)
if err != nil {
logging.Errorf("unexpected error during writing data to WebSocket: %v", err)
}
}
}
}()
Expand Down

0 comments on commit df6daa5

Please sign in to comment.