mirror of
https://github.com/cloudreve/cloudreve.git
synced 2026-03-03 02:57:01 +00:00
feat(storage): load balance storage policy (#2436)
This commit is contained in:
2
assets
2
assets
Submodule assets updated: 8e2c2bcff1...27996dc3ea
@@ -27,6 +27,7 @@ type (
|
|||||||
SkipStoragePolicyCache struct{}
|
SkipStoragePolicyCache struct{}
|
||||||
|
|
||||||
StoragePolicyClient interface {
|
StoragePolicyClient interface {
|
||||||
|
TxOperator
|
||||||
// GetByGroup returns the storage policies of the group.
|
// GetByGroup returns the storage policies of the group.
|
||||||
GetByGroup(ctx context.Context, group *ent.Group) (*ent.StoragePolicy, error)
|
GetByGroup(ctx context.Context, group *ent.Group) (*ent.StoragePolicy, error)
|
||||||
// GetPolicyByID returns the storage policy by id.
|
// GetPolicyByID returns the storage policy by id.
|
||||||
@@ -64,6 +65,14 @@ type storagePolicyClient struct {
|
|||||||
cache cache.Driver
|
cache cache.Driver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *storagePolicyClient) SetClient(newClient *ent.Client) TxOperator {
|
||||||
|
return &storagePolicyClient{client: newClient, cache: c.cache}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *storagePolicyClient) GetClient() *ent.Client {
|
||||||
|
return c.client
|
||||||
|
}
|
||||||
|
|
||||||
func (c *storagePolicyClient) Delete(ctx context.Context, policy *ent.StoragePolicy) error {
|
func (c *storagePolicyClient) Delete(ctx context.Context, policy *ent.StoragePolicy) error {
|
||||||
if err := c.client.StoragePolicy.DeleteOne(policy).Exec(ctx); err != nil {
|
if err := c.client.StoragePolicy.DeleteOne(policy).Exec(ctx); err != nil {
|
||||||
return fmt.Errorf("failed to delete storage policy: %w", err)
|
return fmt.Errorf("failed to delete storage policy: %w", err)
|
||||||
|
|||||||
@@ -294,11 +294,22 @@ func (service *UpdateStoragePolicyService) Update(c *gin.Context) (*GetStoragePo
|
|||||||
}
|
}
|
||||||
|
|
||||||
service.Policy.ID = idInt
|
service.Policy.ID = idInt
|
||||||
_, err = storagePolicyClient.Upsert(c, service.Policy)
|
|
||||||
|
sc, tx, ctx, err := inventory.WithTx(c, storagePolicyClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return nil, serializer.NewError(serializer.CodeDBError, "Failed to create transaction", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = sc.Upsert(ctx, service.Policy)
|
||||||
|
if err != nil {
|
||||||
|
_ = inventory.Rollback(tx)
|
||||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to update policy", err)
|
return nil, serializer.NewError(serializer.CodeDBError, "Failed to update policy", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := inventory.Commit(tx); err != nil {
|
||||||
|
return nil, serializer.NewError(serializer.CodeDBError, "Failed to commit transaction", err)
|
||||||
|
}
|
||||||
|
|
||||||
_ = dep.KV().Delete(manager.EntityUrlCacheKeyPrefix)
|
_ = dep.KV().Delete(manager.EntityUrlCacheKeyPrefix)
|
||||||
|
|
||||||
s := SingleStoragePolicyService{ID: idInt}
|
s := SingleStoragePolicyService{ID: idInt}
|
||||||
|
|||||||
Reference in New Issue
Block a user