From 6f9f791f02c08befa15e76f68e6ddffc4e011a15 Mon Sep 17 00:00:00 2001 From: mountain Date: Sun, 6 Aug 2023 12:28:11 +0900 Subject: [PATCH] =?UTF-8?q?room=20=EC=9D=B8=EC=9B=90=EC=9D=84=20count?= =?UTF-8?q?=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wshandler/room.go | 7 ++++++- wshandler/wshandler.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/wshandler/room.go b/wshandler/room.go index c26bbf0..9706e93 100644 --- a/wshandler/room.go +++ b/wshandler/room.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" + "github.com/go-redis/redis/v8" "github.com/gorilla/websocket" "repositories.action2quare.com/ayo/gocommon/logger" ) @@ -16,10 +17,11 @@ type room struct { name string destroyChan chan<- string sendMsgChan chan<- send_msg_queue_elem + redisClient *redis.Client } // 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room -func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room { +func makeRoom(name string, redisClient *redis.Client, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room { return &room{ inChan: make(chan *wsconn, 10), outChan: make(chan *wsconn, 10), @@ -27,6 +29,7 @@ func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_ms name: name, destroyChan: destroyChan, sendMsgChan: sendMsgChan, + redisClient: redisClient, } } @@ -70,9 +73,11 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b return true case conn := <-r.inChan: + r.redisClient.HIncrBy(ctx, r.name, "count", 1).Result() (*conns)[conn.sender.Accid.Hex()] = conn case conn := <-r.outChan: + r.redisClient.HIncrBy(ctx, r.name, "count", -1).Result() delete((*conns), conn.sender.Accid.Hex()) if len(*conns) == 0 && r.destroyChan != nil { r.destroyChan <- r.name diff --git a/wshandler/wshandler.go b/wshandler/wshandler.go index d06d72a..e1598cb 100644 --- a/wshandler/wshandler.go +++ b/wshandler/wshandler.go @@ -362,7 +362,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) { findRoom := func(name string, create bool) *room { room := rooms[name] if room == nil && create { - room = makeRoom(name, roomDestroyChan, sh.sendMsgChan) + room = makeRoom(name, sh.redisSync, roomDestroyChan, sh.sendMsgChan) rooms[name] = room room.start(ctx) go sh.callReceiver.OnRoomCreated(sh.region, name)