Browse Source

调整代码结构

gitlink
Sydonian 7 months ago
parent
commit
fd6b8c3c1f
2 changed files with 83 additions and 0 deletions
  1. +76
    -0
      client/internal/accessstat/access_stat.go
  2. +7
    -0
      client/internal/accessstat/config.go

+ 76
- 0
client/internal/accessstat/access_stat.go View File

@@ -0,0 +1,76 @@
package accessstat

import (
"fmt"
"sync"
"time"

"gitlink.org.cn/cloudream/common/pkgs/logger"
cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
"gitlink.org.cn/cloudream/common/utils/sync2"
stgglb "gitlink.org.cn/cloudream/storage2/common/globals"
coormq "gitlink.org.cn/cloudream/storage2/common/pkgs/mq/coordinator"
)

type AccessStatEvent interface{}

type AccessStat struct {
cfg Config
stats []coormq.AddAccessStatEntry
lock sync.Mutex
}

func NewAccessStat(cfg Config) *AccessStat {
return &AccessStat{
cfg: cfg,
}
}

func (p *AccessStat) AddAccessCounter(objID cdssdk.ObjectID, pkgID cdssdk.PackageID, stgID cdssdk.StorageID, value float64) {
p.lock.Lock()
defer p.lock.Unlock()

p.stats = append(p.stats, coormq.AddAccessStatEntry{
ObjectID: objID,
PackageID: pkgID,
StorageID: stgID,
Counter: value,
})
}

func (p *AccessStat) Start() *sync2.UnboundChannel[AccessStatEvent] {
ch := sync2.NewUnboundChannel[AccessStatEvent]()

go func() {
coorCli, err := stgglb.CoordinatorMQPool.Acquire()
if err != nil {
ch.Send(fmt.Errorf("new coordinator client: %w", err))
}
defer stgglb.CoordinatorMQPool.Release(coorCli)

ticker := time.NewTicker(p.cfg.ReportInterval)
for {
<-ticker.C

p.lock.Lock()
st := p.stats
p.stats = nil
p.lock.Unlock()

if len(st) == 0 {
continue
}

err := coorCli.AddAccessStat(coormq.ReqAddAccessStat(st))
if err != nil {
logger.Errorf("add all package access stat counter: %v", err)

p.lock.Lock()
p.stats = append(p.stats, st...)
p.lock.Unlock()
continue
}
}
}()
return ch
}

+ 7
- 0
client/internal/accessstat/config.go View File

@@ -0,0 +1,7 @@
package accessstat

import "time"

type Config struct {
ReportInterval time.Duration
}

Loading…
Cancel
Save