2023-08-30 13:15:44 +09:00
|
|
|
package session
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2023-08-31 14:33:08 +09:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2023-08-30 13:15:44 +09:00
|
|
|
"repositories.action2quare.com/ayo/gocommon"
|
|
|
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-30 15:43:26 +09:00
|
|
|
type sessionRedis struct {
|
|
|
|
|
*Authorization
|
2023-08-30 13:15:44 +09:00
|
|
|
expireAt time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
type provider_redis struct {
|
|
|
|
|
redisClient *redis.Client
|
|
|
|
|
updateChannel string
|
|
|
|
|
deleteChannel string
|
|
|
|
|
ttl time.Duration
|
|
|
|
|
ctx context.Context
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 20:39:00 +09:00
|
|
|
func newProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Provider, error) {
|
2023-08-31 14:33:08 +09:00
|
|
|
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &provider_redis{
|
|
|
|
|
redisClient: redisClient,
|
|
|
|
|
updateChannel: communication_channel_name_prefix + "_u",
|
|
|
|
|
deleteChannel: communication_channel_name_prefix + "_d",
|
|
|
|
|
ttl: ttl,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *provider_redis) New(input *Authorization) (string, error) {
|
|
|
|
|
bt, err := bson.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sk := make_storagekey(input.Account)
|
|
|
|
|
_, err = p.redisClient.SetEX(p.ctx, string(sk), bt, p.ttl).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = p.redisClient.Publish(p.ctx, p.updateChannel, string(sk)).Result()
|
|
|
|
|
return string(storagekey_to_publickey(sk)), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *provider_redis) Delete(account primitive.ObjectID) error {
|
|
|
|
|
prefix := account.Hex()
|
|
|
|
|
sks, err := p.redisClient.Keys(p.ctx, prefix+"*").Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, sk := range sks {
|
|
|
|
|
p.redisClient.Del(p.ctx, sk).Result()
|
|
|
|
|
p.redisClient.Publish(p.ctx, p.deleteChannel, sk).Result()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *provider_redis) Query(pk string) (*Authorization, error) {
|
|
|
|
|
sk := publickey_to_storagekey(publickey(pk))
|
|
|
|
|
payload, err := p.redisClient.Get(p.ctx, string(sk)).Result()
|
|
|
|
|
if err == redis.Nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var auth Authorization
|
|
|
|
|
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &auth, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 15:43:26 +09:00
|
|
|
type consumer_redis struct {
|
2023-08-30 18:23:19 +09:00
|
|
|
consumer_common[*sessionRedis]
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient *redis.Client
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 20:39:00 +09:00
|
|
|
func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Consumer, error) {
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 15:43:26 +09:00
|
|
|
consumer := &consumer_redis{
|
2023-08-30 18:23:19 +09:00
|
|
|
consumer_common: consumer_common[*sessionRedis]{
|
2023-08-30 15:43:26 +09:00
|
|
|
ttl: ttl,
|
|
|
|
|
ctx: ctx,
|
2023-08-30 18:23:19 +09:00
|
|
|
stages: [2]*cache_stage[*sessionRedis]{make_cache_stage[*sessionRedis](), make_cache_stage[*sessionRedis]()},
|
2023-08-30 15:43:26 +09:00
|
|
|
startTime: time.Now(),
|
|
|
|
|
},
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient: redisClient,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateChannel := communication_channel_name_prefix + "_u"
|
|
|
|
|
deleteChannel := communication_channel_name_prefix + "_d"
|
|
|
|
|
sub := redisClient.Subscribe(ctx, updateChannel, deleteChannel)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
stageswitch := time.Now().Add(ttl)
|
|
|
|
|
tickTimer := time.After(ttl)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case <-tickTimer:
|
|
|
|
|
consumer.changeStage()
|
|
|
|
|
stageswitch = stageswitch.Add(ttl)
|
|
|
|
|
tempttl := time.Until(stageswitch)
|
|
|
|
|
tickTimer = time.After(tempttl)
|
|
|
|
|
|
|
|
|
|
case msg := <-sub.Channel():
|
|
|
|
|
if msg == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(msg.Payload) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch msg.Channel {
|
|
|
|
|
case updateChannel:
|
2023-08-31 14:33:08 +09:00
|
|
|
sk := storagekey(msg.Payload)
|
|
|
|
|
raw, err := redisClient.Get(ctx, string(sk)).Result()
|
2023-08-30 13:15:44 +09:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Println(err)
|
|
|
|
|
} else if len(raw) > 0 {
|
2023-08-30 13:38:13 +09:00
|
|
|
var si Authorization
|
2023-08-30 13:15:44 +09:00
|
|
|
if bson.Unmarshal([]byte(raw), &si) == nil {
|
2023-08-31 14:33:08 +09:00
|
|
|
consumer.add(sk, &sessionRedis{
|
2023-08-30 15:43:26 +09:00
|
|
|
Authorization: &si,
|
|
|
|
|
expireAt: time.Now().Add(consumer.ttl),
|
|
|
|
|
})
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case deleteChannel:
|
2023-08-31 14:33:08 +09:00
|
|
|
sk := storagekey(msg.Payload)
|
|
|
|
|
consumer.delete(sk)
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return consumer, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
func (c *consumer_redis) query_internal(sk storagekey) (*sessionRedis, bool, error) {
|
|
|
|
|
if _, deleted := c.stages[0].deleted[sk]; deleted {
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, false, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
if _, deleted := c.stages[1].deleted[sk]; deleted {
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, false, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
found, ok := c.stages[0].cache[sk]
|
2023-08-30 13:15:44 +09:00
|
|
|
if !ok {
|
2023-08-31 14:33:08 +09:00
|
|
|
found, ok = c.stages[1].cache[sk]
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ok {
|
2023-08-30 18:23:19 +09:00
|
|
|
return found, false, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
payload, err := c.redisClient.Get(c.ctx, string(sk)).Result()
|
2023-08-30 13:15:44 +09:00
|
|
|
if err == redis.Nil {
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, false, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
} else if err != nil {
|
|
|
|
|
logger.Println("consumer Query :", err)
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, false, err
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 18:23:19 +09:00
|
|
|
if len(payload) == 0 {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
2023-08-30 13:15:44 +09:00
|
|
|
|
2023-08-30 18:23:19 +09:00
|
|
|
var auth Authorization
|
|
|
|
|
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
ttl, err := c.redisClient.TTL(c.ctx, string(sk)).Result()
|
2023-08-30 18:23:19 +09:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Println("consumer Query :", err)
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
si := &sessionRedis{
|
|
|
|
|
Authorization: &auth,
|
|
|
|
|
expireAt: time.Now().Add(ttl),
|
|
|
|
|
}
|
2023-08-31 14:33:08 +09:00
|
|
|
c.add_internal(sk, si)
|
2023-08-30 18:23:19 +09:00
|
|
|
|
|
|
|
|
return si, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
func (c *consumer_redis) Query(pk string) (*Authorization, error) {
|
2023-08-30 18:23:19 +09:00
|
|
|
c.lock.Lock()
|
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
sk := publickey_to_storagekey(publickey(pk))
|
|
|
|
|
si, _, err := c.query_internal(sk)
|
2023-08-30 18:23:19 +09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if si == nil {
|
|
|
|
|
return nil, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
2023-08-30 18:23:19 +09:00
|
|
|
|
|
|
|
|
if time.Now().After(si.expireAt) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return si.Authorization, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
func (c *consumer_redis) Touch(pk string) (*Authorization, error) {
|
2023-08-30 13:15:44 +09:00
|
|
|
c.lock.Lock()
|
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
2023-08-31 14:33:08 +09:00
|
|
|
sk := publickey_to_storagekey(publickey(pk))
|
|
|
|
|
ok, err := c.redisClient.Expire(c.ctx, string(sk), c.ttl).Result()
|
2023-08-30 13:15:44 +09:00
|
|
|
if err == redis.Nil {
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
} else if err != nil {
|
|
|
|
|
logger.Println("consumer Touch :", err)
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, err
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ok {
|
2023-08-30 18:23:19 +09:00
|
|
|
// redis에 살아있다.
|
2023-08-31 14:33:08 +09:00
|
|
|
si, added, err := c.query_internal(sk)
|
2023-08-30 18:23:19 +09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 18:23:19 +09:00
|
|
|
if si == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !added {
|
|
|
|
|
si.expireAt = time.Now().Add(c.ttl)
|
|
|
|
|
// stage 0으로 옮기기 위해 add_internal을 다시 부름
|
2023-08-31 14:33:08 +09:00
|
|
|
c.add_internal(sk, si)
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
2023-08-30 18:23:19 +09:00
|
|
|
|
|
|
|
|
return si.Authorization, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 18:23:19 +09:00
|
|
|
return nil, nil
|
2023-08-30 13:15:44 +09:00
|
|
|
}
|