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.

connectivity.go 2.0 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package metacache
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  8. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  9. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  10. )
  11. func (m *MetaCacheHost) AddConnectivity() *Connectivity {
  12. cache := &Connectivity{
  13. entries: make(map[jcstypes.HubID]*ConnectivityEntry),
  14. }
  15. m.caches = append(m.caches, cache)
  16. return cache
  17. }
  18. type Connectivity struct {
  19. lock sync.RWMutex
  20. entries map[jcstypes.HubID]*ConnectivityEntry
  21. }
  22. func (c *Connectivity) Get(from jcstypes.HubID, to jcstypes.HubID) *time.Duration {
  23. for i := 0; i < 2; i++ {
  24. c.lock.RLock()
  25. entry, ok := c.entries[from]
  26. if ok {
  27. con, ok := entry.To[to]
  28. if ok {
  29. c.lock.RUnlock()
  30. if con.Latency == nil {
  31. return nil
  32. }
  33. l := time.Millisecond * time.Duration(*con.Latency)
  34. return &l
  35. }
  36. }
  37. c.lock.RUnlock()
  38. c.load(from)
  39. }
  40. return nil
  41. }
  42. func (c *Connectivity) ClearOutdated() {
  43. c.lock.Lock()
  44. defer c.lock.Unlock()
  45. for hubID, entry := range c.entries {
  46. if time.Since(entry.UpdateTime) > time.Minute*5 {
  47. delete(c.entries, hubID)
  48. }
  49. }
  50. }
  51. func (c *Connectivity) load(hubID jcstypes.HubID) {
  52. coorCli := stgglb.CoordinatorRPCPool.Get()
  53. defer coorCli.Release()
  54. get, cerr := coorCli.GetHubConnectivities(context.Background(), corrpc.ReqGetHubConnectivities([]jcstypes.HubID{hubID}))
  55. if cerr != nil {
  56. logger.Warnf("get hub connectivities: %v", cerr)
  57. return
  58. }
  59. c.lock.Lock()
  60. defer c.lock.Unlock()
  61. ce := &ConnectivityEntry{
  62. From: hubID,
  63. To: make(map[jcstypes.HubID]jcstypes.HubConnectivity),
  64. UpdateTime: time.Now(),
  65. }
  66. for _, conn := range get.Connectivities {
  67. ce.To[conn.ToHubID] = conn
  68. }
  69. c.entries[hubID] = ce
  70. }
  71. type ConnectivityEntry struct {
  72. From jcstypes.HubID
  73. To map[jcstypes.HubID]jcstypes.HubConnectivity
  74. UpdateTime time.Time
  75. }

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