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.

manager.go 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package ioswitch
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/utils/lo2"
  8. )
  9. type finding struct {
  10. PlanID PlanID
  11. Callback *future.SetValueFuture[*Switch]
  12. }
  13. type Manager struct {
  14. lock sync.Mutex
  15. switchs map[PlanID]*Switch
  16. findings []*finding
  17. }
  18. func NewManager() Manager {
  19. return Manager{
  20. switchs: make(map[PlanID]*Switch),
  21. }
  22. }
  23. func (s *Manager) Add(sw *Switch) {
  24. s.lock.Lock()
  25. defer s.lock.Unlock()
  26. s.switchs[sw.Plan().ID] = sw
  27. s.findings = lo.Reject(s.findings, func(f *finding, idx int) bool {
  28. if f.PlanID != sw.Plan().ID {
  29. return false
  30. }
  31. f.Callback.SetValue(sw)
  32. return true
  33. })
  34. }
  35. func (s *Manager) Remove(sw *Switch) {
  36. s.lock.Lock()
  37. defer s.lock.Unlock()
  38. delete(s.switchs, sw.Plan().ID)
  39. }
  40. func (s *Manager) FindByID(id PlanID) *Switch {
  41. s.lock.Lock()
  42. defer s.lock.Unlock()
  43. return s.switchs[id]
  44. }
  45. func (s *Manager) FindByIDContexted(ctx context.Context, id PlanID) *Switch {
  46. s.lock.Lock()
  47. sw := s.switchs[id]
  48. if sw != nil {
  49. s.lock.Unlock()
  50. return sw
  51. }
  52. cb := future.NewSetValue[*Switch]()
  53. f := &finding{
  54. PlanID: id,
  55. Callback: cb,
  56. }
  57. s.findings = append(s.findings, f)
  58. s.lock.Unlock()
  59. sw, _ = cb.WaitValue(ctx)
  60. s.lock.Lock()
  61. defer s.lock.Unlock()
  62. s.findings = lo2.Remove(s.findings, f)
  63. return sw
  64. }

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