wshandler에서 authcache제거하고 config 포맷 변경
This commit is contained in:
@ -205,7 +205,7 @@ func (acg *AuthCollectionGlobal) Reload(context context.Context) error {
|
|||||||
for r, url := range config.RegionStorage {
|
for r, url := range config.RegionStorage {
|
||||||
if _, ok := oldval[r]; !ok {
|
if _, ok := oldval[r]; !ok {
|
||||||
// 새로 생겼네
|
// 새로 생겼네
|
||||||
redisClient, err := NewRedisClient(url.Redis.URL, url.Redis.Offset["session"])
|
redisClient, err := NewRedisClient(url.Redis["session"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -228,7 +228,7 @@ func NewAuthCollectionGlobal(context context.Context, apiToken string) (AuthColl
|
|||||||
|
|
||||||
output := make(map[string]*AuthCollection)
|
output := make(map[string]*AuthCollection)
|
||||||
for region, url := range config.RegionStorage {
|
for region, url := range config.RegionStorage {
|
||||||
redisClient, err := NewRedisClient(url.Redis.URL, url.Redis.Offset["session"])
|
redisClient, err := NewRedisClient(url.Redis["session"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return AuthCollectionGlobal{}, err
|
return AuthCollectionGlobal{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
26
redis.go
26
redis.go
@ -2,7 +2,6 @@ package gocommon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -19,23 +18,30 @@ func newRedisClient(uri string, dbidxoffset int) *redis.Client {
|
|||||||
return redis.NewClient(option)
|
return redis.NewClient(option)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRedisClient(uri string, dbidx int) (*redis.Client, error) {
|
func NewRedisClient(uri string) (*redis.Client, error) {
|
||||||
if !*devflag {
|
if !*devflag {
|
||||||
return newRedisClient(uri, dbidx), nil
|
return newRedisClient(uri, 0), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rdb := newRedisClient(uri, 0)
|
option, err := redis.ParseURL(uri)
|
||||||
devUrl, _ := url.Parse(uri)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
zero := option
|
||||||
|
zero.DB = 0
|
||||||
|
rdb := redis.NewClient(zero)
|
||||||
|
defer rdb.Close()
|
||||||
|
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
myidx, _ := rdb.HGet(context.Background(), "private_db", hostname).Result()
|
myidx, _ := rdb.HGet(context.Background(), "private_db", hostname).Result()
|
||||||
if len(myidx) > 0 {
|
if len(myidx) > 0 {
|
||||||
devUrl.Path = "/" + myidx
|
offset, _ := strconv.Atoi(myidx)
|
||||||
return newRedisClient(devUrl.String(), dbidx), nil
|
option.DB += offset
|
||||||
|
return redis.NewClient(option), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
alldbs, err := rdb.HGetAll(context.Background(), "private_db").Result()
|
alldbs, err := rdb.HGetAll(context.Background(), "private_db").Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rdb.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +59,6 @@ func NewRedisClient(uri string, dbidx int) (*redis.Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
devUrl.Path = "/" + strconv.Itoa(newidx)
|
option.DB += newidx
|
||||||
return newRedisClient(devUrl.String(), dbidx), nil
|
return redis.NewClient(option), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,10 +65,7 @@ func LoadConfig[T any](outptr *T) error {
|
|||||||
|
|
||||||
type StorageAddr struct {
|
type StorageAddr struct {
|
||||||
Mongo string
|
Mongo string
|
||||||
Redis struct {
|
Redis map[string]string
|
||||||
URL string
|
|
||||||
Offset map[string]int
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionStorageConfig struct {
|
type RegionStorageConfig struct {
|
||||||
|
|||||||
@ -34,7 +34,7 @@ func TestRpc(t *testing.T) {
|
|||||||
RegistReceiver(&tr)
|
RegistReceiver(&tr)
|
||||||
myctx, cancel := context.WithCancel(context.Background())
|
myctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
redisClient, _ := gocommon.NewRedisClient("redis://192.168.8.94:6379", 0)
|
redisClient, _ := gocommon.NewRedisClient("redis://192.168.8.94:6379")
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
tr.TestFunc("aaaa", "bbbb", 333)
|
tr.TestFunc("aaaa", "bbbb", 333)
|
||||||
|
|||||||
@ -88,7 +88,6 @@ type Sender struct {
|
|||||||
type WebSocketMessageReceiver func(sender *Sender, messageType WebSocketMessageType, body io.Reader)
|
type WebSocketMessageReceiver func(sender *Sender, messageType WebSocketMessageType, body io.Reader)
|
||||||
|
|
||||||
type subhandler struct {
|
type subhandler struct {
|
||||||
authCache *gocommon.AuthCollection
|
|
||||||
redisMsgChanName string
|
redisMsgChanName string
|
||||||
redisCmdChanName string
|
redisCmdChanName string
|
||||||
redisSync *redis.Client
|
redisSync *redis.Client
|
||||||
@ -98,28 +97,26 @@ type subhandler struct {
|
|||||||
callReceiver WebSocketMessageReceiver
|
callReceiver WebSocketMessageReceiver
|
||||||
connWaitGroup sync.WaitGroup
|
connWaitGroup sync.WaitGroup
|
||||||
region string
|
region string
|
||||||
|
receiverChain []WebSocketMessageReceiver
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebsocketHandler :
|
// WebsocketHandler :
|
||||||
type WebsocketHandler struct {
|
type WebsocketHandler struct {
|
||||||
authCaches map[string]*subhandler
|
subhandlers map[string]*subhandler
|
||||||
RedisSync *redis.Client
|
|
||||||
receiverChain map[string][]WebSocketMessageReceiver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type wsConfig struct {
|
type wsConfig struct {
|
||||||
SyncPipeline string `json:"ws_sync_pipeline"`
|
gocommon.RegionStorageConfig
|
||||||
|
Maingate string `json:"maingate_service_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebsocketHandler(authglobal gocommon.AuthCollectionGlobal) (wsh *WebsocketHandler) {
|
|
||||||
var config wsConfig
|
var config wsConfig
|
||||||
gocommon.LoadConfig(&config)
|
|
||||||
|
|
||||||
redisSync, err := gocommon.NewRedisClient(config.SyncPipeline, 0)
|
func init() {
|
||||||
if err != nil {
|
gocommon.LoadConfig(&config)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewWebsocketHandler() (*WebsocketHandler, error) {
|
||||||
// decoder := func(r io.Reader) *T {
|
// decoder := func(r io.Reader) *T {
|
||||||
// if r == nil {
|
// if r == nil {
|
||||||
// // 접속이 끊겼을 때.
|
// // 접속이 끊겼을 때.
|
||||||
@ -135,10 +132,14 @@ func NewWebsocketHandler(authglobal gocommon.AuthCollectionGlobal) (wsh *Websock
|
|||||||
// return &m
|
// return &m
|
||||||
// }
|
// }
|
||||||
|
|
||||||
authCaches := make(map[string]*subhandler)
|
subhandlers := make(map[string]*subhandler)
|
||||||
for _, region := range authglobal.Regions() {
|
for region, cfg := range config.RegionStorage {
|
||||||
|
redisSync, err := gocommon.NewRedisClient(cfg.Redis["wshandler"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
sh := &subhandler{
|
sh := &subhandler{
|
||||||
authCache: authglobal.Get(region),
|
|
||||||
redisMsgChanName: fmt.Sprintf("_wsh_msg_%s", region),
|
redisMsgChanName: fmt.Sprintf("_wsh_msg_%s", region),
|
||||||
redisCmdChanName: fmt.Sprintf("_wsh_cmd_%s", region),
|
redisCmdChanName: fmt.Sprintf("_wsh_cmd_%s", region),
|
||||||
redisSync: redisSync,
|
redisSync: redisSync,
|
||||||
@ -148,23 +149,23 @@ func NewWebsocketHandler(authglobal gocommon.AuthCollectionGlobal) (wsh *Websock
|
|||||||
region: region,
|
region: region,
|
||||||
}
|
}
|
||||||
|
|
||||||
authCaches[region] = sh
|
subhandlers[region] = sh
|
||||||
}
|
}
|
||||||
|
|
||||||
return &WebsocketHandler{
|
return &WebsocketHandler{
|
||||||
authCaches: authCaches,
|
subhandlers: subhandlers,
|
||||||
RedisSync: redisSync,
|
}, nil
|
||||||
receiverChain: make(map[string][]WebSocketMessageReceiver),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) RegisterReceiver(region string, receiver WebSocketMessageReceiver) {
|
func (ws *WebsocketHandler) RegisterReceiver(region string, receiver WebSocketMessageReceiver) {
|
||||||
ws.receiverChain[region] = append(ws.receiverChain[region], receiver)
|
if sh := ws.subhandlers[region]; sh != nil {
|
||||||
|
sh.receiverChain = append(sh.receiverChain, receiver)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) Start(ctx context.Context) {
|
func (ws *WebsocketHandler) Start(ctx context.Context) {
|
||||||
for region, sh := range ws.authCaches {
|
for _, sh := range ws.subhandlers {
|
||||||
chain := ws.receiverChain[region]
|
chain := sh.receiverChain
|
||||||
if len(chain) == 0 {
|
if len(chain) == 0 {
|
||||||
sh.callReceiver = func(sender *Sender, messageType WebSocketMessageType, body io.Reader) {}
|
sh.callReceiver = func(sender *Sender, messageType WebSocketMessageType, body io.Reader) {}
|
||||||
} else if len(chain) == 1 {
|
} else if len(chain) == 1 {
|
||||||
@ -182,13 +183,13 @@ func (ws *WebsocketHandler) Start(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) Cleanup() {
|
func (ws *WebsocketHandler) Cleanup() {
|
||||||
for _, sh := range ws.authCaches {
|
for _, sh := range ws.subhandlers {
|
||||||
sh.connWaitGroup.Wait()
|
sh.connWaitGroup.Wait()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
func (ws *WebsocketHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
||||||
for region, sh := range ws.authCaches {
|
for region, sh := range ws.subhandlers {
|
||||||
if region == "default" {
|
if region == "default" {
|
||||||
region = ""
|
region = ""
|
||||||
}
|
}
|
||||||
@ -204,26 +205,28 @@ func (ws *WebsocketHandler) RegisterHandlers(serveMux *http.ServeMux, prefix str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) GetState(region string, accid primitive.ObjectID) string {
|
func (ws *WebsocketHandler) GetState(region string, accid primitive.ObjectID) string {
|
||||||
state, err := ws.RedisSync.Get(context.Background(), accid.Hex()).Result()
|
if sh := ws.subhandlers[region]; sh != nil {
|
||||||
if err == redis.Nil {
|
state, _ := sh.redisSync.Get(context.Background(), accid.Hex()).Result()
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) SetState(region string, accid primitive.ObjectID, state string) {
|
func (ws *WebsocketHandler) SetState(region string, accid primitive.ObjectID, state string) {
|
||||||
ws.RedisSync.SetArgs(context.Background(), accid.Hex(), state, redis.SetArgs{Mode: "XX"}).Result()
|
if sh := ws.subhandlers[region]; sh != nil {
|
||||||
|
sh.redisSync.SetArgs(context.Background(), accid.Hex(), state, redis.SetArgs{Mode: "XX"}).Result()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) SendUpstreamMessage(region string, msg *UpstreamMessage) {
|
func (ws *WebsocketHandler) SendUpstreamMessage(region string, msg *UpstreamMessage) {
|
||||||
sh := ws.authCaches[region]
|
sh := ws.subhandlers[region]
|
||||||
if sh != nil {
|
if sh != nil {
|
||||||
sh.localDeliveryChan <- msg
|
sh.localDeliveryChan <- msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) EnterRoom(region string, room string, accid primitive.ObjectID) {
|
func (ws *WebsocketHandler) EnterRoom(region string, room string, accid primitive.ObjectID) {
|
||||||
sh := ws.authCaches[region]
|
sh := ws.subhandlers[region]
|
||||||
if sh != nil {
|
if sh != nil {
|
||||||
sh.localDeliveryChan <- &commandMessage{
|
sh.localDeliveryChan <- &commandMessage{
|
||||||
Cmd: commandType_JoinRoom,
|
Cmd: commandType_JoinRoom,
|
||||||
@ -233,7 +236,7 @@ func (ws *WebsocketHandler) EnterRoom(region string, room string, accid primitiv
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) LeaveRoom(region string, room string, accid primitive.ObjectID) {
|
func (ws *WebsocketHandler) LeaveRoom(region string, room string, accid primitive.ObjectID) {
|
||||||
sh := ws.authCaches[region]
|
sh := ws.subhandlers[region]
|
||||||
if sh != nil {
|
if sh != nil {
|
||||||
sh.localDeliveryChan <- &commandMessage{
|
sh.localDeliveryChan <- &commandMessage{
|
||||||
Cmd: commandType_LeaveRoom,
|
Cmd: commandType_LeaveRoom,
|
||||||
@ -529,17 +532,25 @@ func (sh *subhandler) upgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
sk := r.Header.Get("AS-X-SESSION")
|
sk := r.Header.Get("AS-X-SESSION")
|
||||||
auth := strings.Split(r.Header.Get("Authorization"), " ")
|
auth := r.Header.Get("Authorization")
|
||||||
if len(auth) != 2 {
|
|
||||||
//TODO : 클라이언트는 BadRequest를 받으면 로그인 화면으로 돌아가야 한다.
|
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/query?sk=%s", config.Maingate, sk), nil)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
req.Header.Add("Authorization", auth)
|
||||||
|
|
||||||
|
client := http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
logger.Error("authorize query failed :", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
authtoken := auth[1]
|
defer resp.Body.Close()
|
||||||
|
|
||||||
accid, success := sh.authCache.IsValid(sk, authtoken)
|
var authinfo gocommon.Authinfo
|
||||||
if !success {
|
dec := json.NewDecoder(resp.Body)
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
if err = dec.Decode(&authinfo); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
logger.Error("authorize query failed :", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -554,8 +565,8 @@ func (sh *subhandler) upgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 {
|
if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 {
|
||||||
alias = v
|
alias = v
|
||||||
} else {
|
} else {
|
||||||
alias = accid.Hex()
|
alias = authinfo.Accid.Hex()
|
||||||
}
|
}
|
||||||
|
|
||||||
upgrade_core(sh, conn, accid, alias)
|
upgrade_core(sh, conn, authinfo.Accid, alias)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user