Skip to content

Commit

Permalink
add refresh token cleanup log message
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Mar 10, 2021
1 parent 04fd723 commit 4d32d19
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ func refetch(a *assets.AvatarHandler) {
}

func cleanupExpiredRefreshTokens(db database.Middleware) {
if err := db.CleanupExpiredTokens(); err != nil {
n, err := db.CleanupExpiredTokens()
if err != nil {
logger.Error("DATABASE :: failed cleaning up expired refresh tokens: %s", err.Error())
} else {
logger.Info("AUTH :: cleaned %d expired refresh tokens", n)
}
}

Expand Down Expand Up @@ -199,7 +202,7 @@ func main() {
}()
logger.Info("WEBSERVER :: started")

lct := lifecycletimer.New(24 * time.Hour).
lct := lifecycletimer.New(24 * time.Second).
Handle(func() { refetch(avatarAssetsHandler) }).
Handle(func() { cleanupExpiredRefreshTokens(db) }).
Start()
Expand Down
2 changes: 1 addition & 1 deletion internal/database/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Middleware interface {
RemoveRefreshToken(id snowflake.ID) error
// CleanupExpiredTokens removes all expired tokens
// from the database.
CleanupExpiredTokens() error
CleanupExpiredTokens() (int, error)

// SetAPIToken sets the passed API token
// to the user defined in the APIToken
Expand Down
9 changes: 6 additions & 3 deletions internal/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,21 @@ func (m *MongoDB) RemoveRefreshToken(id snowflake.ID) error {
return err
}

func (m *MongoDB) CleanupExpiredTokens() error {
func (m *MongoDB) CleanupExpiredTokens() (n int, err error) {
ctx, cancel := ctxTimeout(10 * time.Second)
defer cancel()

now := time.Now()
_, err := m.collections.refreshtokens.DeleteMany(ctx, bson.M{
res, err := m.collections.refreshtokens.DeleteMany(ctx, bson.M{
"deadline": bson.M{
"$lte": now,
},
})
if res != nil {
n = int(res.DeletedCount)
}

return err
return
}

// --- HELPERS ------------------------------------------------------------------
Expand Down

0 comments on commit 4d32d19

Please sign in to comment.