61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
|
|
package objectstorage
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"sync"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ReloadableClient struct {
|
||
|
|
mu sync.RWMutex
|
||
|
|
current Client
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewReloadableClient(current Client) *ReloadableClient {
|
||
|
|
return &ReloadableClient{current: current}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Update(next Client) {
|
||
|
|
if c == nil || next == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.mu.Lock()
|
||
|
|
defer c.mu.Unlock()
|
||
|
|
c.current = next
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Validate() error {
|
||
|
|
return c.snapshot().Validate()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) PutBytes(ctx context.Context, objectKey string, content []byte, contentType string) error {
|
||
|
|
return c.snapshot().PutBytes(ctx, objectKey, content, contentType)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) GetBytes(ctx context.Context, objectKey string) ([]byte, error) {
|
||
|
|
return c.snapshot().GetBytes(ctx, objectKey)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Exists(ctx context.Context, objectKey string) (bool, error) {
|
||
|
|
return c.snapshot().Exists(ctx, objectKey)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Delete(ctx context.Context, objectKey string) error {
|
||
|
|
return c.snapshot().Delete(ctx, objectKey)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) PublicURL(objectKey string) (string, error) {
|
||
|
|
return c.snapshot().PublicURL(objectKey)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) snapshot() Client {
|
||
|
|
if c == nil {
|
||
|
|
return disabledClient{reason: "object storage client is nil"}
|
||
|
|
}
|
||
|
|
c.mu.RLock()
|
||
|
|
defer c.mu.RUnlock()
|
||
|
|
if c.current == nil {
|
||
|
|
return disabledClient{reason: "object storage client is not initialized"}
|
||
|
|
}
|
||
|
|
return c.current
|
||
|
|
}
|