코드 정리 및 websocket도 http와 비슷하게 api handler로 통일
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user