Skip to content

Commit e369b0f

Browse files
Added Redis connection functions and status field in sessions
1 parent d56c61f commit e369b0f

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ require (
66
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
77
github.com/MakeNowJust/heredoc v1.0.0 // indirect
88
github.com/bufbuild/buf v1.53.0 // indirect
9+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
910
github.com/davecgh/go-spew v1.1.1 // indirect
11+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1012
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 // indirect
1113
github.com/go-ldap/ldap/v3 v3.4.11 // indirect
1214
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
1315
github.com/google/uuid v1.6.0 // indirect
1416
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1517
github.com/joho/godotenv v1.5.1 // indirect
18+
github.com/redis/go-redis/v9 v9.9.0 // indirect
1619
github.com/spf13/cobra v1.9.1 // indirect
1720
github.com/spf13/pflag v1.0.6 // indirect
1821
go.uber.org/automaxprocs v1.6.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ
44
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
55
github.com/bufbuild/buf v1.53.0 h1:i0OgpDkzv8yLyCokXRCgbQao/SqTnqXYtERre0Jw6Do=
66
github.com/bufbuild/buf v1.53.0/go.mod h1:DylfLDMblZt5mX/SEFv2VzZMgdlePArhMGYVpk4M6N0=
7+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
8+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
810
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
911
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
13+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
1014
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 h1:BP4M0CvQ4S3TGls2FvczZtj5Re/2ZzkV9VwqPHH/3Bo=
1115
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
1216
github.com/go-ldap/ldap/v3 v3.4.11 h1:4k0Yxweg+a3OyBLjdYn5OKglv18JNvfDykSoI8bW0gU=
@@ -19,6 +23,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
1923
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2024
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
2125
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
26+
github.com/redis/go-redis/v9 v9.9.0 h1:URbPQ4xVQSQhZ27WMQVmZSo3uT3pL+4IdHVcYq2nVfM=
27+
github.com/redis/go-redis/v9 v9.9.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
2228
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2329
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
2430
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=

internal/session/manager.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (m *Manager) CreateSession(username, ipAddress, userAgent string) error {
4444
/* create the session */
4545
session := &Session{
4646
ID: sessionID,
47+
Status: StatusActive,
4748
Username: username,
4849
IP: ipAddress,
4950
UserAgent: userAgent,
@@ -73,19 +74,22 @@ func (m *Manager) ExpireSession(username string) {
7374
m.mutex.Lock()
7475
defer m.mutex.Unlock()
7576

76-
/* TODO: Add expired session to REDIS for persistent logging */
77-
7877
/* check if user exists in active sessions */
7978
session, ok := m.sessionsMap[username]
8079
if !ok {
8180
return
8281
}
8382

83+
/* disable the session */
84+
session.Status = StatusExpired
85+
8486
/* remove session from sessionOrder Linked List */
8587
if session.listElem != nil {
8688
m.sessionOrder.Remove(session.listElem)
8789
}
8890

91+
/* TODO: Add expired session to REDIS for persistent logging */
92+
8993
/* remove session from sessionsMap */
9094
delete(m.sessionsMap, username)
9195
}

internal/session/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66
"time"
77
)
88

9+
/* defining Status type for sessions */
10+
type Status string
11+
12+
/* for status field */
13+
const (
14+
StatusActive Status = "active"
15+
StatusExpired Status = "expired"
16+
)
17+
918
/*
1019
session struct for a user
1120
appropriate fields must always be updated when any request is made
@@ -15,6 +24,9 @@ type Session struct {
1524
CompletedCount int
1625
FailedCount int
1726

27+
/* session status: active: 1 / expired: 0 */
28+
Status Status
29+
1830
/* unique ID of session [will be associated with the user forever in logs] */
1931
ID string
2032

internal/session/redis.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package session
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
/* defines the methods to expose (for dependency injection) */
12+
type RedisClient interface {
13+
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
14+
Get(ctx context.Context, key string) (string, error)
15+
HMSet(ctx context.Context, key string, values map[string]interface{}) error
16+
}
17+
18+
/* redisClient implementation */
19+
type redisClient struct {
20+
client *redis.Client
21+
}
22+
23+
/* for creating a new redis client */
24+
func NewRedisClient(address, password string, db int) (RedisClient, error) {
25+
rdb := redis.NewClient(&redis.Options{
26+
Addr: address,
27+
Password: password,
28+
DB: db,
29+
})
30+
31+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
32+
defer cancel()
33+
34+
if err := rdb.Ping(ctx).Err(); err != nil {
35+
return nil, fmt.Errorf("could not connect to Redis: %w", err)
36+
}
37+
38+
return &redisClient{client: rdb}, nil
39+
}
40+
41+
/* Set sets a key-value pair in Redis */
42+
func (r *redisClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
43+
return r.client.Set(ctx, key, value, expiration).Err()
44+
}
45+
46+
/* retrieves a value by key from Redis */
47+
func (r *redisClient) Get(ctx context.Context, key string) (string, error) {
48+
return r.client.Get(ctx, key).Result()
49+
}
50+
51+
/* avoid unsafe casting */
52+
func (r *redisClient) HMSet(ctx context.Context, key string, values map[string]interface{}) error {
53+
return r.client.HSet(ctx, key, values).Err()
54+
}

0 commit comments

Comments
 (0)