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.

access_stat.go 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package accessstat
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  8. "gitlink.org.cn/cloudream/common/utils/sync2"
  9. stgglb "gitlink.org.cn/cloudream/storage2/common/globals"
  10. coormq "gitlink.org.cn/cloudream/storage2/common/pkgs/mq/coordinator"
  11. )
  12. type AccessStatEvent interface{}
  13. type AccessStat struct {
  14. cfg Config
  15. stats []coormq.AddAccessStatEntry
  16. lock sync.Mutex
  17. }
  18. func NewAccessStat(cfg Config) *AccessStat {
  19. return &AccessStat{
  20. cfg: cfg,
  21. }
  22. }
  23. func (p *AccessStat) AddAccessCounter(objID cdssdk.ObjectID, pkgID cdssdk.PackageID, stgID cdssdk.StorageID, value float64) {
  24. p.lock.Lock()
  25. defer p.lock.Unlock()
  26. p.stats = append(p.stats, coormq.AddAccessStatEntry{
  27. ObjectID: objID,
  28. PackageID: pkgID,
  29. StorageID: stgID,
  30. Counter: value,
  31. })
  32. }
  33. func (p *AccessStat) Start() *sync2.UnboundChannel[AccessStatEvent] {
  34. ch := sync2.NewUnboundChannel[AccessStatEvent]()
  35. go func() {
  36. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  37. if err != nil {
  38. ch.Send(fmt.Errorf("new coordinator client: %w", err))
  39. }
  40. defer stgglb.CoordinatorMQPool.Release(coorCli)
  41. ticker := time.NewTicker(p.cfg.ReportInterval)
  42. for {
  43. <-ticker.C
  44. p.lock.Lock()
  45. st := p.stats
  46. p.stats = nil
  47. p.lock.Unlock()
  48. if len(st) == 0 {
  49. continue
  50. }
  51. err := coorCli.AddAccessStat(coormq.ReqAddAccessStat(st))
  52. if err != nil {
  53. logger.Errorf("add all package access stat counter: %v", err)
  54. p.lock.Lock()
  55. p.stats = append(p.stats, st...)
  56. p.lock.Unlock()
  57. continue
  58. }
  59. }
  60. }()
  61. return ch
  62. }

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