Squashed commit of the following:
commit29b2f25850Author: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:33:37 2023 +0900 타입 이름 변경 commit256bfd030cAuthor: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:31:01 2023 +0900 redison 추가 commit72a683fed2Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 19:51:24 2023 +0900 gob에 []any 추가 commit89fa9e4ac5Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:45:12 2023 +0900 write control 수정 commitd724cc84faAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:38:04 2023 +0900 redis pubsub 채널 이름에 디비 인덱스 추가 commit8df248fa54Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:20:47 2023 +0900 close를 writecontrol로 변경 commit40a603522dAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 12:21:06 2023 +0900 conn에 msg를 쓰는 쓰레드 단일화 commitc21017d2cdAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:08:38 2023 +0900 redis call이 문제가 아니었음 commit82abcddb49Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:04:15 2023 +0900 잦은 redis call 회피 commit289af24a8fAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:55:18 2023 +0900 room create 메시지 전송 commit4b35e0e638Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:45:27 2023 +0900 EventReceiver 인터페이스 추가 commit29843802ffAuthor: mountain <mountain@action2quare.com> Date: Mon Jul 17 17:45:40 2023 +0900 gob 등록 commit66aea48fb7Author: mountain <mountain@action2quare.com> Date: Sun Jul 16 18:39:11 2023 +0900 채널간 publish marshalling을 gob으로 변경 commit8f6c87a8aeAuthor: mountain <mountain@action2quare.com> Date: Sun Jul 16 16:37:02 2023 +0900 redis option을 copy로 변경 commitf0f459332dAuthor: mountain <mountain@action2quare.com> Date: Sat Jul 15 17:08:33 2023 +0900 wshandler에서 authcache제거하고 config 포맷 변경
This commit is contained in:
@ -30,7 +30,7 @@ type Authinfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sessionSyncChannelName = "session-sync-channel2"
|
sessionSyncChannelNamePrefix = "session-sync-channel2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthinfoCell interface {
|
type AuthinfoCell interface {
|
||||||
@ -99,6 +99,8 @@ func (ac *redisAuthCell) ToBytes() []byte {
|
|||||||
func newAuthCollectionWithRedis(redisClient *redis.Client, subctx context.Context, maingateURL string, apiToken string) *AuthCollection {
|
func newAuthCollectionWithRedis(redisClient *redis.Client, subctx context.Context, maingateURL string, apiToken string) *AuthCollection {
|
||||||
sessionTTL := int64(3600)
|
sessionTTL := int64(3600)
|
||||||
ac := MakeAuthCollection(time.Duration(sessionTTL * int64(time.Second)))
|
ac := MakeAuthCollection(time.Duration(sessionTTL * int64(time.Second)))
|
||||||
|
|
||||||
|
sessionSyncChannelName := fmt.Sprintf("%s-%d", sessionSyncChannelNamePrefix, redisClient.Options().DB)
|
||||||
pubsub := redisClient.Subscribe(subctx, sessionSyncChannelName)
|
pubsub := redisClient.Subscribe(subctx, sessionSyncChannelName)
|
||||||
ctx, cancel := context.WithCancel(context.TODO())
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
go func(ctx context.Context, sub *redis.PubSub, authCache *AuthCollection) {
|
go func(ctx context.Context, sub *redis.PubSub, authCache *AuthCollection) {
|
||||||
@ -205,7 +207,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 +230,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
|
||||||
}
|
}
|
||||||
|
|||||||
412
redis.go
412
redis.go
@ -2,9 +2,11 @@ package gocommon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
@ -19,23 +21,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 +62,389 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
type RedisonSetOption = string
|
||||||
|
type RedisonGetOption = [2]any
|
||||||
|
|
||||||
|
const (
|
||||||
|
// JSONSET command Options
|
||||||
|
RedisonSetOptionNX RedisonSetOption = "NX"
|
||||||
|
RedisonSetOptionXX RedisonSetOption = "XX"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
RedisonGetOptionSPACE = RedisonGetOption{"SPACE", " "}
|
||||||
|
RedisonGetOptionINDENT = RedisonGetOption{"INDENT", "\t"}
|
||||||
|
RedisonGetOptionNEWLINE = RedisonGetOption{"NEWLINE", "\n"}
|
||||||
|
RedisonGetOptionNOESCAPE = RedisonGetOption{"NOESCAPE", ""}
|
||||||
|
)
|
||||||
|
|
||||||
|
// gocommon으로 옮길 거
|
||||||
|
type RedisonHandler struct {
|
||||||
|
*redis.Client
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedisonHandler(ctx context.Context, redisClient *redis.Client) *RedisonHandler {
|
||||||
|
return &RedisonHandler{
|
||||||
|
Client: redisClient,
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func respToArray[T any](resp any, err error) ([]T, error) {
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resArr := resp.([]any)
|
||||||
|
v := make([]T, len(resArr))
|
||||||
|
for i, e := range resArr {
|
||||||
|
v[i] = e.(T)
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendArgs[T any](args []any, ext ...T) []any {
|
||||||
|
for _, e := range ext {
|
||||||
|
args = append(args, e)
|
||||||
|
}
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONMSetRel(key string, prefixPath string, kv map[string]any) error {
|
||||||
|
if len(prefixPath) > 0 && !strings.HasSuffix(prefixPath, ".") {
|
||||||
|
prefixPath += "."
|
||||||
|
}
|
||||||
|
|
||||||
|
pl := rh.Pipeline()
|
||||||
|
for path, obj := range kv {
|
||||||
|
b, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pl.Do(rh.ctx, "JSON.SET", key, prefixPath+path, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmders, err := pl.Exec(rh.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cmder := range cmders {
|
||||||
|
if cmder.Err() != nil {
|
||||||
|
return cmder.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONMSet(key string, kv map[string]any) error {
|
||||||
|
return rh.JSONMSetRel(key, "", kv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) jsonSetMergeJSONSet(cmd, key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||||
|
b, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []any{
|
||||||
|
"JSON.SET",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
b,
|
||||||
|
}
|
||||||
|
if len(opts) > 0 {
|
||||||
|
args = append(args, opts[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.(string) == "OK", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONSet(key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||||
|
return rh.jsonSetMergeJSONSet("JSON.SET", key, path, obj, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONMerge(key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||||
|
return rh.jsonSetMergeJSONSet("JSON.MERGE", key, path, obj, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONGet(key, path string, opts ...RedisonGetOption) (res any, err error) {
|
||||||
|
args := appendArgs[string]([]any{
|
||||||
|
"JSON.GET",
|
||||||
|
key,
|
||||||
|
}, strings.Split(path, " ")...)
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
args = append(args, opt[:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rh.Do(rh.ctx, args...).Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONGetString(key, path string) ([]string, error) {
|
||||||
|
return respToArray[string](rh.JSONResp(key, path))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONGetInt64(key, path string) ([]int64, error) {
|
||||||
|
return respToArray[int64](rh.JSONResp(key, path))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONMGet(path string, keys ...string) (res any, err error) {
|
||||||
|
args := appendArgs[string]([]any{
|
||||||
|
"JSON.MGET",
|
||||||
|
path,
|
||||||
|
}, keys...)
|
||||||
|
return rh.Do(rh.ctx, args...).Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONMDel(key string, paths []string) error {
|
||||||
|
pl := rh.Pipeline()
|
||||||
|
for _, path := range paths {
|
||||||
|
args := []any{
|
||||||
|
"JSON.DEL",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
pl.Do(rh.ctx, args...)
|
||||||
|
}
|
||||||
|
_, err := pl.Exec(rh.ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONDel(key, path string) (int64, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.DEL",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.(int64), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONType(key, path string) ([]string, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.TYPE",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
return respToArray[string](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONNumIncrBy(key, path string, number int) ([]any, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.NUMINCRBY",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
number,
|
||||||
|
}
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.([]any), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONNumMultBy(key, path string, number int) (res any, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.NUMMULTBY",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
number,
|
||||||
|
}
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.([]any), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONStrAppend(key, path string, jsonstring string) ([]int64, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.STRAPPEND",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
fmt.Sprintf(`'"%s"'`, jsonstring),
|
||||||
|
}
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONStrLen(key, path string) (res []int64, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.STRLEN",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrAppend(key, path string, values ...any) (int64, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.ARRAPPEND",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.(int64), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrLen(key, path string) ([]int64, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.ARRLEN",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrPop(key, path string, index int) (res any, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.ARRPOP",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
index,
|
||||||
|
}
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp.([]any)[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendValues(args []any, values ...any) []any {
|
||||||
|
for _, jsonValue := range values {
|
||||||
|
switch jsonValue := jsonValue.(type) {
|
||||||
|
case string:
|
||||||
|
args = append(args, fmt.Sprintf(`'"%s"'`, jsonValue))
|
||||||
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||||
|
args = append(args, jsonValue)
|
||||||
|
default:
|
||||||
|
bt, _ := json.Marshal(jsonValue)
|
||||||
|
args = append(args, bt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrIndex(key, path string, jsonValue any, optionalRange ...int) ([]int64, error) {
|
||||||
|
args := appendValues([]any{
|
||||||
|
"JSON.ARRINDEX",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}, jsonValue)
|
||||||
|
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrTrim(key, path string, start, end int) (res any, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.ARRTRIM",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONArrInsert(key, path string, index int, values ...any) (res any, err error) {
|
||||||
|
args := appendValues([]any{
|
||||||
|
"JSON.ARRINSERT",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
index,
|
||||||
|
}, values...)
|
||||||
|
|
||||||
|
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONObjKeys(key, path string) ([]string, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.OBJKEYS",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resArr := res.([]any)
|
||||||
|
resArr = resArr[0].([]any)
|
||||||
|
slc := make([]string, len(resArr))
|
||||||
|
|
||||||
|
for i, r := range resArr {
|
||||||
|
slc[i] = r.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return slc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONObjLen(key, path string) ([]int64, error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.OBJLEN",
|
||||||
|
key,
|
||||||
|
}
|
||||||
|
|
||||||
|
if path != "$" {
|
||||||
|
args = append(args, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch resp := resp.(type) {
|
||||||
|
case []any:
|
||||||
|
return respToArray[int64](resp, nil)
|
||||||
|
|
||||||
|
case int64:
|
||||||
|
return []int64{resp}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return []int64{0}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONDebug(key, path string) (res any, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.DEBUG",
|
||||||
|
"MEMORY",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
return rh.Do(rh.ctx, args...).Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONForget(key, path string) (int64, error) {
|
||||||
|
return rh.JSONDel(key, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rh *RedisonHandler) JSONResp(key, path string) (res any, err error) {
|
||||||
|
args := []any{
|
||||||
|
"JSON.RESP",
|
||||||
|
key,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
return rh.Do(rh.ctx, args...).Result()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
@ -68,6 +68,7 @@ func Start(ctx context.Context, redisClient *redis.Client) {
|
|||||||
hash.Write([]byte(inName))
|
hash.Write([]byte(inName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
hash.Write([]byte(fmt.Sprintf("%d", redisClient.Options().DB)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pubsubName := hex.EncodeToString(hash.Sum(nil))[:16]
|
pubsubName := hex.EncodeToString(hash.Sum(nil))[:16]
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -14,16 +14,18 @@ type room struct {
|
|||||||
messageChan chan *UpstreamMessage
|
messageChan chan *UpstreamMessage
|
||||||
name string
|
name string
|
||||||
destroyChan chan<- string
|
destroyChan chan<- string
|
||||||
|
sendMsgChan chan<- send_msg_queue_elem
|
||||||
}
|
}
|
||||||
|
|
||||||
// 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room
|
// 만약 destroyChan가 nil이면 room이 비어도 파괴되지 않는다. 영구 유지되는 room
|
||||||
func makeRoom(name string, destroyChan chan<- string) *room {
|
func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room {
|
||||||
return &room{
|
return &room{
|
||||||
inChan: make(chan *wsconn, 10),
|
inChan: make(chan *wsconn, 10),
|
||||||
outChan: make(chan *wsconn, 10),
|
outChan: make(chan *wsconn, 10),
|
||||||
messageChan: make(chan *UpstreamMessage, 100),
|
messageChan: make(chan *UpstreamMessage, 100),
|
||||||
name: name,
|
name: name,
|
||||||
destroyChan: destroyChan,
|
destroyChan: destroyChan,
|
||||||
|
sendMsgChan: sendMsgChan,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +87,11 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
|
|||||||
bt, _ := json.Marshal(ds)
|
bt, _ := json.Marshal(ds)
|
||||||
|
|
||||||
for _, conn := range *conns {
|
for _, conn := range *conns {
|
||||||
conn.Conn.WriteMessage(websocket.TextMessage, bt)
|
r.sendMsgChan <- send_msg_queue_elem{
|
||||||
|
to: conn,
|
||||||
|
mt: websocket.TextMessage,
|
||||||
|
msg: bt,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
package wshandler
|
package wshandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/gob"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -9,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"repositories.action2quare.com/ayo/gocommon"
|
"repositories.action2quare.com/ayo/gocommon"
|
||||||
@ -85,41 +88,56 @@ type Sender struct {
|
|||||||
Alias string
|
Alias string
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebSocketMessageReceiver func(sender *Sender, messageType WebSocketMessageType, body io.Reader)
|
type EventReceiver interface {
|
||||||
|
OnClientMessageReceived(sender *Sender, messageType WebSocketMessageType, body io.Reader)
|
||||||
|
OnRoomCreated(region, name string)
|
||||||
|
OnRoomDestroyed(region, name string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type send_msg_queue_elem struct {
|
||||||
|
to *wsconn
|
||||||
|
mt int
|
||||||
|
msg []byte
|
||||||
|
}
|
||||||
|
|
||||||
type subhandler struct {
|
type subhandler struct {
|
||||||
authCache *gocommon.AuthCollection
|
|
||||||
redisMsgChanName string
|
redisMsgChanName string
|
||||||
redisCmdChanName string
|
redisCmdChanName string
|
||||||
redisSync *redis.Client
|
redisSync *redis.Client
|
||||||
connInOutChan chan *wsconn
|
connInOutChan chan *wsconn
|
||||||
deliveryChan chan any
|
deliveryChan chan any
|
||||||
localDeliveryChan chan any
|
localDeliveryChan chan any
|
||||||
callReceiver WebSocketMessageReceiver
|
sendMsgChan chan send_msg_queue_elem
|
||||||
|
callReceiver EventReceiver
|
||||||
connWaitGroup sync.WaitGroup
|
connWaitGroup sync.WaitGroup
|
||||||
region string
|
region string
|
||||||
|
receiverChain []EventReceiver
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
|
func init() {
|
||||||
gocommon.LoadConfig(&config)
|
gocommon.LoadConfig(&config)
|
||||||
|
gob.Register(UpstreamMessage{})
|
||||||
|
gob.Register(commandMessage{})
|
||||||
|
gob.Register(map[string]any{})
|
||||||
|
gob.Register(primitive.A{})
|
||||||
|
gob.Register(primitive.M{})
|
||||||
|
gob.Register(primitive.ObjectID{})
|
||||||
|
gob.Register([]any{})
|
||||||
|
}
|
||||||
|
|
||||||
redisSync, err := gocommon.NewRedisClient(config.SyncPipeline, 0)
|
func NewWebsocketHandler() (*WebsocketHandler, error) {
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// decoder := func(r io.Reader) *T {
|
// decoder := func(r io.Reader) *T {
|
||||||
// if r == nil {
|
// if r == nil {
|
||||||
// // 접속이 끊겼을 때.
|
// // 접속이 끊겼을 때.
|
||||||
@ -135,46 +153,93 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
sendchan := make(chan send_msg_queue_elem, 1000)
|
||||||
|
go func() {
|
||||||
|
sender := func(elem *send_msg_queue_elem) {
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
if r != nil {
|
||||||
|
logger.Println(r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
elem.to.WriteMessage(elem.mt, elem.msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
for elem := range sendchan {
|
||||||
|
sender(&elem)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
sh := &subhandler{
|
sh := &subhandler{
|
||||||
authCache: authglobal.Get(region),
|
redisMsgChanName: fmt.Sprintf("_wsh_msg_%s_%d", region, redisSync.Options().DB),
|
||||||
redisMsgChanName: fmt.Sprintf("_wsh_msg_%s", region),
|
redisCmdChanName: fmt.Sprintf("_wsh_cmd_%s_%d", region, redisSync.Options().DB),
|
||||||
redisCmdChanName: fmt.Sprintf("_wsh_cmd_%s", region),
|
|
||||||
redisSync: redisSync,
|
redisSync: redisSync,
|
||||||
connInOutChan: make(chan *wsconn),
|
connInOutChan: make(chan *wsconn),
|
||||||
deliveryChan: make(chan any, 1000),
|
deliveryChan: make(chan any, 1000),
|
||||||
localDeliveryChan: make(chan any, 100),
|
localDeliveryChan: make(chan any, 100),
|
||||||
|
sendMsgChan: sendchan,
|
||||||
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 EventReceiver) {
|
||||||
|
if sh := ws.subhandlers[region]; sh != nil {
|
||||||
|
sh.receiverChain = append(sh.receiverChain, receiver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebsocketHandler) RegisterReceiver(region string, receiver WebSocketMessageReceiver) {
|
type nilReceiver struct{}
|
||||||
ws.receiverChain[region] = append(ws.receiverChain[region], receiver)
|
|
||||||
|
func (r *nilReceiver) OnClientMessageReceived(sender *Sender, messageType WebSocketMessageType, body io.Reader) {
|
||||||
|
}
|
||||||
|
func (r *nilReceiver) OnRoomCreated(region, name string) {}
|
||||||
|
func (r *nilReceiver) OnRoomDestroyed(region, name string) {}
|
||||||
|
|
||||||
|
type chainReceiver struct {
|
||||||
|
chain []EventReceiver
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chainReceiver) OnClientMessageReceived(sender *Sender, messageType WebSocketMessageType, body io.Reader) {
|
||||||
|
for _, cr := range r.chain {
|
||||||
|
cr.OnClientMessageReceived(sender, messageType, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chainReceiver) OnRoomCreated(region, name string) {
|
||||||
|
for _, cr := range r.chain {
|
||||||
|
cr.OnRoomCreated(region, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chainReceiver) OnRoomDestroyed(region, name string) {
|
||||||
|
for _, cr := range r.chain {
|
||||||
|
cr.OnRoomDestroyed(region, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = &nilReceiver{}
|
||||||
} else if len(chain) == 1 {
|
} else if len(chain) == 1 {
|
||||||
sh.callReceiver = chain[0]
|
sh.callReceiver = chain[0]
|
||||||
} else {
|
} else {
|
||||||
sh.callReceiver = func(sender *Sender, messageType WebSocketMessageType, body io.Reader) {
|
sh.callReceiver = &chainReceiver{chain: sh.receiverChain}
|
||||||
for _, r := range chain {
|
|
||||||
r(sender, messageType, body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go sh.mainLoop(ctx)
|
go sh.mainLoop(ctx)
|
||||||
@ -182,13 +247,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 +269,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 +300,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,
|
||||||
@ -260,16 +327,19 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
|
|
||||||
raw, err := pubsub.ReceiveMessage(ctx)
|
raw, err := pubsub.ReceiveMessage(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
buffer := bytes.NewBuffer([]byte(raw.Payload))
|
||||||
|
dec := gob.NewDecoder(buffer)
|
||||||
|
|
||||||
if raw.Channel == sh.redisMsgChanName {
|
if raw.Channel == sh.redisMsgChanName {
|
||||||
var msg UpstreamMessage
|
var msg UpstreamMessage
|
||||||
if err := json.Unmarshal([]byte(raw.Payload), &msg); err == nil {
|
if err := dec.Decode(&msg); err == nil {
|
||||||
sh.deliveryChan <- &msg
|
sh.deliveryChan <- &msg
|
||||||
} else {
|
} else {
|
||||||
logger.Println("decode UpstreamMessage failed :", err)
|
logger.Println("decode UpstreamMessage failed :", err)
|
||||||
}
|
}
|
||||||
} else if raw.Channel == sh.redisCmdChanName {
|
} else if raw.Channel == sh.redisCmdChanName {
|
||||||
var cmd commandMessage
|
var cmd commandMessage
|
||||||
if err := json.Unmarshal([]byte(raw.Payload), &cmd); err == nil {
|
if err := dec.Decode(&cmd); err == nil {
|
||||||
sh.deliveryChan <- &cmd
|
sh.deliveryChan <- &cmd
|
||||||
} else {
|
} else {
|
||||||
logger.Println("decode UpstreamMessage failed :", err)
|
logger.Println("decode UpstreamMessage failed :", err)
|
||||||
@ -293,18 +363,23 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
findRoom := func(name string, create bool) *room {
|
findRoom := func(name string, create bool) *room {
|
||||||
room := rooms[name]
|
room := rooms[name]
|
||||||
if room == nil && create {
|
if room == nil && create {
|
||||||
room = makeRoom(name, roomDestroyChan)
|
room = makeRoom(name, roomDestroyChan, sh.sendMsgChan)
|
||||||
rooms[name] = room
|
rooms[name] = room
|
||||||
room.start(ctx)
|
room.start(ctx)
|
||||||
|
go sh.callReceiver.OnRoomCreated(sh.region, name)
|
||||||
}
|
}
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
|
|
||||||
// 유저에게서 온 메세지, 소켓 연결/해체 처리
|
// 유저에게서 온 메세지, 소켓 연결/해체 처리
|
||||||
for {
|
for {
|
||||||
|
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
|
buffer.Reset()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case destroyedRoom := <-roomDestroyChan:
|
case destroyedRoom := <-roomDestroyChan:
|
||||||
delete(rooms, destroyedRoom)
|
delete(rooms, destroyedRoom)
|
||||||
|
go sh.callReceiver.OnRoomDestroyed(sh.region, destroyedRoom)
|
||||||
|
|
||||||
case usermsg := <-sh.localDeliveryChan:
|
case usermsg := <-sh.localDeliveryChan:
|
||||||
// 로컬에 connection이 있는지 먼저 확인해 보기 위한 채널
|
// 로컬에 connection이 있는지 먼저 확인해 보기 위한 채널
|
||||||
@ -322,13 +397,23 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
Body: usermsg.Body,
|
Body: usermsg.Body,
|
||||||
Tag: usermsg.Tag,
|
Tag: usermsg.Tag,
|
||||||
})
|
})
|
||||||
|
sh.sendMsgChan <- send_msg_queue_elem{
|
||||||
conn.WriteMessage(websocket.TextMessage, ds)
|
to: conn,
|
||||||
|
mt: websocket.TextMessage,
|
||||||
|
msg: ds,
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if bt, err := json.Marshal(usermsg); err == nil {
|
|
||||||
sh.redisSync.Publish(context.Background(), sh.redisMsgChanName, bt).Result()
|
var err error
|
||||||
|
enc := gob.NewEncoder(buffer)
|
||||||
|
if err = enc.Encode(usermsg); err == nil {
|
||||||
|
_, err = sh.redisSync.Publish(context.Background(), sh.redisMsgChanName, buffer.Bytes()).Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Println("gob.Encode or publish failed :", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
case *commandMessage:
|
case *commandMessage:
|
||||||
@ -347,7 +432,8 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
if conn != nil {
|
if conn != nil {
|
||||||
if room := findRoom(roomName, false); room != nil {
|
if room := findRoom(roomName, false); room != nil {
|
||||||
if conn.popRoom(room.out(conn)) == 0 {
|
if conn.popRoom(room.out(conn)) == 0 {
|
||||||
conn.Close()
|
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
|
||||||
|
conn.WriteControl(websocket.CloseMessage, closeMsg, time.Time{})
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -355,8 +441,13 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 위에서 break 안걸리면 나한테 없으므로 publish를 해야 함. 그러면 다른 호스트가 deliveryChan으로 받는다
|
// 위에서 break 안걸리면 나한테 없으므로 publish를 해야 함. 그러면 다른 호스트가 deliveryChan으로 받는다
|
||||||
if bt, err := json.Marshal(usermsg); err == nil {
|
var err error
|
||||||
sh.redisSync.Publish(context.Background(), sh.redisCmdChanName, bt).Result()
|
enc := gob.NewEncoder(buffer)
|
||||||
|
if err = enc.Encode(usermsg); err == nil {
|
||||||
|
_, err = sh.redisSync.Publish(context.Background(), sh.redisCmdChanName, buffer.Bytes()).Result()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logger.Println("gob.Encode or Publish failed :", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,7 +470,11 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
Body: usermsg.Body,
|
Body: usermsg.Body,
|
||||||
Tag: usermsg.Tag,
|
Tag: usermsg.Tag,
|
||||||
})
|
})
|
||||||
conn.WriteMessage(websocket.TextMessage, ds)
|
sh.sendMsgChan <- send_msg_queue_elem{
|
||||||
|
to: conn,
|
||||||
|
mt: websocket.TextMessage,
|
||||||
|
msg: ds,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -398,7 +493,8 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
if conn != nil {
|
if conn != nil {
|
||||||
if room := findRoom(roomName, false); room != nil {
|
if room := findRoom(roomName, false); room != nil {
|
||||||
if conn.popRoom(room.out(conn)) == 0 {
|
if conn.popRoom(room.out(conn)) == 0 {
|
||||||
conn.Close()
|
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
|
||||||
|
conn.WriteControl(websocket.CloseMessage, closeMsg, time.Time{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -415,10 +511,10 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
|
|||||||
room.out(c)
|
room.out(c)
|
||||||
}
|
}
|
||||||
c.joinedRooms = nil
|
c.joinedRooms = nil
|
||||||
sh.callReceiver(c.sender, Disconnected, nil)
|
go sh.callReceiver.OnClientMessageReceived(c.sender, Disconnected, nil)
|
||||||
} else {
|
} else {
|
||||||
entireConns[c.sender.Accid.Hex()] = c
|
entireConns[c.sender.Accid.Hex()] = c
|
||||||
sh.callReceiver(c.sender, Connected, nil)
|
go sh.callReceiver.OnClientMessageReceived(c.sender, Connected, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -446,15 +542,15 @@ func upgrade_core(sh *subhandler, conn *websocket.Conn, accid primitive.ObjectID
|
|||||||
}
|
}
|
||||||
|
|
||||||
if messageType == websocket.CloseMessage {
|
if messageType == websocket.CloseMessage {
|
||||||
sh.callReceiver(c.sender, CloseMessage, r)
|
sh.callReceiver.OnClientMessageReceived(c.sender, CloseMessage, r)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if messageType == websocket.TextMessage {
|
if messageType == websocket.TextMessage {
|
||||||
// 유저가 직접 보낸 메시지
|
// 유저가 직접 보낸 메시지
|
||||||
sh.callReceiver(c.sender, TextMessage, r)
|
sh.callReceiver.OnClientMessageReceived(c.sender, TextMessage, r)
|
||||||
} else if messageType == websocket.BinaryMessage {
|
} else if messageType == websocket.BinaryMessage {
|
||||||
sh.callReceiver(c.sender, BinaryMessage, r)
|
sh.callReceiver.OnClientMessageReceived(c.sender, BinaryMessage, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sh.redisSync.Del(context.Background(), accid.Hex())
|
sh.redisSync.Del(context.Background(), accid.Hex())
|
||||||
@ -529,17 +625,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 +658,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