优化结构
This commit is contained in:
@@ -22,7 +22,6 @@ type qiniuStorage struct {
|
||||
upload *qstorage.FormUploader
|
||||
manager *qstorage.BucketManager
|
||||
mac *qbox.Mac
|
||||
token string
|
||||
}
|
||||
|
||||
func newQiniuStorage(config *conf.AdminBackend_Qiniu) (system.FileStorage, error) {
|
||||
@@ -43,10 +42,14 @@ func newQiniuStorage(config *conf.AdminBackend_Qiniu) (system.FileStorage, error
|
||||
cfg.Zone = &qstorage.ZoneXinjiapo
|
||||
}
|
||||
mac := qbox.NewMac(config.AccessKey, config.SecretKey)
|
||||
policy := qstorage.PutPolicy{Scope: config.Bucket}
|
||||
token := policy.UploadToken(mac)
|
||||
return &qiniuStorage{config: config, upload: qstorage.NewFormUploader(&cfg), manager: qstorage.NewBucketManager(mac, &cfg), mac: mac, token: token}, nil
|
||||
return &qiniuStorage{config: config, upload: qstorage.NewFormUploader(&cfg), manager: qstorage.NewBucketManager(mac, &cfg), mac: mac}, nil
|
||||
}
|
||||
|
||||
func (s *qiniuStorage) uploadToken() string {
|
||||
policy := qstorage.PutPolicy{Scope: s.config.Bucket, Expires: 3600}
|
||||
return policy.UploadToken(s.mac)
|
||||
}
|
||||
|
||||
func (s *qiniuStorage) file(key string, size int64) *system.StoredFile {
|
||||
return &system.StoredFile{Name: path.Base(key), Path: key, URL: strings.TrimSuffix(s.config.BaseUrl, "/") + "/" + key, Size: size}
|
||||
}
|
||||
@@ -68,7 +71,7 @@ func (s *qiniuStorage) Put(ctx context.Context, name string, reader io.Reader) (
|
||||
}
|
||||
defer temporary.Close()
|
||||
ret := qstorage.PutRet{}
|
||||
if err = s.upload.Put(ctx, &ret, s.token, strings.TrimPrefix(name, "/"), temporary, size, &qstorage.PutExtra{}); err != nil {
|
||||
if err = s.upload.Put(ctx, &ret, s.uploadToken(), strings.TrimPrefix(name, "/"), temporary, size, &qstorage.PutExtra{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.file(ret.Key, size), nil
|
||||
|
||||
@@ -2,6 +2,7 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"sync"
|
||||
@@ -31,38 +32,66 @@ func NewReloadable(config *conf.AdminBackend) (*Reloadable, error) {
|
||||
}
|
||||
|
||||
func (s *Reloadable) Replace(current system.FileStorage) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.current = current
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
func (s *Reloadable) active() (system.FileStorage, error) {
|
||||
if s == nil {
|
||||
return nil, errors.New("file storage is unavailable")
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current.Put(ctx, name, reader)
|
||||
current := s.current
|
||||
s.mu.RUnlock()
|
||||
if current == nil {
|
||||
return nil, errors.New("file storage is unavailable")
|
||||
}
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func (s *Reloadable) Put(ctx context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return 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)
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return 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)
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return 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)
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return 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)
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return 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)
|
||||
current, err := s.active()
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
return current.List(ctx, prefix, cursor, limit)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
)
|
||||
|
||||
type blockingStorage struct {
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
}
|
||||
|
||||
func (s *blockingStorage) Put(context.Context, string, io.Reader) (*system.StoredFile, error) {
|
||||
close(s.started)
|
||||
<-s.release
|
||||
return &system.StoredFile{Name: "uploaded"}, nil
|
||||
}
|
||||
func (*blockingStorage) Open(context.Context, string) (io.ReadCloser, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
func (*blockingStorage) Delete(context.Context, string) error { return nil }
|
||||
func (*blockingStorage) Compose(context.Context, []string, string) (*system.StoredFile, string, error) {
|
||||
return nil, "", errors.New("not implemented")
|
||||
}
|
||||
func (*blockingStorage) DeletePrefix(context.Context, string) error { return nil }
|
||||
func (*blockingStorage) List(context.Context, string, string, int) ([]*system.StoredFile, string, bool, error) {
|
||||
return nil, "", false, errors.New("not implemented")
|
||||
}
|
||||
|
||||
type emptyStorage struct{}
|
||||
|
||||
func (*emptyStorage) Put(context.Context, string, io.Reader) (*system.StoredFile, error) {
|
||||
return &system.StoredFile{Name: "replacement"}, nil
|
||||
}
|
||||
func (*emptyStorage) Open(context.Context, string) (io.ReadCloser, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
func (*emptyStorage) Delete(context.Context, string) error { return nil }
|
||||
func (*emptyStorage) Compose(context.Context, []string, string) (*system.StoredFile, string, error) {
|
||||
return nil, "", errors.New("not implemented")
|
||||
}
|
||||
func (*emptyStorage) DeletePrefix(context.Context, string) error { return nil }
|
||||
func (*emptyStorage) List(context.Context, string, string, int) ([]*system.StoredFile, string, bool, error) {
|
||||
return nil, "", false, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func TestReloadableReplaceDoesNotWaitForInFlightOperation(t *testing.T) {
|
||||
old := &blockingStorage{started: make(chan struct{}), release: make(chan struct{})}
|
||||
reloadable := &Reloadable{current: old}
|
||||
putDone := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := reloadable.Put(context.Background(), "file", nil)
|
||||
putDone <- err
|
||||
}()
|
||||
select {
|
||||
case <-old.started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("storage operation did not start")
|
||||
}
|
||||
|
||||
replaced := make(chan struct{})
|
||||
go func() {
|
||||
reloadable.Replace(&emptyStorage{})
|
||||
close(replaced)
|
||||
}()
|
||||
select {
|
||||
case <-replaced:
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
t.Fatal("Replace waited for an in-flight storage operation")
|
||||
}
|
||||
|
||||
close(old.release)
|
||||
select {
|
||||
case err := <-putDone:
|
||||
if err != nil {
|
||||
t.Fatalf("Put() error = %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("storage operation did not finish")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReloadableNilStorageReturnsError(t *testing.T) {
|
||||
var reloadable *Reloadable
|
||||
if _, err := reloadable.Put(context.Background(), "file", nil); err == nil {
|
||||
t.Fatal("nil Reloadable.Put() succeeded")
|
||||
}
|
||||
if _, err := (&Reloadable{}).Open(context.Background(), "file"); err == nil {
|
||||
t.Fatal("empty Reloadable.Open() succeeded")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user