세션 삭제용 구조체 선언
This commit is contained in:
@ -58,6 +58,7 @@ type Provider interface {
|
|||||||
|
|
||||||
type InvalidatedSession struct {
|
type InvalidatedSession struct {
|
||||||
Account primitive.ObjectID
|
Account primitive.ObjectID
|
||||||
|
SessionKeys []string
|
||||||
Infinite bool
|
Infinite bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package session
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
@ -78,12 +79,17 @@ func (p *provider_redis) RevokeAll(account primitive.ObjectID, infinite bool) ([
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sk := range sks {
|
if len(sks) > 0 {
|
||||||
if infinite {
|
p.redisClient.Del(p.ctx, sks...)
|
||||||
p.redisClient.Publish(p.ctx, p.deleteChannel, "~"+sk).Result()
|
|
||||||
} else {
|
invsess := InvalidatedSession{
|
||||||
p.redisClient.Publish(p.ctx, p.deleteChannel, sk).Result()
|
SessionKeys: sks,
|
||||||
|
Account: account,
|
||||||
|
Infinite: infinite,
|
||||||
}
|
}
|
||||||
|
data, _ := json.Marshal(invsess)
|
||||||
|
|
||||||
|
p.redisClient.Publish(p.ctx, p.deleteChannel, string(data)).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
return sks, nil
|
return sks, nil
|
||||||
@ -172,20 +178,14 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
|||||||
|
|
||||||
switch msg.Channel {
|
switch msg.Channel {
|
||||||
case deleteChannel:
|
case deleteChannel:
|
||||||
infinite := false
|
var invsess InvalidatedSession
|
||||||
var sk string
|
if err := json.Unmarshal([]byte(msg.Payload), &invsess); err != nil {
|
||||||
if msg.Payload[0] == '~' {
|
logger.Println("redis consumer deleteChannel unmarshal failed :", err)
|
||||||
sk = msg.Payload[1:]
|
break
|
||||||
infinite = true
|
|
||||||
} else {
|
|
||||||
sk = msg.Payload
|
|
||||||
infinite = false
|
|
||||||
}
|
}
|
||||||
old := consumer.delete(storagekey(sk))
|
|
||||||
if old != nil {
|
for _, sk := range invsess.SessionKeys {
|
||||||
invsess := InvalidatedSession{
|
consumer.delete(storagekey(sk))
|
||||||
Account: old.Account,
|
|
||||||
Infinite: infinite,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range consumer.onSessionInvalidated {
|
for _, f := range consumer.onSessionInvalidated {
|
||||||
@ -194,7 +194,6 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return consumer, nil
|
return consumer, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user