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