service.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/upload/conf"
  6. "go-common/app/interface/main/upload/dao"
  7. "go-common/app/interface/main/upload/model"
  8. "go-common/library/log"
  9. )
  10. // Service .
  11. type Service struct {
  12. dao *dao.Dao
  13. bfs *dao.Bfs
  14. c *conf.Config
  15. bucketCache map[string]*model.Bucket
  16. }
  17. // New .
  18. func New(c *conf.Config) *Service {
  19. s := &Service{
  20. dao: dao.NewDao(c),
  21. bfs: dao.NewBfs(c),
  22. c: c,
  23. bucketCache: make(map[string]*model.Bucket),
  24. }
  25. go s.cacheproc()
  26. return s
  27. }
  28. // Ping .
  29. func (s *Service) Ping(c context.Context) (err error) {
  30. return
  31. }
  32. func (s *Service) cacheproc() {
  33. for {
  34. s.loadBucketCache()
  35. time.Sleep(5 * time.Minute)
  36. }
  37. }
  38. func (s *Service) loadBucketCache() {
  39. var (
  40. bMap map[string]*model.Bucket
  41. err error
  42. )
  43. if bMap, err = s.dao.Buckets(); err != nil {
  44. log.Error("get bucket meta failed! error(%v)", err)
  45. return
  46. }
  47. s.bucketCache = bMap
  48. }
  49. // GetRateLimit return rate limit of bucket and dir
  50. func (s *Service) GetRateLimit(bucket, dir string) (model.DirRateConfig, bool) {
  51. b, ok := s.bucketCache[bucket]
  52. if !ok {
  53. return model.DirRateConfig{}, false
  54. }
  55. config, ok := b.DirLimit[dir]
  56. if config == nil {
  57. return model.DirRateConfig{}, false
  58. }
  59. return config.Rate, ok
  60. }