wshandler도 session.Consumer로 교체
This commit is contained in:
@ -21,8 +21,8 @@ func make_cache_stage[T any]() *cache_stage[T] {
|
||||
}
|
||||
|
||||
type Consumer interface {
|
||||
Query(string) *Authorization
|
||||
Touch(string) bool
|
||||
Query(string) (*Authorization, error)
|
||||
Touch(string) (*Authorization, error)
|
||||
}
|
||||
|
||||
type consumer_common[T any] struct {
|
||||
|
||||
@ -19,7 +19,7 @@ type sessionMongo struct {
|
||||
}
|
||||
|
||||
type consumer_mongo struct {
|
||||
consumer_common[sessionMongo]
|
||||
consumer_common[*sessionMongo]
|
||||
ids map[primitive.ObjectID]string
|
||||
mongoClient gocommon.MongoClient
|
||||
ttl time.Duration
|
||||
@ -30,7 +30,7 @@ type sessionPipelineDocument struct {
|
||||
DocumentKey struct {
|
||||
Id primitive.ObjectID `bson:"_id"`
|
||||
} `bson:"documentKey"`
|
||||
Session sessionMongo `bson:"fullDocument"`
|
||||
Session *sessionMongo `bson:"fullDocument"`
|
||||
}
|
||||
|
||||
func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, ttl time.Duration) (Consumer, error) {
|
||||
@ -40,10 +40,10 @@ func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, t
|
||||
}
|
||||
|
||||
consumer := &consumer_mongo{
|
||||
consumer_common: consumer_common[sessionMongo]{
|
||||
consumer_common: consumer_common[*sessionMongo]{
|
||||
ttl: ttl,
|
||||
ctx: ctx,
|
||||
stages: [2]*cache_stage[sessionMongo]{make_cache_stage[sessionMongo](), make_cache_stage[sessionMongo]()},
|
||||
stages: [2]*cache_stage[*sessionMongo]{make_cache_stage[*sessionMongo](), make_cache_stage[*sessionMongo]()},
|
||||
startTime: time.Now(),
|
||||
},
|
||||
ids: make(map[primitive.ObjectID]string),
|
||||
@ -134,16 +134,13 @@ func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, t
|
||||
return consumer, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Query(key string) *Authorization {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
func (c *consumer_mongo) query_internal(key string) (*sessionMongo, bool, error) {
|
||||
if _, deleted := c.stages[0].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
if _, deleted := c.stages[1].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
found, ok := c.stages[0].cache[key]
|
||||
@ -151,11 +148,8 @@ func (c *consumer_mongo) Query(key string) *Authorization {
|
||||
found, ok = c.stages[1].cache[key]
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
if ok {
|
||||
if now.Before(found.Ts.Time().Add(c.ttl)) {
|
||||
return found.Authorization
|
||||
}
|
||||
return found, false, nil
|
||||
}
|
||||
|
||||
var si sessionMongo
|
||||
@ -165,21 +159,42 @@ func (c *consumer_mongo) Query(key string) *Authorization {
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(si.Key) > 0 {
|
||||
c.add_internal(key, si)
|
||||
return si.Authorization
|
||||
siptr := &si
|
||||
c.add_internal(key, siptr)
|
||||
return siptr, true, nil
|
||||
}
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Touch(key string) bool {
|
||||
func (c *consumer_mongo) Query(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
_, _, err := c.mongoClient.Update(session_collection_name, bson.M{
|
||||
si, _, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if time.Now().After(si.Ts.Time().Add(c.ttl)) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Touch(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
worked, _, err := c.mongoClient.Update(session_collection_name, bson.M{
|
||||
"key": key,
|
||||
}, bson.M{
|
||||
"$currentDate": bson.M{
|
||||
@ -189,13 +204,50 @@ func (c *consumer_mongo) Touch(key string) bool {
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Touch :", err)
|
||||
return false
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return true
|
||||
if !worked {
|
||||
// 이미 만료되서 사라짐
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
si, added, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if !added {
|
||||
var doc struct {
|
||||
sessionMongo `bson:",inline"`
|
||||
Id primitive.ObjectID `bson:"_id"`
|
||||
}
|
||||
|
||||
err := c.mongoClient.FindOneAs(session_collection_name, bson.M{
|
||||
"key": key,
|
||||
}, &doc)
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(si.Key) > 0 {
|
||||
c.add_internal(key, &doc.sessionMongo)
|
||||
c.ids[doc.Id] = key
|
||||
|
||||
return doc.Authorization, nil
|
||||
}
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) add(key string, id primitive.ObjectID, si sessionMongo) {
|
||||
func (c *consumer_mongo) add(key string, id primitive.ObjectID, si *sessionMongo) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ type sessionRedis struct {
|
||||
}
|
||||
|
||||
type consumer_redis struct {
|
||||
consumer_common[sessionRedis]
|
||||
consumer_common[*sessionRedis]
|
||||
redisClient *redis.Client
|
||||
}
|
||||
|
||||
@ -27,10 +27,10 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
}
|
||||
|
||||
consumer := &consumer_redis{
|
||||
consumer_common: consumer_common[sessionRedis]{
|
||||
consumer_common: consumer_common[*sessionRedis]{
|
||||
ttl: ttl,
|
||||
ctx: ctx,
|
||||
stages: [2]*cache_stage[sessionRedis]{make_cache_stage[sessionRedis](), make_cache_stage[sessionRedis]()},
|
||||
stages: [2]*cache_stage[*sessionRedis]{make_cache_stage[*sessionRedis](), make_cache_stage[*sessionRedis]()},
|
||||
startTime: time.Now(),
|
||||
},
|
||||
redisClient: redisClient,
|
||||
@ -73,7 +73,7 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
} else if len(raw) > 0 {
|
||||
var si Authorization
|
||||
if bson.Unmarshal([]byte(raw), &si) == nil {
|
||||
consumer.add(key, sessionRedis{
|
||||
consumer.add(key, &sessionRedis{
|
||||
Authorization: &si,
|
||||
expireAt: time.Now().Add(consumer.ttl),
|
||||
})
|
||||
@ -90,16 +90,13 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
return consumer, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Query(key string) *Authorization {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
func (c *consumer_redis) query_internal(key string) (*sessionRedis, bool, error) {
|
||||
if _, deleted := c.stages[0].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
if _, deleted := c.stages[1].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
found, ok := c.stages[0].cache[key]
|
||||
@ -108,62 +105,92 @@ func (c *consumer_redis) Query(key string) *Authorization {
|
||||
}
|
||||
|
||||
if ok {
|
||||
if found.expireAt.After(time.Now()) {
|
||||
return found.Authorization
|
||||
}
|
||||
return found, false, nil
|
||||
}
|
||||
|
||||
payload, err := c.redisClient.Get(c.ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
} else if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(payload) > 0 {
|
||||
var si Authorization
|
||||
if bson.Unmarshal([]byte(payload), &si) == nil {
|
||||
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
c.add_internal(key, sessionRedis{
|
||||
Authorization: &si,
|
||||
expireAt: time.Now().Add(ttl),
|
||||
})
|
||||
return &si
|
||||
}
|
||||
if len(payload) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil
|
||||
|
||||
var auth Authorization
|
||||
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
si := &sessionRedis{
|
||||
Authorization: &auth,
|
||||
expireAt: time.Now().Add(ttl),
|
||||
}
|
||||
c.add_internal(key, si)
|
||||
|
||||
return si, true, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Touch(key string) bool {
|
||||
func (c *consumer_redis) Query(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
si, _, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if time.Now().After(si.expireAt) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Touch(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
ok, err := c.redisClient.Expire(c.ctx, key, c.ttl).Result()
|
||||
if err == redis.Nil {
|
||||
return false
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
logger.Println("consumer Touch :", err)
|
||||
return false
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ok {
|
||||
newexpire := time.Now().Add(c.ttl)
|
||||
found, ok := c.stages[0].cache[key]
|
||||
if ok {
|
||||
found.expireAt = newexpire
|
||||
// redis에 살아있다.
|
||||
si, added, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
found, ok = c.stages[1].cache[key]
|
||||
if ok {
|
||||
found.expireAt = newexpire
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if !added {
|
||||
si.expireAt = time.Now().Add(c.ttl)
|
||||
// stage 0으로 옮기기 위해 add_internal을 다시 부름
|
||||
c.add_internal(key, si)
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
return ok
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@ -35,8 +35,11 @@ func TestExpTable(t *testing.T) {
|
||||
sk2 := primitive.NewObjectID().Hex()
|
||||
go func() {
|
||||
for {
|
||||
logger.Println("query :", cs.Query(sk1))
|
||||
logger.Println("query :", cs.Query(sk2))
|
||||
q1, err := cs.Query(sk1)
|
||||
logger.Println("query :", q1, err)
|
||||
|
||||
q2, err := cs.Query(sk2)
|
||||
logger.Println("query :", q2, err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
@ -73,6 +76,7 @@ func TestExpTable(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
logger.Println("queryf :", cs2.Query(sk2))
|
||||
q2, err := cs2.Query(sk2)
|
||||
logger.Println("queryf :", q2, err)
|
||||
time.Sleep(20 * time.Second)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user