feat: expire & prolong sessions
This commit is contained in:
@@ -16,15 +16,18 @@ type SessionData struct {
|
||||
SessionId int `redis:"session_id"`
|
||||
RoomId string `redis:"room_id"`
|
||||
PersonId rooms.PersonId `redis:"person_id"`
|
||||
ExpireIn time.Duration `redis:"-"`
|
||||
}
|
||||
|
||||
type SessionManagement interface {
|
||||
Get(sessionId int) SessionData
|
||||
Get(ctx context.Context, sessionId int) SessionData
|
||||
Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error)
|
||||
Remove(ctx context.Context, sessionId int) error
|
||||
}
|
||||
|
||||
const sessionPrefix = "session"
|
||||
const SessionTtl = 3 * time.Hour
|
||||
const SessionProlongationWindow = time.Hour
|
||||
|
||||
func sessionIdToKey(sessionId int) string {
|
||||
return fmt.Sprintf("%s:%d", sessionPrefix, sessionId)
|
||||
@@ -34,15 +37,34 @@ type RedisSM struct {
|
||||
Rdb *redis.Client
|
||||
}
|
||||
|
||||
func (redisSM RedisSM) Get(sessionId int) SessionData {
|
||||
func (redisSM RedisSM) Get(ctx context.Context, sessionId int) SessionData {
|
||||
var foundSession SessionData
|
||||
redisKey := sessionIdToKey(sessionId)
|
||||
err := redisSM.Rdb.HGetAll(context.TODO(), redisKey).Scan(&foundSession)
|
||||
err := redisSM.Rdb.HGetAll(ctx, redisKey).Scan(&foundSession)
|
||||
if err != nil {
|
||||
log.Printf("> error reading %s : %s", redisKey, err)
|
||||
return SessionData{}
|
||||
}
|
||||
log.Printf("> successfully found %d %+v", sessionId, foundSession)
|
||||
ttl, err := redisSM.Rdb.TTL(ctx, redisKey).Result()
|
||||
if err != nil {
|
||||
log.Printf("> error getting ttl for session %+v", foundSession)
|
||||
return foundSession
|
||||
}
|
||||
if ttl == -2 {
|
||||
log.Printf("> ttl indicates session doesn't exist for session %+v", foundSession)
|
||||
return SessionData{}
|
||||
}
|
||||
if ttl < SessionProlongationWindow {
|
||||
err = redisSM.Rdb.Expire(ctx, redisKey, SessionTtl).Err()
|
||||
if err != nil {
|
||||
log.Printf("> error updating ttl for session %+v", foundSession)
|
||||
return foundSession
|
||||
} else {
|
||||
ttl = SessionTtl
|
||||
}
|
||||
}
|
||||
foundSession.ExpireIn = ttl
|
||||
return foundSession
|
||||
}
|
||||
func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error) {
|
||||
@@ -51,9 +73,10 @@ func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms
|
||||
SessionId: randId,
|
||||
RoomId: roomName,
|
||||
PersonId: personId,
|
||||
ExpireIn: SessionTtl,
|
||||
}
|
||||
err := redisSM.Rdb.HSet(ctx, sessionIdToKey(randId), newSession).Err()
|
||||
redisSM.Rdb.Expire(ctx, sessionIdToKey(randId), 24 * time.Hour)
|
||||
redisSM.Rdb.Expire(ctx, sessionIdToKey(randId), SessionTtl)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("> error! saving session %+v %s", newSession, err)
|
||||
@@ -69,7 +92,7 @@ func (redisSM RedisSM) Remove(ctx context.Context, sessionId int) error {
|
||||
|
||||
type DummySM struct{}
|
||||
|
||||
func (d DummySM) Get(sessionId int) SessionData {
|
||||
func (d DummySM) Get(ctx context.Context, sessionId int) SessionData {
|
||||
log.Printf("get dummy session by %d", sessionId)
|
||||
return SessionData{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user