ccu metric 추가
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
"repositories.action2quare.com/ayo/gocommon"
|
"repositories.action2quare.com/ayo/gocommon"
|
||||||
"repositories.action2quare.com/ayo/gocommon/flagx"
|
"repositories.action2quare.com/ayo/gocommon/flagx"
|
||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
|
"repositories.action2quare.com/ayo/gocommon/metric"
|
||||||
"repositories.action2quare.com/ayo/gocommon/session"
|
"repositories.action2quare.com/ayo/gocommon/session"
|
||||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||||
|
|
||||||
@ -21,6 +23,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var devflag = flagx.Bool("dev", false, "")
|
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 {
|
type TavernConfig struct {
|
||||||
session.SessionConfig `json:",inline"`
|
session.SessionConfig `json:",inline"`
|
||||||
@ -79,6 +83,7 @@ func New(context context.Context, wsh *wshandler.WebsocketHandler) (*Tavern, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tv *Tavern) Cleanup() {
|
func (tv *Tavern) Cleanup() {
|
||||||
|
totalCCUWriter(0)
|
||||||
tv.mongoClient.Close()
|
tv.mongoClient.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,6 +141,7 @@ func (tv *Tavern) LeaveChannel(ctx wshandler.ApiCallContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tv *Tavern) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) {
|
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())
|
tv.redison.Del(tv.redison.Context(), callby.Accid.Hex())
|
||||||
_, err := tv.redison.JSONSet(callby.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()})
|
_, err := tv.redison.JSONSet(callby.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()})
|
||||||
if err != nil {
|
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) {
|
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()
|
tv.redison.Del(tv.redison.Context(), callby.Accid.Hex()).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user