세션 해제 콜백 추가

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

@ -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)
}