3 Commits

Author SHA1 Message Date
Adrian Zuercher
567d2fe03d add optional endpoint to new client 2025-08-24 21:18:43 +02:00
Adrian Zuercher
756985882a remove memory and garbage collection infos 2025-08-24 08:33:25 +02:00
Adrian Zuercher
bcb271ea3e change package name to repo name 2025-08-24 08:12:25 +02:00
7 changed files with 20 additions and 58 deletions

2
go.mod
View File

@@ -1,4 +1,4 @@
module statusServer
module gitea.tecamino.com/paadi/statusServer
go 1.24.0

View File

@@ -3,9 +3,10 @@ package handlers
import (
"encoding/json"
"fmt"
"statusServer/models"
"sync"
"gitea.tecamino.com/paadi/statusServer/models"
json_dataModels "gitea.tecamino.com/paadi/tecamino-json_data/models"
"github.com/gin-gonic/gin"

View File

@@ -57,8 +57,11 @@ var upgrader = websocket.Upgrader{
EnableCompression: false,
}
func NewClient(ip, id string, port uint, logger *logging.Logger) (*Client, error) {
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", ip, port), Path: "status", RawQuery: "id=" + id}
func NewClient(id, ip, endpoint string, port uint, logger *logging.Logger) (*Client, error) {
if endpoint == "" {
endpoint = "status"
}
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", ip, port), Path: endpoint, RawQuery: "id=" + id}
c := &Client{
id: id,

View File

@@ -3,7 +3,8 @@ package statusServer
import (
"encoding/json"
"fmt"
"statusServer/models"
"gitea.tecamino.com/paadi/statusServer/models"
pubSubModels "gitea.tecamino.com/paadi/pubSub/models"
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
@@ -42,7 +43,7 @@ func NewStatusClient(serviceName, ip string, port uint, debug bool) (*StatusClie
}
// connect underlying websocket client
sc.client, err = models.NewClient(ip, serviceName, port, logger)
sc.client, err = models.NewClient(serviceName, ip, "", port, logger)
if err != nil {
return nil, err
}

View File

@@ -1,15 +1,14 @@
package statusServer
import (
"context"
"fmt"
"net/http"
"runtime"
"statusServer/utils"
"strings"
"time"
"statusServer/models"
"gitea.tecamino.com/paadi/statusServer/utils"
"gitea.tecamino.com/paadi/statusServer/models"
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
"github.com/gin-contrib/cors"
@@ -96,56 +95,12 @@ func NewStatusServer(config models.Config) (*StatusServer, error) {
return s, nil
}
// ServeHttp starts the HTTP server and continuously publishes runtime metrics
// (memory allocation + GC count) to the "info" topic.
//
// ServeHttp starts the HTTP server
// It blocks until the HTTP server stops. Resources are cleaned up on shutdown:
// PubSub is closed and a shutdown message is logged.
func (s *StatusServer) ServeHttp() error {
s.log.Debug("ServeHttp", "start publishing runtime metrics (memory + GC count)")
// create cancellable context for metrics goroutine
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// metrics publisher goroutine
go func() {
for {
var init bool
var m runtime.MemStats
var oldM uint64
var oldGc uint32
var info struct {
Memory uint64 `json:"memory"`
GC uint32 `json:"gc"`
}
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
// stop publishing if context is canceled
return
case <-ticker.C:
// read runtime memory statistics
runtime.ReadMemStats(&m)
info.GC = m.NumGC
// publish only when values changed or first run
if oldGc != m.NumGC || oldM != m.Alloc/1024 || !init {
info.Memory = m.Alloc / 1024
s.pubSub.Publish("info", info)
oldGc = m.NumGC
oldM = m.Alloc / 1024
init = true
}
}
}
}
}()
// log startup
s.log.Info("ServeHttp", fmt.Sprintf("start listening on %s:%d", s.Ip, s.Port))

View File

@@ -2,7 +2,8 @@ package statusServer
import (
"fmt"
"statusServer/handlers"
"gitea.tecamino.com/paadi/statusServer/handlers"
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
"github.com/gin-gonic/gin"

View File

@@ -1,11 +1,12 @@
package test
import (
"statusServer"
"statusServer/models"
"testing"
"time"
"gitea.tecamino.com/paadi/statusServer"
"gitea.tecamino.com/paadi/statusServer/models"
"gitea.tecamino.com/paadi/tecamino-logger/logging"
)