kra-new/internal/integration/storage/reloadable.go

69 lines
1.7 KiB
Go

package storage
import (
"context"
"io"
"kra/internal/biz/system"
"sync"
"kra/internal/conf"
)
type Reloadable struct {
mu sync.RWMutex
current system.FileStorage
}
func NewFileStorage(runtime *conf.Runtime) (*Reloadable, error) {
var config *conf.AdminBackend
if runtime != nil {
config = runtime.Admin()
}
return NewReloadable(config)
}
func NewReloadable(config *conf.AdminBackend) (*Reloadable, error) {
current, err := New(config)
if err != nil {
return nil, err
}
return &Reloadable{current: current}, nil
}
func (s *Reloadable) Replace(current system.FileStorage) {
s.mu.Lock()
s.current = current
s.mu.Unlock()
}
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.Put(ctx, name, reader)
}
func (s *Reloadable) Open(ctx context.Context, name string) (io.ReadCloser, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.Open(ctx, name)
}
func (s *Reloadable) Delete(ctx context.Context, name string) error {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.Delete(ctx, name)
}
func (s *Reloadable) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.Compose(ctx, names, destination)
}
func (s *Reloadable) DeletePrefix(ctx context.Context, prefix string) error {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.DeletePrefix(ctx, prefix)
}
func (s *Reloadable) List(ctx context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.current.List(ctx, prefix, cursor, limit)
}