Change store All to use channel

This commit is contained in:
2022-08-26 12:21:52 +02:00
parent 4c77b5ef9b
commit 07fe16b72a
6 changed files with 52 additions and 22 deletions

View File

@@ -2,6 +2,8 @@ package store
import "git.t-juice.club/torjus/apiary/models"
var _ LoginAttemptStore = &CachingStore{}
type CachingStore struct {
backend LoginAttemptStore
@@ -33,14 +35,16 @@ func NewCachingStore(backend LoginAttemptStore) *CachingStore {
//TODO: Handle better maybe?
panic(err)
}
var loginCount int
cs.totalLoginsCount = len(all)
for _, attempt := range all {
for attempt := range all {
cs.uniqueUsernames[attempt.Username] = struct{}{}
cs.uniquePasswords[attempt.Password] = struct{}{}
cs.uniqueIPs[attempt.RemoteIP.String()] = struct{}{}
cs.uniqueCountries[attempt.Country] = struct{}{}
loginCount++
}
cs.totalLoginsCount = loginCount
return cs
}
@@ -58,7 +62,7 @@ func (s *CachingStore) AddAttempt(l *models.LoginAttempt) error {
return s.backend.AddAttempt(l)
}
func (s *CachingStore) All() ([]models.LoginAttempt, error) {
func (s *CachingStore) All() (<-chan models.LoginAttempt, error) {
return s.backend.All()
}