service.go 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/laser/conf"
  5. "go-common/app/interface/main/laser/dao"
  6. "go-common/library/log"
  7. "go-common/library/stat/prom"
  8. )
  9. type Service struct {
  10. conf *conf.Config
  11. dao *dao.Dao
  12. pCacheHit *prom.Prom
  13. pCacheMiss *prom.Prom
  14. missch chan func()
  15. }
  16. func New(c *conf.Config) (s *Service) {
  17. s = &Service{
  18. conf: c,
  19. dao: dao.New(c),
  20. pCacheHit: prom.CacheHit,
  21. pCacheMiss: prom.CacheMiss,
  22. missch: make(chan func(), 1024),
  23. }
  24. go s.cacheproc()
  25. return
  26. }
  27. func (s *Service) Ping(c context.Context) (err error) {
  28. return s.dao.Ping(c)
  29. }
  30. func (s *Service) Close() {
  31. s.dao.Close()
  32. }
  33. // AddCache add to chan for cache
  34. func (s *Service) addCache(f func()) {
  35. select {
  36. case s.missch <- f:
  37. default:
  38. log.Warn("cacheproc chan full")
  39. }
  40. }
  41. // cacheproc is a routine for execute closure.
  42. func (s *Service) cacheproc() {
  43. for {
  44. f := <-s.missch
  45. f()
  46. }
  47. }