세션 해제 콜백 추가

This commit is contained in:
2023-12-25 22:06:57 +09:00
parent 08802176cb
commit 46f7d358ed
6 changed files with 110 additions and 27 deletions

View File

@ -31,6 +31,7 @@ type Provider interface {
type Consumer interface {
Query(string) (Authorization, error)
Touch(string) (Authorization, error)
RegisterOnSessionInvalidated(func(primitive.ObjectID))
}
type storagekey string

View File

@ -4,6 +4,8 @@ import (
"context"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type cache_stage[T any] struct {
@ -19,11 +21,12 @@ func make_cache_stage[T any]() *cache_stage[T] {
}
type consumer_common[T any] struct {
lock sync.Mutex
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
startTime time.Time
lock sync.Mutex
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
startTime time.Time
onSessionInvalidated []func(primitive.ObjectID)
}
func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
@ -33,18 +36,25 @@ func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
delete(c.stages[1].deleted, sk)
}
func (c *consumer_common[T]) delete_internal(sk storagekey) {
delete(c.stages[0].cache, sk)
func (c *consumer_common[T]) delete_internal(sk storagekey) (old T) {
if v, ok := c.stages[0].cache[sk]; ok {
old = v
delete(c.stages[0].cache, sk)
delete(c.stages[1].cache, sk)
} else if v, ok = c.stages[1].cache[sk]; ok {
old = v
delete(c.stages[1].cache, sk)
}
c.stages[0].deleted[sk] = true
delete(c.stages[1].cache, sk)
c.stages[1].deleted[sk] = true
return
}
func (c *consumer_common[T]) delete(sk storagekey) {
func (c *consumer_common[T]) delete(sk storagekey) T {
c.lock.Lock()
defer c.lock.Unlock()
c.delete_internal(sk)
return c.delete_internal(sk)
}
func (c *consumer_common[T]) changeStage() {

View File

@ -182,12 +182,20 @@ func newConsumerWithMongo(ctx context.Context, mongoUrl string, ttl time.Duratio
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
case "update":
if data.Session == nil {
consumer.deleteById(data.DocumentKey.Id)
if old := consumer.deleteById(data.DocumentKey.Id); old != nil {
for _, f := range consumer.onSessionInvalidated {
f(old.Auth.Account)
}
}
} else {
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
}
case "delete":
consumer.deleteById(data.DocumentKey.Id)
if old := consumer.deleteById(data.DocumentKey.Id); old != nil {
for _, f := range consumer.onSessionInvalidated {
f(old.Auth.Account)
}
}
}
} else {
logger.Error("watchAuthCollection stream.Decode failed :", err)
@ -338,12 +346,17 @@ func (c *consumer_mongo) add(sk storagekey, id primitive.ObjectID, si *sessionMo
c.ids[id] = sk
}
func (c *consumer_mongo) deleteById(id primitive.ObjectID) {
func (c *consumer_mongo) deleteById(id primitive.ObjectID) (old *sessionMongo) {
c.lock.Lock()
defer c.lock.Unlock()
if sk, ok := c.ids[id]; ok {
c.consumer_common.delete_internal(sk)
old = c.consumer_common.delete_internal(sk)
delete(c.ids, id)
}
return
}
func (c *consumer_mongo) RegisterOnSessionInvalidated(cb func(primitive.ObjectID)) {
c.onSessionInvalidated = append(c.onSessionInvalidated, cb)
}

View File

@ -161,7 +161,12 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
switch msg.Channel {
case deleteChannel:
sk := storagekey(msg.Payload)
consumer.delete(sk)
old := consumer.delete(sk)
if old != nil {
for _, f := range consumer.onSessionInvalidated {
f(old.Account)
}
}
}
}
}
@ -286,3 +291,7 @@ func (c *consumer_redis) Touch(pk string) (Authorization, error) {
return Authorization{}, nil
}
func (c *consumer_redis) RegisterOnSessionInvalidated(cb func(primitive.ObjectID)) {
c.onSessionInvalidated = append(c.onSessionInvalidated, cb)
}