Add healthcheck to all stores

This commit is contained in:
Torjus Håkestad 2021-11-06 00:37:28 +01:00
parent a19fb3086b
commit 8ba601a2b1
5 changed files with 21 additions and 0 deletions

View File

@ -108,3 +108,7 @@ func (s *CachingStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
return attempts, err return attempts, err
} }
func (s *CachingStore) IsHealthy() bool {
return s.backend.IsHealthy()
}

View File

@ -142,6 +142,10 @@ func (ms *MemoryStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
return results, nil return results, nil
} }
func (s *MemoryStore) IsHealthy() bool {
return true
}
func toResults(m map[string]int) []StatsResult { func toResults(m map[string]int) []StatsResult {
var results []StatsResult var results []StatsResult

View File

@ -120,3 +120,7 @@ func (s *MetricsCollectingStore) Stats(statType LoginStats, limit int) ([]StatsR
func (s *MetricsCollectingStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) { func (s *MetricsCollectingStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) {
return s.store.Query(query) return s.store.Query(query)
} }
func (s *MetricsCollectingStore) IsHealthy() bool {
return s.IsHealthy()
}

View File

@ -126,6 +126,10 @@ func (s *PostgresStore) Stats(statType LoginStats, limit int) ([]StatsResult, er
return results, nil return results, nil
} }
func (s *PostgresStore) IsHealthy() bool {
return s.db.Ping() == nil
}
func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) { func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) {
uniquePasswordStmt := `select count(*) from (select distinct password from login_attempts) as temp` uniquePasswordStmt := `select count(*) from (select distinct password from login_attempts) as temp`
uniqueUsernameStmt := `select count(*) from (select distinct username from login_attempts) as temp` uniqueUsernameStmt := `select count(*) from (select distinct username from login_attempts) as temp`

View File

@ -35,4 +35,9 @@ type LoginAttemptStore interface {
All() ([]models.LoginAttempt, error) All() ([]models.LoginAttempt, error)
Stats(statType LoginStats, limit int) ([]StatsResult, error) Stats(statType LoginStats, limit int) ([]StatsResult, error)
Query(query AttemptQuery) ([]models.LoginAttempt, error) Query(query AttemptQuery) ([]models.LoginAttempt, error)
HealthCheker
}
type HealthCheker interface {
IsHealthy() bool
} }