From d8458662fd07cd588423a76bf174da9c326f5a55 Mon Sep 17 00:00:00 2001 From: mountain Date: Wed, 30 Aug 2023 13:38:13 +0900 Subject: [PATCH] =?UTF-8?q?consumer/provider=EC=97=90=20Authorization?= =?UTF-8?q?=ED=83=80=EC=9E=85=EC=9C=BC=EB=A1=9C=20=EA=B3=A0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session/common.go | 9 +++++++++ session/consumer.go | 44 ++++++++++++++++++++--------------------- session/provider.go | 10 +++++----- session/session_test.go | 17 +++++++--------- 4 files changed, 43 insertions(+), 37 deletions(-) diff --git a/session/common.go b/session/common.go index bd8f959..da279cf 100644 --- a/session/common.go +++ b/session/common.go @@ -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"` +} diff --git a/session/consumer.go b/session/consumer.go index fc7bed9..faec072 100644 --- a/session/consumer.go +++ b/session/consumer.go @@ -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() diff --git a/session/provider.go b/session/provider.go index 84f0899..3ea2375 100644 --- a/session/provider.go +++ b/session/provider.go @@ -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 diff --git a/session/session_test.go b/session/session_test.go index 0f2c3fd..a0cd0ef 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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) }