Compare commits
4 Commits
9cea173185
...
main
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8ca441cc24 | ||
![]() |
567d2fe03d | ||
![]() |
756985882a | ||
![]() |
bcb271ea3e |
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module statusServer
|
module gitea.tecamino.com/paadi/statusServer
|
||||||
|
|
||||||
go 1.24.0
|
go 1.24.0
|
||||||
|
|
||||||
|
@@ -3,9 +3,10 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"statusServer/models"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/statusServer/models"
|
||||||
|
|
||||||
json_dataModels "gitea.tecamino.com/paadi/tecamino-json_data/models"
|
json_dataModels "gitea.tecamino.com/paadi/tecamino-json_data/models"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@@ -57,8 +57,11 @@ var upgrader = websocket.Upgrader{
|
|||||||
EnableCompression: false,
|
EnableCompression: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ip, id string, port uint, logger *logging.Logger) (*Client, error) {
|
func NewClient(id, ip, endpoint 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}
|
if endpoint == "" {
|
||||||
|
endpoint = "status"
|
||||||
|
}
|
||||||
|
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", ip, port), Path: endpoint, RawQuery: "id=" + id}
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
id: id,
|
id: id,
|
||||||
|
@@ -3,7 +3,8 @@ package statusServer
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"statusServer/models"
|
|
||||||
|
"gitea.tecamino.com/paadi/statusServer/models"
|
||||||
|
|
||||||
pubSubModels "gitea.tecamino.com/paadi/pubSub/models"
|
pubSubModels "gitea.tecamino.com/paadi/pubSub/models"
|
||||||
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||||
@@ -23,7 +24,7 @@ type StatusClient struct {
|
|||||||
// serviceName: unique ID/name for this client
|
// serviceName: unique ID/name for this client
|
||||||
// ip, port: target server address
|
// ip, port: target server address
|
||||||
// debug: enable debug logging
|
// debug: enable debug logging
|
||||||
func NewStatusClient(serviceName, ip string, port uint, debug bool) (*StatusClient, error) {
|
func NewStatusClient(serviceName, ip, endpoint string, port uint, debug bool) (*StatusClient, error) {
|
||||||
config := models.Config{Log: logging.Config{Debug: debug}}
|
config := models.Config{Log: logging.Config{Debug: debug}}
|
||||||
config.Default()
|
config.Default()
|
||||||
|
|
||||||
@@ -42,7 +43,7 @@ func NewStatusClient(serviceName, ip string, port uint, debug bool) (*StatusClie
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect underlying websocket client
|
// connect underlying websocket client
|
||||||
sc.client, err = models.NewClient(ip, serviceName, port, logger)
|
sc.client, err = models.NewClient(serviceName, ip, endpoint, port, logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@@ -1,15 +1,14 @@
|
|||||||
package statusServer
|
package statusServer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
|
||||||
"statusServer/utils"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"statusServer/models"
|
"gitea.tecamino.com/paadi/statusServer/utils"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/statusServer/models"
|
||||||
|
|
||||||
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
@@ -96,56 +95,12 @@ func NewStatusServer(config models.Config) (*StatusServer, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHttp starts the HTTP server and continuously publishes runtime metrics
|
// ServeHttp starts the HTTP server
|
||||||
// (memory allocation + GC count) to the "info" topic.
|
|
||||||
//
|
|
||||||
// It blocks until the HTTP server stops. Resources are cleaned up on shutdown:
|
// It blocks until the HTTP server stops. Resources are cleaned up on shutdown:
|
||||||
// PubSub is closed and a shutdown message is logged.
|
// PubSub is closed and a shutdown message is logged.
|
||||||
func (s *StatusServer) ServeHttp() error {
|
func (s *StatusServer) ServeHttp() error {
|
||||||
s.log.Debug("ServeHttp", "start publishing runtime metrics (memory + GC count)")
|
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
|
// log startup
|
||||||
s.log.Info("ServeHttp", fmt.Sprintf("start listening on %s:%d", s.Ip, s.Port))
|
s.log.Info("ServeHttp", fmt.Sprintf("start listening on %s:%d", s.Ip, s.Port))
|
||||||
|
|
||||||
|
@@ -2,7 +2,8 @@ package statusServer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"statusServer/handlers"
|
|
||||||
|
"gitea.tecamino.com/paadi/statusServer/handlers"
|
||||||
|
|
||||||
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
logging "gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"statusServer"
|
|
||||||
"statusServer/models"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/statusServer"
|
||||||
|
"gitea.tecamino.com/paadi/statusServer/models"
|
||||||
|
|
||||||
"gitea.tecamino.com/paadi/tecamino-logger/logging"
|
"gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,11 +35,11 @@ func TestConnection(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
client1, err := statusServer.NewStatusClient("adrian", "127.0.0.1", 8080, true)
|
client1, err := statusServer.NewStatusClient("adrian", "127.0.0.1", "", 8080, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
client, err := statusServer.NewStatusClient("test", "127.0.0.1", 8080, true)
|
client, err := statusServer.NewStatusClient("test", "127.0.0.1", "", 8080, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user