package entity import ( "github.com/gogf/gf/os/glog" ) var g_runner *scheduleRunner // 定义一个结构体,用于携带函数和参数 type ScheduleFunc struct { Fn func(...interface{}) Args []interface{} } type scheduleRunner struct { // max schedule num maxScheduleCount int `json:"maxScheduleCount"` reqCh chan ScheduleFunc } func ReqChannel() chan ScheduleFunc { return Runner().reqCh } func (this scheduleRunner) SetMaxScheduleCount(count int) { this.maxScheduleCount = count } func (this scheduleRunner) init() { glog.Debug("scheduleRunner init") this.SetMaxScheduleCount(128) //default this.reqCh = make(chan ScheduleFunc) go func() { glog.Debug("func is running") for fun := range this.reqCh { fun.Fn(fun.Args...) } }() } func Runner() *scheduleRunner { if g_runner == nil { g_runner = &scheduleRunner{} g_runner.init() } return g_runner } func (this scheduleRunner) Run(fun ScheduleFunc) { glog.Debug("add func to run") this.reqCh <- fun }