wshandler도 session.Consumer로 교체
This commit is contained in:
@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user