Chore: Fix issues reported by staticcheck; enable stylecheck linter (#28866)

* Chore: Fix issues reported by staticcheck

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Apply suggestions from code review

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
Arve Knudsen
2020-11-05 15:37:11 +01:00
committed by GitHub
co-authored by Emil Tullstedt
parent 135b83e17f
commit 676d393ec9
27 changed files with 193 additions and 182 deletions
+16 -16
View File
@@ -7,31 +7,31 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
)
func (srv *UserAuthTokenService) Run(ctx context.Context) error {
func (s *UserAuthTokenService) Run(ctx context.Context) error {
var err error
ticker := time.NewTicker(time.Hour)
maxInactiveLifetime := srv.Cfg.LoginMaxInactiveLifetime
maxLifetime := srv.Cfg.LoginMaxLifetime
maxInactiveLifetime := s.Cfg.LoginMaxInactiveLifetime
maxLifetime := s.Cfg.LoginMaxLifetime
err = srv.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func() {
if _, err := srv.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
srv.log.Error("An error occurred while deleting expired tokens", "err", err)
err = s.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func() {
if _, err := s.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
s.log.Error("An error occurred while deleting expired tokens", "err", err)
}
})
if err != nil {
srv.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
s.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
}
for {
select {
case <-ticker.C:
err = srv.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func() {
if _, err := srv.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
srv.log.Error("An error occurred while deleting expired tokens", "err", err)
err = s.ServerLockService.LockAndExecute(ctx, "cleanup expired auth tokens", time.Hour*12, func() {
if _, err := s.deleteExpiredTokens(ctx, maxInactiveLifetime, maxLifetime); err != nil {
s.log.Error("An error occurred while deleting expired tokens", "err", err)
}
})
if err != nil {
srv.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
s.log.Error("failed to lock and execute cleanup of expired auth token", "error", err)
}
case <-ctx.Done():
@@ -40,14 +40,14 @@ func (srv *UserAuthTokenService) Run(ctx context.Context) error {
}
}
func (srv *UserAuthTokenService) deleteExpiredTokens(ctx context.Context, maxInactiveLifetime, maxLifetime time.Duration) (int64, error) {
func (s *UserAuthTokenService) deleteExpiredTokens(ctx context.Context, maxInactiveLifetime, maxLifetime time.Duration) (int64, error) {
createdBefore := getTime().Add(-maxLifetime)
rotatedBefore := getTime().Add(-maxInactiveLifetime)
srv.log.Debug("starting cleanup of expired auth tokens", "createdBefore", createdBefore, "rotatedBefore", rotatedBefore)
s.log.Debug("starting cleanup of expired auth tokens", "createdBefore", createdBefore, "rotatedBefore", rotatedBefore)
var affected int64
err := srv.SQLStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
err := s.SQLStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
sql := `DELETE from user_auth_token WHERE created_at <= ? OR rotated_at <= ?`
res, err := dbSession.Exec(sql, createdBefore.Unix(), rotatedBefore.Unix())
if err != nil {
@@ -56,11 +56,11 @@ func (srv *UserAuthTokenService) deleteExpiredTokens(ctx context.Context, maxIna
affected, err = res.RowsAffected()
if err != nil {
srv.log.Error("failed to cleanup expired auth tokens", "error", err)
s.log.Error("failed to cleanup expired auth tokens", "error", err)
return nil
}
srv.log.Debug("cleanup of expired auth tokens done", "count", affected)
s.log.Debug("cleanup of expired auth tokens done", "count", affected)
return nil
})