You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

package.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package lockprovider
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/utils/lo2"
  5. "gitlink.org.cn/cloudream/jcs-pub/client/internal/publock/types"
  6. )
  7. const (
  8. PackageLockPathPrefix = "Package"
  9. PackageStorageIDPathIndex = 1
  10. PackageBuzyLock = "Buzy"
  11. PackagePinLock = "Pin"
  12. )
  13. type PackageLock struct {
  14. stgLocks map[string]*PackageStorageLock
  15. dummyLock *PackageStorageLock
  16. }
  17. func NewPackageLock() *PackageLock {
  18. return &PackageLock{
  19. stgLocks: make(map[string]*PackageStorageLock),
  20. dummyLock: NewPackageStorageLock(),
  21. }
  22. }
  23. // CanLock 判断这个锁能否锁定成功
  24. func (l *PackageLock) CanLock(lock types.Lock) error {
  25. nodeLock, ok := l.stgLocks[lock.Path[PackageStorageIDPathIndex]]
  26. if !ok {
  27. // 不能直接返回nil,因为如果锁数据的格式不对,也不能获取锁。
  28. // 这里使用一个空Provider来进行检查。
  29. return l.dummyLock.CanLock(lock)
  30. }
  31. return nodeLock.CanLock(lock)
  32. }
  33. // 锁定。在内部可以不用判断能否加锁,外部需要保证调用此函数前调用了CanLock进行检查
  34. func (l *PackageLock) Lock(reqID types.RequestID, lock types.Lock) error {
  35. stgID := lock.Path[PackageStorageIDPathIndex]
  36. nodeLock, ok := l.stgLocks[stgID]
  37. if !ok {
  38. nodeLock = NewPackageStorageLock()
  39. l.stgLocks[stgID] = nodeLock
  40. }
  41. return nodeLock.Lock(reqID, lock)
  42. }
  43. // 解锁
  44. func (l *PackageLock) Unlock(reqID types.RequestID, lock types.Lock) error {
  45. stgID := lock.Path[PackageStorageIDPathIndex]
  46. nodeLock, ok := l.stgLocks[stgID]
  47. if !ok {
  48. return nil
  49. }
  50. return nodeLock.Unlock(reqID, lock)
  51. }
  52. // Clear 清除内部所有状态
  53. func (l *PackageLock) Clear() {
  54. l.stgLocks = make(map[string]*PackageStorageLock)
  55. }
  56. type PackageStorageLock struct {
  57. buzyReqIDs []types.RequestID
  58. pinReqIDs []types.RequestID
  59. lockCompatibilityTable *LockCompatibilityTable
  60. }
  61. func NewPackageStorageLock() *PackageStorageLock {
  62. compTable := &LockCompatibilityTable{}
  63. sdLock := PackageStorageLock{
  64. lockCompatibilityTable: compTable,
  65. }
  66. compTable.
  67. Column(PackageBuzyLock, func() bool { return len(sdLock.buzyReqIDs) > 0 }).
  68. Column(PackagePinLock, func() bool { return len(sdLock.pinReqIDs) > 0 })
  69. comp := LockCompatible()
  70. uncp := LockUncompatible()
  71. compTable.MustRow(comp, uncp)
  72. compTable.MustRow(uncp, comp)
  73. return &sdLock
  74. }
  75. // CanLock 判断这个锁能否锁定成功
  76. func (l *PackageStorageLock) CanLock(lock types.Lock) error {
  77. return l.lockCompatibilityTable.Test(lock)
  78. }
  79. // 锁定
  80. func (l *PackageStorageLock) Lock(reqID types.RequestID, lock types.Lock) error {
  81. switch lock.Name {
  82. case PackageBuzyLock:
  83. l.buzyReqIDs = append(l.buzyReqIDs, reqID)
  84. case PackagePinLock:
  85. l.pinReqIDs = append(l.pinReqIDs, reqID)
  86. default:
  87. return fmt.Errorf("unknow lock name: %s", lock.Name)
  88. }
  89. return nil
  90. }
  91. // 解锁
  92. func (l *PackageStorageLock) Unlock(reqID types.RequestID, lock types.Lock) error {
  93. switch lock.Name {
  94. case PackageBuzyLock:
  95. l.buzyReqIDs = lo2.Remove(l.buzyReqIDs, reqID)
  96. case PackagePinLock:
  97. l.pinReqIDs = lo2.Remove(l.pinReqIDs, reqID)
  98. default:
  99. return fmt.Errorf("unknow lock name: %s", lock.Name)
  100. }
  101. return nil
  102. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。