go-common/app/job/main/member/service/block/service.go
2019-04-22 18:49:16 +08:00

115 lines
2.2 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package block
import (
"context"
"fmt"
"time"
"go-common/app/job/main/member/conf"
"go-common/app/job/main/member/dao/block"
"go-common/library/cache"
"go-common/library/log"
"go-common/library/queue/databus"
"github.com/pkg/errors"
)
// Service struct
type Service struct {
dao *block.Dao
conf *conf.Config
cache *cache.Cache
missch chan func()
creditSub *databus.Databus
}
// New init
func New(conf *conf.Config, dao *block.Dao, creditSub *databus.Databus) (s *Service) {
s = &Service{
dao: dao,
conf: conf,
cache: cache.New(1, 10240),
missch: make(chan func(), 10240),
creditSub: creditSub,
}
// 自动解禁检查
if s.conf.BlockProperty.Flag.ExpireCheck {
go s.limitcheckproc()
go s.creditcheckproc()
}
// 小黑屋答题状态订阅
if s.conf.BlockProperty.Flag.CreditSub {
go s.creditsubproc()
}
go s.missproc()
return s
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
return s.dao.Ping(c)
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
}
func (s *Service) limitcheckproc() {
defer func() {
if x := recover(); x != nil {
log.Error("service.limitcheckproc panic(%v)", x)
go s.limitcheckproc()
log.Info("service.limitcheckproc recover")
}
}()
for {
log.Info("limit check start")
s.limitExpireHandler(context.TODO())
log.Info("limit check end")
time.Sleep(time.Duration(s.conf.BlockProperty.LimitExpireCheckTick))
}
}
func (s *Service) creditcheckproc() {
defer func() {
if x := recover(); x != nil {
log.Error("%+v", errors.WithStack(fmt.Errorf("service.creditcheckproc panic(%v)", x)))
go s.creditcheckproc()
log.Info("service.creditcheckproc recover")
}
}()
for {
log.Info("black house check start")
s.creditExpireHandler(context.TODO())
log.Info("black house check end")
time.Sleep(time.Duration(s.conf.BlockProperty.CreditExpireCheckTick))
}
}
func (s *Service) missproc() {
defer func() {
if x := recover(); x != nil {
log.Error("service.missproc panic(%v)", x)
go s.missproc()
log.Info("service.missproc recover")
}
}()
for {
f := <-s.missch
f()
}
}
func (s *Service) mission(f func()) {
select {
case s.missch <- f:
default:
log.Error("s.missch full")
}
}