From 6d76a88c9cb5c474e584fabdefbefcf41fa83058 Mon Sep 17 00:00:00 2001 From: Sacha Ligthert Date: Tue, 4 Feb 2025 01:22:31 +0100 Subject: [PATCH] Some more changes to the client. I got a grasp on how this may work. --- README.md | 6 ++++++ client/Start.go | 50 +++++++++++++++++++++++++++++++++++++++---------- client/types.go | 4 ++++ go.mod | 18 +++++++++++++++++- go.sum | 38 +++++++++++++++++++++++++++++++++++++ server/Start.go | 2 +- 6 files changed, 106 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fa4066c..37d5683 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,9 @@ Solver flags := flags.Flags{Controller: &controller} solver := solver.Solver{Controller: &controller, Outp: &outp} ``` +### Manager & Client channel setup +#### Client + + +#### Manager + diff --git a/client/Start.go b/client/Start.go index 3be3b12..945656c 100644 --- a/client/Start.go +++ b/client/Start.go @@ -9,27 +9,37 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/inhies/go-bytesize" + "github.com/mackerelio/go-osstat/memory" + "github.com/shirou/gopsutil/v4/cpu" ) func (client *Client) Start() { + // Setup the interrupts 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()) + log.Printf("Connecting to %s", u.String()) - c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) + // sigh + var err error + client.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil) if err != nil { log.Fatal("dial:", err) } - defer c.Close() + defer client.conn.Close() done := make(chan struct{}) + // Setup updater logic + updater := make(chan string) + go client.statusUpdater(updater) + go func() { defer close(done) for { - _, message, err := c.ReadMessage() + _, message, err := client.conn.ReadMessage() if err != nil { log.Println("read:", err) return @@ -38,15 +48,13 @@ func (client *Client) Start() { } }() - ticker := time.NewTicker(time.Second) - defer ticker.Stop() - for { select { case <-done: return - case t := <-ticker.C: - err := c.WriteMessage(websocket.TextMessage, []byte(t.String())) + 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 @@ -56,7 +64,7 @@ func (client *Client) Start() { // Cleanly close the connection by sending a close message and then // waiting (with timeout) for the server to close the connection. - err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + err := client.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) if err != nil { log.Println("write close:", err) return @@ -70,3 +78,25 @@ 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) + } +} diff --git a/client/types.go b/client/types.go index 558a8d4..1b66a25 100644 --- a/client/types.go +++ b/client/types.go @@ -1,6 +1,10 @@ package client +import "github.com/gorilla/websocket" + type Client struct { ServerAddress string ServerPort int + taskId int + conn *websocket.Conn } diff --git a/go.mod b/go.mod index f6c616d..9af4747 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,20 @@ go 1.23.4 require gitea.ligthert.net/golang/sudoku-funpark v0.0.0-20250129164508-c1f2a28ac155 -require github.com/gorilla/websocket v1.5.3 +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 +) + +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/tklauser/go-sysconf v0.3.12 // indirect + github.com/tklauser/numcpus v0.6.1 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect + golang.org/x/sys v0.28.0 // indirect +) diff --git a/go.sum b/go.sum index 844751a..7a27324 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,42 @@ gitea.ligthert.net/golang/sudoku-funpark v0.0.0-20250129164508-c1f2a28ac155 h1:o3MLpDn26r/9DOCdATINPck2W0pNdXi6WiJcpKQwAYQ= 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/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= +github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= +github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= +github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= +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= diff --git a/server/Start.go b/server/Start.go index 3a15fff..8c144a8 100644 --- a/server/Start.go +++ b/server/Start.go @@ -61,6 +61,6 @@ func writeMessages(conn *websocket.Conn) { } // This sets the time-out for any outgoing messages. // conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) - time.Sleep(time.Second) + time.Sleep(time.Minute) } }