feat: add BVK exchange library
This commit is contained in:
47
store_memory.go
Normal file
47
store_memory.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package mila
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
state ContentState
|
||||
ready bool
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(ctx context.Context) (ContentState, error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ContentState{}, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
if !s.ready {
|
||||
return ContentState{}, ErrNotReady
|
||||
}
|
||||
return s.state, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Set(ctx context.Context, state 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