Skip to content

Commit 1415572

Browse files
committed
shutdown of user challenges cleaner
1 parent ca177c5 commit 1415572

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

internal/api/auth/auth.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ type Challenge struct {
3636

3737
type UserChalenges struct {
3838
chalenges map[string]Challenge
39+
stopChannel chan struct{}
3940
mu sync.RWMutex
4041
}
4142

4243
func NewUserChalenges() *UserChalenges {
4344
uc := &UserChalenges{
4445
chalenges: make(map[string]Challenge),
46+
stopChannel: make(chan struct{}),
4547
}
4648

4749
go uc.cleanupExpired()
@@ -51,7 +53,8 @@ func NewUserChalenges() *UserChalenges {
5153
func (uc *UserChalenges) cleanupExpired() {
5254
t := time.Tick(time.Minute * 1)
5355

54-
for range t {
56+
select {
57+
case <-t:
5558
uc.mu.Lock()
5659
for k, v := range uc.chalenges {
5760
if time.Since(v.Created) >= challengeTimeToLive {
@@ -60,9 +63,16 @@ func (uc *UserChalenges) cleanupExpired() {
6063
}
6164
}
6265
uc.mu.Unlock()
66+
case <- uc.stopChannel:
67+
return
6368
}
6469
}
6570

71+
func (uc *UserChalenges) ShutDown() {
72+
close(uc.stopChannel)
73+
slog.Info("Cleaned up resourses")
74+
}
75+
6676
func (uc *UserChalenges) Add() (string, string) {
6777
uc.mu.Lock()
6878
defer uc.mu.Unlock()

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func main() {
2626

2727
passwordStore := render_folder.NewPasswordIdStore()
2828
userChallenges := auth.NewUserChalenges()
29+
defer userChallenges.ShutDown()
30+
2931
mu := router.NewMutexHandler(passwordStore, userChallenges)
3032

3133
fs := http.FileServer(http.FS(clientAssets))

0 commit comments

Comments
 (0)