Skip to content

Commit

Permalink
websocket: Make StatusConnectionListener generic
Browse files Browse the repository at this point in the history
Only the call to get the status is status-specific, the rest of the code
marshalling the data, fetching it every few seconds, ... is applicable
to anything we want to send through websocket. This commit extracts the
'status' bit from the generic code, this will be useful if we add other
similar websocket calls.
  • Loading branch information
cfergeau committed Feb 20, 2023
1 parent df6daa5 commit c413ac6
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions pkg/crc/api/websocket/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@ import (
"time"

"github.com/crc-org/crc/pkg/crc/logging"
"github.com/crc-org/crc/pkg/crc/machine"
crcMachine "github.com/crc-org/crc/pkg/crc/machine"
)

type StatusConnectionListener struct {
machine machine.Client
done chan bool
type genData func() (interface{}, error)

type TickListener struct {
done chan bool
generator genData
tickPeriod time.Duration
}

func NewStatusListener(machine machine.Client) ConnectionListener {
return &StatusConnectionListener{
machine: machine,
done: make(chan bool),
func NewStatusListener(machine crcMachine.Client) ConnectionListener {
getStatus := func() (interface{}, error) {
return machine.GetClusterLoad()
}
return NewTickListener(getStatus)
}

func (s StatusConnectionListener) start(dataSender io.Writer) {
func NewTickListener(generator genData) ConnectionListener {
return &TickListener{
done: make(chan bool),
generator: generator,
tickPeriod: 2000 * time.Millisecond,
}
}

ticker := time.NewTicker(2000 * time.Millisecond)
func (s *TickListener) start(dataSender io.Writer) {

ticker := time.NewTicker(s.tickPeriod)
go func() {
for {
select {
Expand All @@ -32,12 +43,13 @@ func (s StatusConnectionListener) start(dataSender io.Writer) {
logging.Debug("stop fetching machine info")
return
case <-ticker.C:
status, err := s.machine.GetClusterLoad()
data, err := s.generator()
if err != nil {
logging.Errorf("unexpected error during getting machine status: %v", err)
continue
}

bytes, marshallError := json.Marshal(status)
bytes, marshallError := json.Marshal(data)
if marshallError != nil {
logging.Errorf("unexpected error during status object to JSON conversion: %v", err)
continue
Expand All @@ -52,6 +64,6 @@ func (s StatusConnectionListener) start(dataSender io.Writer) {

}

func (s StatusConnectionListener) stop() {
func (s *TickListener) stop() {
s.done <- true
}

0 comments on commit c413ac6

Please sign in to comment.