코드 정리 및 websocket도 http와 비슷하게 api handler로 통일

This commit is contained in:
2023-09-08 11:35:57 +09:00
parent eb54fa2e3a
commit 6cbf32c386
10 changed files with 216 additions and 130 deletions

View File

@ -10,12 +10,6 @@ import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon"
)
const (
communication_channel_name_prefix = "_sess_comm_chan_name"
session_collection_name = gocommon.CollectionName("session")
)
type Authorization struct {

View File

@ -33,13 +33,6 @@ func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
delete(c.stages[1].deleted, sk)
}
func (c *consumer_common[T]) add(sk storagekey, si T) {
c.lock.Lock()
defer c.lock.Unlock()
c.add_internal(sk, si)
}
func (c *consumer_common[T]) delete_internal(sk storagekey) {
delete(c.stages[0].cache, sk)
c.stages[0].deleted[sk] = true

View File

@ -12,6 +12,10 @@ import (
"repositories.action2quare.com/ayo/gocommon/logger"
)
const (
session_collection_name = gocommon.CollectionName("session")
)
type provider_mongo struct {
mongoClient gocommon.MongoClient
}

View File

@ -2,6 +2,7 @@ package session
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v8"
@ -11,6 +12,10 @@ import (
"repositories.action2quare.com/ayo/gocommon/logger"
)
const (
communication_channel_name_prefix = "_sess_comm_chan_name"
)
type sessionRedis struct {
*Authorization
expireAt time.Time
@ -18,7 +23,6 @@ type sessionRedis struct {
type provider_redis struct {
redisClient *redis.Client
updateChannel string
deleteChannel string
ttl time.Duration
ctx context.Context
@ -32,8 +36,7 @@ func newProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
return &provider_redis{
redisClient: redisClient,
updateChannel: communication_channel_name_prefix + "_u",
deleteChannel: communication_channel_name_prefix + "_d",
deleteChannel: fmt.Sprintf("%s_%d", communication_channel_name_prefix, redisClient.Options().DB),
ttl: ttl,
ctx: ctx,
}, nil
@ -51,7 +54,6 @@ func (p *provider_redis) New(input *Authorization) (string, error) {
return "", err
}
_, err = p.redisClient.Publish(p.ctx, p.updateChannel, string(sk)).Result()
return string(storagekey_to_publickey(sk)), err
}
@ -122,9 +124,8 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
redisClient: redisClient,
}
updateChannel := communication_channel_name_prefix + "_u"
deleteChannel := communication_channel_name_prefix + "_d"
sub := redisClient.Subscribe(ctx, updateChannel, deleteChannel)
sub := redisClient.Subscribe(ctx, deleteChannel)
go func() {
stageswitch := time.Now().Add(ttl)
@ -151,20 +152,6 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
}
switch msg.Channel {
case updateChannel:
sk := storagekey(msg.Payload)
raw, err := redisClient.Get(ctx, string(sk)).Result()
if err != nil {
logger.Println(err)
} else if len(raw) > 0 {
var si Authorization
if bson.Unmarshal([]byte(raw), &si) == nil {
consumer.add(sk, &sessionRedis{
Authorization: &si,
expireAt: time.Now().Add(consumer.ttl),
})
}
}
case deleteChannel:
sk := storagekey(msg.Payload)
consumer.delete(sk)