세션키를 자체 발급하도록 수정

This commit is contained in:
2023-08-31 14:33:08 +09:00
parent e88df26ed7
commit cde46e6a5f
6 changed files with 306 additions and 243 deletions

View File

@ -1,6 +1,11 @@
package session package session
import ( import (
"encoding/binary"
"encoding/hex"
"math/rand"
"time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon"
) )
@ -18,3 +23,58 @@ type Authorization struct {
Uid string `bson:"u" json:"u"` Uid string `bson:"u" json:"u"`
Email string `bson:"em" json:"em"` Email string `bson:"em" json:"em"`
} }
type Provider interface {
New(*Authorization) (string, error)
Delete(primitive.ObjectID) error
Query(string) (*Authorization, error)
}
type Consumer interface {
Query(string) (*Authorization, error)
Touch(string) (*Authorization, error)
}
type storagekey string
type publickey string
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
func make_storagekey(acc primitive.ObjectID) storagekey {
bs := [4]byte{}
binary.LittleEndian.PutUint32(bs[:], r.Uint32())
return storagekey(acc.Hex() + hex.EncodeToString(bs[2:]))
}
func storagekey_to_publickey(sk storagekey) publickey {
bs, _ := hex.DecodeString(string(sk))
acc := bs[:12]
cs := bs[12:]
encoded := [14]byte{}
for i, v := range acc[:] {
encoded[i] = (v ^ cs[0]) ^ cs[1]
}
encoded[12] = cs[0]
encoded[13] = cs[1]
return publickey(hex.EncodeToString(encoded[:]))
}
func publickey_to_storagekey(pk publickey) storagekey {
bs, _ := hex.DecodeString(string(pk))
acc := bs[:12]
cs := bs[12:]
decoded := [14]byte{}
for i, v := range acc[:] {
decoded[i] = (v ^ cs[1]) ^ cs[0]
}
decoded[12] = cs[0]
decoded[13] = cs[1]
return storagekey(hex.EncodeToString(decoded[:]))
}

View File

