Proof-of-Concept splitting of reader and writers.

This commit is contained in:
Sacha Ligthert 2025-02-05 23:04:51 +01:00
parent 5d534221a4
commit 1d16d30cb6
10 changed files with 363041 additions and 73 deletions

View File

@ -2,7 +2,6 @@ package client
import (
"log"
"net/url"
"os"
"os/signal"
"strconv"
@ -11,62 +10,77 @@ import (
"github.com/gorilla/websocket"
"github.com/inhies/go-bytesize"
"github.com/mackerelio/go-osstat/memory"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/cpu"
)
func (client *Client) Start() {
// Setup the interrupts
// Setup interrupt handling
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: client.ServerAddress + ":" + strconv.Itoa(client.ServerPort), Path: "/ws"}
log.Printf("Connecting to %s", u.String())
// sigh
var err error
client.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
// Connect to the server
err := client.connectToServer()
if err != nil {
log.Fatal("dial:", err)
log.Fatal("ERROR: Failed connecting to server - ", err)
}
defer client.conn.Close()
done := make(chan struct{})
// Agent => Manager:
// - register: register an agent with the manager
// - update: agents sends CPU and Mem metrics (does this every second)
// - solution: solution of a task given by the manager
// - deregister: deregister an agent from the manager
// Register
// One-time at the start
// Fetch the hostname
hostname, err := os.Hostname()
if err != nil {
log.Fatal("ERROR: Failed to register - cannot get hostname - ", err)
}
// Fetch the number of logical cores
cpustats, err := cpu.Counts(true)
if err != nil {
log.Fatal("ERROR: Failed to register - unable to fetch number of cores - ", err)
}
// Fetch memory stats in bytes
mem, err := memory.Get()
if err != nil {
log.Println("ERROR: Failed to register - fetching memory info - ", err)
}
// Use the ByteSize package to allow for memory calculations
b := bytesize.New(float64(mem.Total))
// Register this agent with the scheduler
client.writeToServer("register;" + hostname + ";" + strconv.Itoa(cpustats) + ";" + b.String())
// Setup updater logic
updater := make(chan string)
go client.statusUpdater(updater)
// Runs continuesly in the background
go client.statusUpdater()
go func() {
defer close(done)
for {
_, message, err := client.conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
}
}()
// Start handler for incoming messages
done := make(chan struct{})
go client.handleIncoming(done)
for {
select {
case <-done:
return
case update := <-updater: // Dumping this into a var saves a second. Weird!
// Dump the message towards the server.
err = client.conn.WriteMessage(websocket.TextMessage, []byte(update))
if err != nil {
log.Println("write:", err)
return
}
case <-interrupt:
log.Println("interrupt")
log.Println("Interrupt received -- Stopping")
// Dereg the agent before dying off
client.writeToServer("deregister;" + hostname)
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := client.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
log.Println("ERROR: Close connection - ", err)
return
}
select {
@ -78,25 +92,3 @@ func (client *Client) Start() {
}
}
func (client *Client) statusUpdater(updater chan string) {
for {
// Fetch CPU usage in percentages
cpustats, err := cpu.Percent(time.Second, false)
if err != nil {
log.Println("Err:", err)
}
// Fetch memory stats in bytes
mem, err := memory.Get()
if err != nil {
log.Println("Err:", err)
}
// Use the ByteSize package to allow for memory calculations
b := bytesize.New(float64(mem.Used))
// Dump the message towards the server.
updater <- "update;" + strconv.Itoa(int(cpustats[0])) + ";" + b.String() + ";" + strconv.Itoa(client.taskId)
}
}

362880
client/blocks.csv Normal file

File diff suppressed because it is too large Load Diff

22
client/connectToServer.go Normal file
View File

@ -0,0 +1,22 @@
package client
import (
"log"
"net/url"
"strconv"
"github.com/gorilla/websocket"
)
func (client *Client) connectToServer() (err error) {
u := url.URL{Scheme: "ws", Host: client.ServerAddress + ":" + strconv.Itoa(client.ServerPort), Path: "/ws"}
log.Printf("Connecting to %s", u.String())
// sigh
client.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("ERROR: dialing - :", err)
return err
}
return nil
}

26
client/handleIncoming.go Normal file
View File

@ -0,0 +1,26 @@
package client
import (
"log"
"strings"
)
func (client *Client) handleIncoming(done chan struct{}) {
defer close(done)
for {
_, message, err := client.conn.ReadMessage()
if err != nil {
log.Println("ERROR: read message - :", err)
return
}
// Ignore printing keep alives
if string(message) == "keep alive - staying alive" {
continue
}
log.Printf("recv: %s", message)
inputs := strings.Split(string(message), ";")
if inputs[0] == "task" {
client.task(inputs)
}
}
}

38
client/statusUpdater.go Normal file
View File

@ -0,0 +1,38 @@
package client
import (
"log"
"strconv"
"time"
"github.com/inhies/go-bytesize"
"github.com/mackerelio/go-osstat/memory"
"github.com/shirou/gopsutil/cpu"
)
// Look into using a ticker instead of a for-loop
func (client *Client) statusUpdater() {
for {
// Fetch CPU usage in percentages
cpustats, err := cpu.Percent(time.Second, false)
if err != nil {
log.Println("ERROR: fetching CPU usage infor - ", err)
}
// Fetch memory stats in bytes
mem, err := memory.Get()
if err != nil {
log.Println("ERROR: fetching memory info - ", err)
}
// Use the ByteSize package to allow for memory calculations
b := bytesize.New(float64(mem.Used))
// Prepare the message.
update := "update;" + strconv.Itoa(int(cpustats[0])) + ";" + b.String() + ";" + strconv.Itoa(client.taskId)
// Write the message to the server
client.writeToServer(update)
}
}

7
client/task.go Normal file
View File

@ -0,0 +1,7 @@
package client
import "log"
func (client *Client) task(inputs []string) {
log.Println(inputs)
}

16
client/writeToServer.go Normal file
View File

@ -0,0 +1,16 @@
package client
import (
"log"
"github.com/gorilla/websocket"
)
func (client *Client) writeToServer(msg string) {
// Send the message towards the server.
err := client.conn.WriteMessage(websocket.TextMessage, []byte(msg))
if err != nil {
log.Println("ERROR: writing - ", err)
return
}
}

6
go.mod
View File

@ -8,14 +8,12 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf
github.com/mackerelio/go-osstat v0.2.5
github.com/shirou/gopsutil/v4 v4.25.1
github.com/shirou/gopsutil v3.21.11+incompatible
)
require (
github.com/ebitengine/purego v0.8.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect

15
go.sum
View File

@ -2,27 +2,18 @@ gitea.ligthert.net/golang/sudoku-funpark v0.0.0-20250129164508-c1f2a28ac155 h1:o
gitea.ligthert.net/golang/sudoku-funpark v0.0.0-20250129164508-c1f2a28ac155/go.mod h1:GdZ2otAB+NMA19Noe7UFeP3gJtHCU4h8N158Oj1jNVY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaBfAKrE1Cwb61YDtYq9JxChK1c7AKce7s=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/mackerelio/go-osstat v0.2.5 h1:+MqTbZUhoIt4m8qzkVoXUJg1EuifwlAJSk4Yl2GXh+o=
github.com/mackerelio/go-osstat v0.2.5/go.mod h1:atxwWF+POUZcdtR1wnsUcQxTytoHG4uhl2AKKzrOajY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v4 v4.25.1 h1:QSWkTc+fu9LTAWfkZwZ6j8MSUk4A2LV7rbH0ZqmLjXs=
github.com/shirou/gopsutil/v4 v4.25.1/go.mod h1:RoUCUpndaJFtT+2zsZzzmhvbfGoDCJ7nFXKJf8GqJbI=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
@ -32,11 +23,9 @@ github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9f
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -54,7 +54,7 @@ func readMessages(conn *websocket.Conn) {
func writeMessages(conn *websocket.Conn) {
for {
err := conn.WriteMessage(websocket.TextMessage, []byte("Hello World!"))
err := conn.WriteMessage(websocket.TextMessage, []byte("keep alive - staying alive"))
if err != nil {
log.Println("Error writing message:", err)
break