consumer/provider에 Authorization타입으로 고정
This commit is contained in:
@ -3,3 +3,12 @@ package session
|
|||||||
const (
|
const (
|
||||||
communication_channel_name_prefix = "_sess_comm_chan_name"
|
communication_channel_name_prefix = "_sess_comm_chan_name"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Authorization struct {
|
||||||
|
Account string `bson:"a" json:"a"`
|
||||||
|
|
||||||
|
// by authorization provider
|
||||||
|
Platform string `bson:"p" json:"p"`
|
||||||
|
Uid string `bson:"u" json:"u"`
|
||||||
|
Email string `bson:"em" json:"em"`
|
||||||
|
}
|
||||||
|
|||||||
@ -11,43 +11,43 @@ import (
|
|||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type innerSession[T any] struct {
|
type innerSession struct {
|
||||||
inner *T
|
inner *Authorization
|
||||||
expireAt time.Time
|
expireAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type cache_stage[T any] struct {
|
type cache_stage struct {
|
||||||
cache map[string]innerSession[T]
|
cache map[string]innerSession
|
||||||
deleted map[string]bool
|
deleted map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func make_cache_stage[T any]() *cache_stage[T] {
|
func make_cache_stage() *cache_stage {
|
||||||
return &cache_stage[T]{
|
return &cache_stage{
|
||||||
cache: make(map[string]innerSession[T]),
|
cache: make(map[string]innerSession),
|
||||||
deleted: make(map[string]bool),
|
deleted: make(map[string]bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Consumer[T any] struct {
|
type Consumer struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
redisClient *redis.Client
|
redisClient *redis.Client
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
stages [2]*cache_stage[T]
|
stages [2]*cache_stage
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration) (*Consumer[T], error) {
|
func NewConsumer(ctx context.Context, redisUrl string, ttl time.Duration) (*Consumer, error) {
|
||||||
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
consumer := &Consumer[T]{
|
consumer := &Consumer{
|
||||||
redisClient: redisClient,
|
redisClient: redisClient,
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
stages: [2]*cache_stage[T]{make_cache_stage[T](), make_cache_stage[T]()},
|
stages: [2]*cache_stage{make_cache_stage(), make_cache_stage()},
|
||||||
startTime: time.Now(),
|
startTime: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Println(err)
|
logger.Println(err)
|
||||||
} else if len(raw) > 0 {
|
} else if len(raw) > 0 {
|
||||||
var si T
|
var si Authorization
|
||||||
if bson.Unmarshal([]byte(raw), &si) == nil {
|
if bson.Unmarshal([]byte(raw), &si) == nil {
|
||||||
consumer.add(key, &si)
|
consumer.add(key, &si)
|
||||||
}
|
}
|
||||||
@ -102,8 +102,8 @@ func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration)
|
|||||||
return consumer, nil
|
return consumer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Consumer[T]) add_internal(key string, si *T, ttl time.Duration) {
|
func (c *Consumer) add_internal(key string, si *Authorization, ttl time.Duration) {
|
||||||
inner := innerSession[T]{
|
inner := innerSession{
|
||||||
inner: si,
|
inner: si,
|
||||||
expireAt: time.Now().Add(ttl),
|
expireAt: time.Now().Add(ttl),
|
||||||
}
|
}
|
||||||
@ -116,14 +116,14 @@ func (c *Consumer[T]) add_internal(key string, si *T, ttl time.Duration) {
|
|||||||
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[T]) add(key string, si *T) {
|
func (c *Consumer) add(key string, si *Authorization) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
c.add_internal(key, si, c.ttl)
|
c.add_internal(key, si, c.ttl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Consumer[T]) delete(key string) {
|
func (c *Consumer) delete(key string) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@ -135,19 +135,19 @@ func (c *Consumer[T]) delete(key string) {
|
|||||||
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[T]) changeStage() {
|
func (c *Consumer) changeStage() {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
logger.Printf("changeStage : %v, %v\n", *c.stages[0], *c.stages[1])
|
logger.Printf("changeStage : %v, %v\n", *c.stages[0], *c.stages[1])
|
||||||
|
|
||||||
c.stages[1] = c.stages[0]
|
c.stages[1] = c.stages[0]
|
||||||
c.stages[0] = make_cache_stage[T]()
|
c.stages[0] = make_cache_stage()
|
||||||
|
|
||||||
logger.Printf("---> : %v, %v\n", *c.stages[0], *c.stages[1])
|
logger.Printf("---> : %v, %v\n", *c.stages[0], *c.stages[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Consumer[T]) Query(key string) *T {
|
func (c *Consumer) Query(key string) *Authorization {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ func (c *Consumer[T]) Query(key string) *T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(payload) > 0 {
|
if len(payload) > 0 {
|
||||||
var si T
|
var si Authorization
|
||||||
if bson.Unmarshal([]byte(payload), &si) == nil {
|
if bson.Unmarshal([]byte(payload), &si) == nil {
|
||||||
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -194,7 +194,7 @@ func (c *Consumer[T]) Query(key string) *T {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Consumer[T]) Touch(key string) bool {
|
func (c *Consumer) Touch(key string) bool {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import (
|
|||||||
"repositories.action2quare.com/ayo/gocommon"
|
"repositories.action2quare.com/ayo/gocommon"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Provider[T any] struct {
|
type Provider struct {
|
||||||
redisClient *redis.Client
|
redisClient *redis.Client
|
||||||
updateChannel string
|
updateChannel string
|
||||||
deleteChannel string
|
deleteChannel string
|
||||||
@ -17,13 +17,13 @@ type Provider[T any] struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProvider[T any](ctx context.Context, redisUrl string, ttl time.Duration) (*Provider[T], error) {
|
func NewProvider(ctx context.Context, redisUrl string, ttl time.Duration) (*Provider, error) {
|
||||||
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Provider[T]{
|
return &Provider{
|
||||||
redisClient: redisClient,
|
redisClient: redisClient,
|
||||||
updateChannel: communication_channel_name_prefix + "_u",
|
updateChannel: communication_channel_name_prefix + "_u",
|
||||||
deleteChannel: communication_channel_name_prefix + "_d",
|
deleteChannel: communication_channel_name_prefix + "_d",
|
||||||
@ -32,7 +32,7 @@ func NewProvider[T any](ctx context.Context, redisUrl string, ttl time.Duration)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider[T]) Update(key string, input *T) error {
|
func (p *Provider) Update(key string, input *Authorization) error {
|
||||||
bt, err := bson.Marshal(input)
|
bt, err := bson.Marshal(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -47,7 +47,7 @@ func (p *Provider[T]) Update(key string, input *T) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider[T]) Delete(key string) error {
|
func (p *Provider) Delete(key string) error {
|
||||||
cnt, err := p.redisClient.Del(p.ctx, key).Result()
|
cnt, err := p.redisClient.Del(p.ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -10,18 +10,13 @@ import (
|
|||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sessioninfo struct {
|
|
||||||
Platform string
|
|
||||||
Uid string
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpTable(t *testing.T) {
|
func TestExpTable(t *testing.T) {
|
||||||
pv, err := NewProvider[sessioninfo](context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
pv, err := NewProvider(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cs, err := NewConsumer[sessioninfo](context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
cs, err := NewConsumer(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
@ -37,13 +32,15 @@ func TestExpTable(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
pv.Update(newid1.Hex(), &sessioninfo{
|
pv.Update(newid1.Hex(), &Authorization{
|
||||||
|
Account: newid1.Hex(),
|
||||||
Platform: "editor",
|
Platform: "editor",
|
||||||
Uid: "uid-1",
|
Uid: "uid-1",
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
pv.Update(newid2.Hex(), &sessioninfo{
|
pv.Update(newid2.Hex(), &Authorization{
|
||||||
|
Account: newid2.Hex(),
|
||||||
Platform: "editor",
|
Platform: "editor",
|
||||||
Uid: "uid-2",
|
Uid: "uid-2",
|
||||||
})
|
})
|
||||||
@ -61,7 +58,7 @@ func TestExpTable(t *testing.T) {
|
|||||||
cs.Touch(newid2.Hex())
|
cs.Touch(newid2.Hex())
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
cs2, err := NewConsumer[sessioninfo](context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
cs2, err := NewConsumer(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user