@ -9,22 +9,17 @@ import (
) )
type cache_stage[T any] struct { type cache_stage[T any] struct {
cache map[string]T cache map[storagekey]T
deleted map[string]bool deleted map[storagekey]bool
} }
func make_cache_stage[T any]() *cache_stage[T] { func make_cache_stage[T any]() *cache_stage[T] {
return &cache_stage[T]{ return &cache_stage[T]{
cache: make(map[string]T), cache: make(map[storagekey]T),
deleted: make(map[string]bool), deleted: make(map[storagekey]bool),
} }
} }
type Consumer interface {
Query(string) (*Authorization, error)
Touch(string) (*Authorization, error)
}
type consumer_common[T any] struct { type consumer_common[T any] struct {
lock sync.Mutex lock sync.Mutex
ttl time.Duration ttl time.Duration
@ -33,36 +28,36 @@ type consumer_common[T any] struct {
startTime time.Time startTime time.Time
} }
func (c *consumer_common[T]) add_internal(key string, si T) { func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
c.stages[0].cache[key] = si c.stages[0].cache[sk] = si
delete(c.stages[0].deleted, key) delete(c.stages[0].deleted, sk)
c.stages[1].cache[key] = si c.stages[1].cache[sk] = si
delete(c.stages[1].deleted, key) delete(c.stages[1].deleted, sk)
logger.Printf("add : %v, %v\n", *c.stages[0], *c.stages[1]) logger.Printf("add : %v, %v\n", *c.stages[0], *c.stages[1])
} }
func (c *consumer_common[T]) add(key string, si T) { func (c *consumer_common[T]) add(sk storagekey, si T) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.add_internal(key, si) c.add_internal(sk, si)
} }
func (c *consumer_common[T]) delete_internal(key string) { func (c *consumer_common[T]) delete_internal(sk storagekey) {
delete(c.stages[0].cache, key) delete(c.stages[0].cache, sk)
c.stages[0].deleted[key] = true c.stages[0].deleted[sk] = true
delete(c.stages[1].cache, key) delete(c.stages[1].cache, sk)
c.stages[1].deleted[key] = true c.stages[1].deleted[sk] = true
logger.Printf("delete : %v, %v\n", *c.stages[0], *c.stages[1]) logger.Printf("delete : %v, %v\n", *c.stages[0], *c.stages[1])
} }
func (c *consumer_common[T]) delete(key string) { func (c *consumer_common[T]) delete(sk storagekey) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.delete_internal(key) c.delete_internal(sk)
} }
func (c *consumer_common[T]) changeStage() { func (c *consumer_common[T]) changeStage() {

View File

@ -12,15 +12,74 @@ import (
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
) )
type provider_mongo struct {
mongoClient gocommon.MongoClient
}
type sessionMongo struct { type sessionMongo struct {
*Authorization `bson:",inline"` Id primitive.ObjectID `bson:"_id,omitempty"`
Key string `bson:"key"` Auth *Authorization `bson:"auth"`
Key storagekey `bson:"key"`
Ts primitive.DateTime `bson:"_ts"` Ts primitive.DateTime `bson:"_ts"`
} }
func NewProviderWithMongo(ctx context.Context, mongoUrl string, dbname string, ttl time.Duration) (Provider, error) {
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, dbname)
if err != nil {
return nil, err
}
if err = mc.MakeUniqueIndices(session_collection_name, map[string]bson.D{
"key": {{Key: "key", Value: 1}},
}); err != nil {
return nil, err
}
if err := mc.MakeExpireIndex(session_collection_name, int32(ttl.Seconds())); err != nil {
return nil, err
}
return &provider_mongo{
mongoClient: mc,
}, nil
}
func (p *provider_mongo) New(input *Authorization) (string, error) {
sk := make_storagekey(input.Account)
_, _, err := p.mongoClient.Update(session_collection_name, bson.M{
"_id": input.Account,
}, bson.M{
"$set": sessionMongo{
Auth: input,
Key: sk,
Ts: primitive.NewDateTimeFromTime(time.Now().UTC()),
},
}, options.Update().SetUpsert(true))
return string(storagekey_to_publickey(sk)), err
}
func (p *provider_mongo) Delete(acc primitive.ObjectID) error {
_, err := p.mongoClient.Delete(session_collection_name, bson.M{
"_id": acc,
})
return err
}
func (p *provider_mongo) Query(pk string) (*Authorization, error) {
sk := publickey_to_storagekey(publickey(pk))
var auth Authorization
err := p.mongoClient.FindOneAs(session_collection_name, bson.M{
"key": sk,
}, &auth)
return &auth, err
}
type consumer_mongo struct { type consumer_mongo struct {
consumer_common[*sessionMongo] consumer_common[*sessionMongo]
ids map[primitive.ObjectID]string ids map[primitive.ObjectID]storagekey
mongoClient gocommon.MongoClient mongoClient gocommon.MongoClient
ttl time.Duration ttl time.Duration
} }
@ -46,7 +105,7 @@ func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, t
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(), startTime: time.Now(),
}, },
ids: make(map[primitive.ObjectID]string), ids: make(map[primitive.ObjectID]storagekey),
ttl: ttl, ttl: ttl,
mongoClient: mc, mongoClient: mc,
} }
@ -100,7 +159,11 @@ func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, t
case "insert": case "insert":
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session) consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
case "update": case "update":
if data.Session == nil {
consumer.deleteById(data.DocumentKey.Id)
} else {
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session) consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
}
case "delete": case "delete":
consumer.deleteById(data.DocumentKey.Id) consumer.deleteById(data.DocumentKey.Id)
} }
@ -134,18 +197,18 @@ func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, t
return consumer, nil return consumer, nil
} }
func (c *consumer_mongo) query_internal(key string) (*sessionMongo, bool, error) { func (c *consumer_mongo) query_internal(sk storagekey) (*sessionMongo, bool, error) {
if _, deleted := c.stages[0].deleted[key]; deleted { if _, deleted := c.stages[0].deleted[sk]; deleted {
return nil, false, nil return nil, false, nil
} }
if _, deleted := c.stages[1].deleted[key]; deleted { if _, deleted := c.stages[1].deleted[sk]; deleted {
return nil, false, nil return nil, false, nil
} }
found, ok := c.stages[0].cache[key] found, ok := c.stages[0].cache[sk]
if !ok { if !ok {
found, ok = c.stages[1].cache[key] found, ok = c.stages[1].cache[sk]
} }
if ok { if ok {
@ -154,7 +217,7 @@ func (c *consumer_mongo) query_internal(key string) (*sessionMongo, bool, error)
var si sessionMongo var si sessionMongo
err := c.mongoClient.FindOneAs(session_collection_name, bson.M{ err := c.mongoClient.FindOneAs(session_collection_name, bson.M{
"key": key, "key": sk,
}, &si) }, &si)
if err != nil { if err != nil {
@ -164,17 +227,18 @@ func (c *consumer_mongo) query_internal(key string) (*sessionMongo, bool, error)
if len(si.Key) > 0 { if len(si.Key) > 0 {
siptr := &si siptr := &si
c.add_internal(key, siptr) c.add_internal(sk, siptr)
return siptr, true, nil return siptr, true, nil
} }
return nil, false, nil return nil, false, nil
} }
func (c *consumer_mongo) Query(key string) (*Authorization, error) { func (c *consumer_mongo) Query(pk string) (*Authorization, error) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
si, _, err := c.query_internal(key) sk := publickey_to_storagekey(publickey(pk))
si, _, err := c.query_internal(sk)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -187,15 +251,16 @@ func (c *consumer_mongo) Query(key string) (*Authorization, error) {
return nil, nil return nil, nil
} }
return si.Authorization, nil return si.Auth, nil
} }
func (c *consumer_mongo) Touch(key string) (*Authorization, error) { func (c *consumer_mongo) Touch(pk string) (*Authorization, error) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
sk := publickey_to_storagekey(publickey(pk))
worked, _, err := c.mongoClient.Update(session_collection_name, bson.M{ worked, _, err := c.mongoClient.Update(session_collection_name, bson.M{
"key": key, "key": sk,
}, bson.M{ }, bson.M{
"$currentDate": bson.M{ "$currentDate": bson.M{
"_ts": bson.M{"$type": "date"}, "_ts": bson.M{"$type": "date"},
@ -212,7 +277,7 @@ func (c *consumer_mongo) Touch(key string) (*Authorization, error) {
return nil, nil return nil, nil
} }
si, added, err := c.query_internal(key) si, added, err := c.query_internal(sk)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -222,13 +287,9 @@ func (c *consumer_mongo) Touch(key string) (*Authorization, error) {
} }
if !added { if !added {
var doc struct { var doc sessionMongo
sessionMongo `bson:",inline"`
Id primitive.ObjectID `bson:"_id"`
}
err := c.mongoClient.FindOneAs(session_collection_name, bson.M{ err := c.mongoClient.FindOneAs(session_collection_name, bson.M{
"key": key, "key": sk,
}, &doc) }, &doc)
if err != nil { if err != nil {
@ -237,30 +298,30 @@ func (c *consumer_mongo) Touch(key string) (*Authorization, error) {
} }
if len(si.Key) > 0 { if len(si.Key) > 0 {
c.add_internal(key, &doc.sessionMongo) c.add_internal(sk, &doc)
c.ids[doc.Id] = key c.ids[doc.Id] = sk
return doc.Authorization, nil return doc.Auth, nil
} }
} }
return si.Authorization, nil return si.Auth, nil
} }
func (c *consumer_mongo) add(key string, id primitive.ObjectID, si *sessionMongo) { func (c *consumer_mongo) add(sk storagekey, id primitive.ObjectID, si *sessionMongo) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.consumer_common.add_internal(key, si) c.consumer_common.add_internal(sk, si)
c.ids[id] = key c.ids[id] = sk
} }
func (c *consumer_mongo) deleteById(id primitive.ObjectID) { func (c *consumer_mongo) deleteById(id primitive.ObjectID) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
if key, ok := c.ids[id]; ok { if sk, ok := c.ids[id]; ok {
c.consumer_common.delete_internal(key) c.consumer_common.delete_internal(sk)
delete(c.ids, id) delete(c.ids, id)
} }
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon" "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
) )
@ -15,6 +16,77 @@ type sessionRedis struct {
expireAt time.Time expireAt time.Time
} }
type provider_redis struct {
redisClient *redis.Client
updateChannel string
deleteChannel string
ttl time.Duration
ctx context.Context
}
func NewProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Provider, error) {
redisClient, err := gocommon.NewRedisClient(redisUrl)
if err != nil {
return nil, err
}
return &provider_redis{
redisClient: redisClient,
updateChannel: communication_channel_name_prefix + "_u",
deleteChannel: communication_channel_name_prefix + "_d",
ttl: ttl,
ctx: ctx,
}, nil
}
func (p *provider_redis) New(input *Authorization) (string, error) {
bt, err := bson.Marshal(input)
if err != nil {
return "", err
}
sk := make_storagekey(input.Account)
_, err = p.redisClient.SetEX(p.ctx, string(sk), bt, p.ttl).Result()
if err != nil {
return "", err
}
_, err = p.redisClient.Publish(p.ctx, p.updateChannel, string(sk)).Result()
return string(storagekey_to_publickey(sk)), err
}
func (p *provider_redis) Delete(account primitive.ObjectID) error {
prefix := account.Hex()
sks, err := p.redisClient.Keys(p.ctx, prefix+"*").Result()
if err != nil {
return err
}
for _, sk := range sks {
p.redisClient.Del(p.ctx, sk).Result()
p.redisClient.Publish(p.ctx, p.deleteChannel, sk).Result()
}
return nil
}
func (p *provider_redis) Query(pk string) (*Authorization, error) {
sk := publickey_to_storagekey(publickey(pk))
payload, err := p.redisClient.Get(p.ctx, string(sk)).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
return nil, err
}
var auth Authorization
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
return nil, err
}
return &auth, nil
}
type consumer_redis struct { type consumer_redis struct {
consumer_common[*sessionRedis] consumer_common[*sessionRedis]
redisClient *redis.Client redisClient *redis.Client
@ -66,22 +138,22 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
switch msg.Channel { switch msg.Channel {
case updateChannel: case updateChannel:
key := msg.Payload sk := storagekey(msg.Payload)
raw, err := redisClient.Get(ctx, key).Result() raw, err := redisClient.Get(ctx, string(sk)).Result()
if err != nil { if err != nil {
logger.Println(err) logger.Println(err)
} else if len(raw) > 0 { } else if len(raw) > 0 {
var si Authorization var si Authorization
if bson.Unmarshal([]byte(raw), &si) == nil { if bson.Unmarshal([]byte(raw), &si) == nil {
consumer.add(key, &sessionRedis{ consumer.add(sk, &sessionRedis{
Authorization: &si, Authorization: &si,
expireAt: time.Now().Add(consumer.ttl), expireAt: time.Now().Add(consumer.ttl),
}) })
} }
} }
case deleteChannel: case deleteChannel:
key := msg.Payload sk := storagekey(msg.Payload)
consumer.delete(key) consumer.delete(sk)
} }
} }
} }
@ -90,25 +162,25 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
return consumer, nil return consumer, nil
} }
func (c *consumer_redis) query_internal(key string) (*sessionRedis, bool, error) { func (c *consumer_redis) query_internal(sk storagekey) (*sessionRedis, bool, error) {
if _, deleted := c.stages[0].deleted[key]; deleted { if _, deleted := c.stages[0].deleted[sk]; deleted {
return nil, false, nil return nil, false, nil
} }
if _, deleted := c.stages[1].deleted[key]; deleted { if _, deleted := c.stages[1].deleted[sk]; deleted {
return nil, false, nil return nil, false, nil
} }
found, ok := c.stages[0].cache[key] found, ok := c.stages[0].cache[sk]
if !ok { if !ok {
found, ok = c.stages[1].cache[key] found, ok = c.stages[1].cache[sk]
} }
if ok { if ok {
return found, false, nil return found, false, nil
} }
payload, err := c.redisClient.Get(c.ctx, key).Result() payload, err := c.redisClient.Get(c.ctx, string(sk)).Result()
if err == redis.Nil { if err == redis.Nil {
return nil, false, nil return nil, false, nil
} else if err != nil { } else if err != nil {
@ -125,7 +197,7 @@ func (c *consumer_redis) query_internal(key string) (*sessionRedis, bool, error)
return nil, false, err return nil, false, err
} }
ttl, err := c.redisClient.TTL(c.ctx, key).Result() ttl, err := c.redisClient.TTL(c.ctx, string(sk)).Result()
if err != nil { if err != nil {
logger.Println("consumer Query :", err) logger.Println("consumer Query :", err)
return nil, false, err return nil, false, err
@ -135,16 +207,17 @@ func (c *consumer_redis) query_internal(key string) (*sessionRedis, bool, error)
Authorization: &auth, Authorization: &auth,
expireAt: time.Now().Add(ttl), expireAt: time.Now().Add(ttl),
} }
c.add_internal(key, si) c.add_internal(sk, si)
return si, true, nil return si, true, nil
} }
func (c *consumer_redis) Query(key string) (*Authorization, error) { func (c *consumer_redis) Query(pk string) (*Authorization, error) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
si, _, err := c.query_internal(key) sk := publickey_to_storagekey(publickey(pk))
si, _, err := c.query_internal(sk)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -160,11 +233,12 @@ func (c *consumer_redis) Query(key string) (*Authorization, error) {
return si.Authorization, nil return si.Authorization, nil
} }
func (c *consumer_redis) Touch(key string) (*Authorization, error) { func (c *consumer_redis) Touch(pk string) (*Authorization, error) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
ok, err := c.redisClient.Expire(c.ctx, key, c.ttl).Result() sk := publickey_to_storagekey(publickey(pk))
ok, err := c.redisClient.Expire(c.ctx, string(sk), c.ttl).Result()
if err == redis.Nil { if err == redis.Nil {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {
@ -174,7 +248,7 @@ func (c *consumer_redis) Touch(key string) (*Authorization, error) {
if ok { if ok {
// redis에 살아있다. // redis에 살아있다.
si, added, err := c.query_internal(key) si, added, err := c.query_internal(sk)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -186,7 +260,7 @@ func (c *consumer_redis) Touch(key string) (*Authorization, error) {
if !added { if !added {
si.expireAt = time.Now().Add(c.ttl) si.expireAt = time.Now().Add(c.ttl)
// stage 0으로 옮기기 위해 add_internal을 다시 부름 // stage 0으로 옮기기 위해 add_internal을 다시 부름
c.add_internal(key, si) c.add_internal(sk, si)
} }
return si.Authorization, nil return si.Authorization, nil

View File

@ -1,138 +0,0 @@
package session
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"repositories.action2quare.com/ayo/gocommon"
)
type Provider interface {
Update(string, *Authorization) error
Delete(string) error
Query(string) (*Authorization, error)
}
type provider_redis struct {
redisClient *redis.Client
updateChannel string
deleteChannel string
ttl time.Duration
ctx context.Context
}
type provider_mongo struct {
mongoClient gocommon.MongoClient
}
func NewProviderWithMongo(ctx context.Context, mongoUrl string, dbname string, ttl time.Duration) (Provider, error) {
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, dbname)
if err != nil {
return nil, err
}
if err = mc.MakeUniqueIndices(session_collection_name, map[string]bson.D{
"key": {{Key: "key", Value: 1}},
}); err != nil {
return nil, err
}
if err := mc.MakeExpireIndex(session_collection_name, int32(ttl.Seconds())); err != nil {
return nil, err
}
return &provider_mongo{
mongoClient: mc,
}, nil
}
func NewProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Provider, error) {
redisClient, err := gocommon.NewRedisClient(redisUrl)
if err != nil {
return nil, err
}
return &provider_redis{
redisClient: redisClient,
updateChannel: communication_channel_name_prefix + "_u",
deleteChannel: communication_channel_name_prefix + "_d",
ttl: ttl,
ctx: ctx,
}, nil
}
func (p *provider_redis) Update(key string, input *Authorization) error {
bt, err := bson.Marshal(input)
if err != nil {
return err
}
_, err = p.redisClient.SetEX(p.ctx, key, bt, p.ttl).Result()
if err != nil {
return err
}
_, err = p.redisClient.Publish(p.ctx, p.updateChannel, key).Result()
return err
}
func (p *provider_redis) Delete(key string) error {
cnt, err := p.redisClient.Del(p.ctx, key).Result()
if err != nil {
return err
}
if cnt > 0 {
_, err = p.redisClient.Publish(p.ctx, p.deleteChannel, key).Result()
}
return err
}
func (p *provider_redis) Query(key string) (*Authorization, error) {
payload, err := p.redisClient.Get(p.ctx, key).Result()
if err == redis.Nil {
return nil, nil
} else if err != nil {
return nil, err
}
var auth Authorization
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
return nil, err
}
return &auth, nil
}
func (p *provider_mongo) Update(key string, input *Authorization) error {
_, _, err := p.mongoClient.Update(session_collection_name, bson.M{
"key": key,
}, bson.M{
"$set": input,
"$currentDate": bson.M{
"_ts": bson.M{"$type": "date"},
},
}, options.Update().SetUpsert(true))
return err
}
func (p *provider_mongo) Delete(key string) error {
_, err := p.mongoClient.Delete(session_collection_name, bson.M{
"key": key,
})
return err
}
func (p *provider_mongo) Query(key string) (*Authorization, error) {
var auth Authorization
err := p.mongoClient.FindOneAs(session_collection_name, bson.M{
"key": key,
}, &auth)
return &auth, err
}

View File

@ -11,28 +11,53 @@ import (
) )
func TestExpTable(t *testing.T) { func TestExpTable(t *testing.T) {
// pv, err := NewProviderWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second) pv, err := NewProviderWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
// if err != nil {
// t.Error(err)
// }
// cs, err := NewConsumerWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
// if err != nil {
// t.Error(err)
// }
pv, err := NewProviderWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
cs, err := NewConsumerWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second) cs, err := NewConsumerWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
if err != nil {
t.Error(err)
}
// pv, err := NewProviderWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second)
// if err != nil {
// t.Error(err)
// }
// cs, err := NewConsumerWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second)
// if err != nil {
// t.Error(err)
// }
test := primitive.NewObjectID()
sk := make_storagekey(test)
pk := storagekey_to_publickey(sk)
if publickey_to_storagekey(pk) != sk {
t.Errorf("pk != sk : %s, %s", pk, sk)
}
au1 := &Authorization{
Account: primitive.NewObjectID(),
Platform: "editor",
Uid: "uid-1",
}
sk1, err := pv.New(au1)
if err != nil {
t.Error(err)
}
au2 := &Authorization{
Account: primitive.NewObjectID(),
Platform: "editor",
Uid: "uid-2",
}
sk2, err := pv.New(au2)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
sk1 := primitive.NewObjectID().Hex()
sk2 := primitive.NewObjectID().Hex()
go func() { go func() {
for { for {
q1, err := cs.Query(sk1) q1, err := cs.Query(sk1)
@ -44,27 +69,13 @@ func TestExpTable(t *testing.T) {
} }
}() }()
time.Sleep(2 * time.Second)
pv.Update(sk1, &Authorization{
Account: primitive.NewObjectID(),
Platform: "editor",
Uid: "uid-1",
})
time.Sleep(2 * time.Second)
pv.Update(sk2, &Authorization{
Account: primitive.NewObjectID(),
Platform: "editor",
Uid: "uid-2",
})
cs.Touch(sk1) cs.Touch(sk1)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
cs.Touch(sk2) cs.Touch(sk2)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
pv.Delete(sk1) pv.Delete(au1.Account)
cs.Touch(sk1) cs.Touch(sk1)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)