lock.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package cluster
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  6. )
  7. const (
  8. CLUSTER_STATE_TENANT_LOCK_PREFIX = "cluster_state_tenant_lock"
  9. CLUSTER_STATE_PLUGIN_LOCK_PREFIX = "cluster_state_plugin_lock"
  10. CLUSTER_UPDATE_NODE_STATUS_LOCK_PREFIX = "cluster_update_node_status_lock"
  11. )
  12. func (c *Cluster) LockTenant(tenant_id string) error {
  13. key := strings.Join([]string{CLUSTER_STATE_TENANT_LOCK_PREFIX, tenant_id}, ":")
  14. return cache.Lock(key, time.Second*5, time.Second)
  15. }
  16. func (c *Cluster) UnlockTenant(tenant_id string) error {
  17. key := strings.Join([]string{CLUSTER_STATE_TENANT_LOCK_PREFIX, tenant_id}, ":")
  18. return cache.Unlock(key)
  19. }
  20. func (c *Cluster) LockPlugin(plugin_id string) error {
  21. key := strings.Join([]string{CLUSTER_STATE_PLUGIN_LOCK_PREFIX, plugin_id}, ":")
  22. return cache.Lock(key, time.Second*5, time.Second)
  23. }
  24. func (c *Cluster) UnlockPlugin(plugin_id string) error {
  25. key := strings.Join([]string{CLUSTER_STATE_PLUGIN_LOCK_PREFIX, plugin_id}, ":")
  26. return cache.Unlock(key)
  27. }
  28. func (c *Cluster) LockNodeStatus(node_id string) error {
  29. key := strings.Join([]string{CLUSTER_UPDATE_NODE_STATUS_LOCK_PREFIX, node_id}, ":")
  30. return cache.Lock(key, time.Second*5, time.Second)
  31. }
  32. func (c *Cluster) UnlockNodeStatus(node_id string) error {
  33. key := strings.Join([]string{CLUSTER_UPDATE_NODE_STATUS_LOCK_PREFIX, node_id}, ":")
  34. return cache.Unlock(key)
  35. }