refactor: разнести пакеты и обновить валидацию
This commit is contained in:
49
storage/memory.go
Normal file
49
storage/memory.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"gitea.unprism.ru/KRBL/mila/protocol"
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
state protocol.ContentState
|
||||
ready bool
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(ctx context.Context) (protocol.ContentState, error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return protocol.ContentState{}, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
if !s.ready {
|
||||
return protocol.ContentState{}, protocol.ErrNotReady
|
||||
}
|
||||
return s.state, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Set(ctx context.Context, state protocol.ContentState) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.state = state
|
||||
s.ready = true
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user