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

@@ -9,6 +9,8 @@ import (
"git.t-juice.club/torjus/apiary/models"
)
var _ LoginAttemptStore = &MemoryStore{}
type MemoryStore struct {
lock sync.RWMutex
attempts []models.LoginAttempt
@@ -32,8 +34,17 @@ func (ms *MemoryStore) AddAttempt(l *models.LoginAttempt) error {
return nil
}
func (ms *MemoryStore) All() ([]models.LoginAttempt, error) {
return ms.attempts, nil
func (ms *MemoryStore) All() (<-chan models.LoginAttempt, error) {
ch := make(chan models.LoginAttempt)
go func() {
ms.lock.RLock()
defer ms.lock.RUnlock()
for _, attempt := range ms.attempts {
ch <- attempt
}
close(ch)
}()
return ch, nil
}
func (ms *MemoryStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) {