From f96058057d588b02df2c37476b6cd4397280a223 Mon Sep 17 00:00:00 2001 From: mountain Date: Thu, 16 Nov 2023 19:59:25 +0900 Subject: [PATCH] =?UTF-8?q?ccu=20metric=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/tavern.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/tavern.go b/core/tavern.go index 9e92fa6..d922ff5 100644 --- a/core/tavern.go +++ b/core/tavern.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "strings" + "sync/atomic" "time" "github.com/go-redis/redis/v8" @@ -14,6 +15,7 @@ import ( "repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon/flagx" "repositories.action2quare.com/ayo/gocommon/logger" + "repositories.action2quare.com/ayo/gocommon/metric" "repositories.action2quare.com/ayo/gocommon/session" "repositories.action2quare.com/ayo/gocommon/wshandler" @@ -21,6 +23,8 @@ import ( ) var devflag = flagx.Bool("dev", false, "") +var totalCCUWriter = metric.NewMetric(metric.MetricGuage, "concurrent_user", "current connected user count") +var ccu = int64(0) type TavernConfig struct { session.SessionConfig `json:",inline"` @@ -79,6 +83,7 @@ func New(context context.Context, wsh *wshandler.WebsocketHandler) (*Tavern, err } func (tv *Tavern) Cleanup() { + totalCCUWriter(0) tv.mongoClient.Close() } @@ -136,6 +141,7 @@ func (tv *Tavern) LeaveChannel(ctx wshandler.ApiCallContext) { } func (tv *Tavern) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) { + totalCCUWriter(float64(atomic.AddInt64(&ccu, 1))) tv.redison.Del(tv.redison.Context(), callby.Accid.Hex()) _, err := tv.redison.JSONSet(callby.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()}) if err != nil { @@ -144,6 +150,8 @@ func (tv *Tavern) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender } func (tv *Tavern) ClientDisconnected(msg string, callby *wshandler.Sender) { + totalCCUWriter(float64(atomic.AddInt64(&ccu, -1))) + tv.redison.Del(tv.redison.Context(), callby.Accid.Hex()).Result() }