// the goroutine cycle absolutely safe, no panic no error to quit. func ExampleWorkerContainer_safe() { var wc core.WorkerContainer wc.GFork("myservice", func(wc core.WorkerContainer) { defer func() { if r := recover(); r != nil { // log the r and ignore. return } }() for { select { case <-time.After(3 * time.Second): // select other channel, do something cycle to get error. if err := error(nil); err != nil { // recoverable error, log it only and continue or return. continue } case <-wc.QC(): // when got a quit signal, break the loop. // and must notify the container again for other workers // in container to quit. wc.Quit() return } } }) }
// the goroutine cycle notify container to quit when error. func ExampleWorkerContainer_fatal() { var wc core.WorkerContainer wc.GFork("myservice", func(wc core.WorkerContainer) { for { select { case <-time.After(3 * time.Second): // select other channel, do something cycle to get error. if err := error(nil); err != nil { // when got none-recoverable error, notify container to quit. wc.Quit() return } case <-wc.QC(): // when got a quit signal, break the loop. // and must notify the container again for other workers // in container to quit. wc.Quit() return } } }) }
func (s *Server) Initialize() (err error) { s.lock.Lock() defer s.lock.Unlock() if s.closed != StateInit { panic("server invalid state.") } // about the runtime. if err = s.initializeRuntime(); err != nil { return } // use worker container to fork. var wc core.WorkerContainer = s // reload goroutine wc.GFork("reload", core.Conf.ReloadCycle) // heartbeat goroutine wc.GFork("htbt(discovery)", s.htbt.discoveryCycle) wc.GFork("htbt(main)", s.htbt.beatCycle) // open rtmp agent. if err = s.rtmp.Open(); err != nil { core.Error.Println("open rtmp agent failed. err is", err) return } c := core.Conf l := fmt.Sprintf("%v(%v/%v)", c.Log.Tank, c.Log.Level, c.Log.File) if !c.LogToFile() { l = fmt.Sprintf("%v(%v)", c.Log.Tank, c.Log.Level) } core.Trace.Println(fmt.Sprintf("init server ok, conf=%v, log=%v, workers=%v/%v, gc=%v/%v%%, daemon=%v", c.Conf(), l, c.Workers, runtime.NumCPU(), c.Go.GcInterval, c.Go.GcPercent, c.Daemon)) // set to ready, requires cleanup. s.closed = StateReady return }