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

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