| 273 | } |
| 274 | |
| 275 | func (n *Nylon) mainLoop() error { |
| 276 | n.Log.Debug("started main loop") |
| 277 | for { |
| 278 | select { |
| 279 | case fun := <-n.DispatchChannel: |
| 280 | if fun == nil { |
| 281 | goto endLoop |
| 282 | } |
| 283 | //n.Log.Debug("start") |
| 284 | start := time.Now() |
| 285 | err := fun() |
| 286 | if err != nil { |
| 287 | n.Log.Error("error occurred during dispatch: ", "error", err) |
| 288 | n.Cancel(err) |
| 289 | } |
| 290 | elapsed := time.Since(start) |
| 291 | perf.DispatchLatency.Add(float64(elapsed.Microseconds())) |
| 292 | if elapsed > time.Millisecond*4 { |
| 293 | n.Log.Warn("dispatch took a long time!", "fun", runtime.FuncForPC(reflect.ValueOf(fun).Pointer()).Name(), "elapsed", elapsed, "len", len(n.DispatchChannel)) |
| 294 | } |
| 295 | //n.Log.Debug("done", "elapsed", elapsed) |
| 296 | case <-n.Context.Done(): |
| 297 | goto endLoop |
| 298 | } |
| 299 | } |
| 300 | endLoop: |
| 301 | n.Log.Info("stopped main loop", "reason", context.Cause(n.Context).Error()) |
| 302 | n.Stop() |
| 303 | n.Log.Info("cleaning up modules") |
| 304 | err := n.Cleanup() |
| 305 | if err != nil { |
| 306 | n.Log.Error("error occurred during Stop: ", "error", err) |
| 307 | } |
| 308 | n.Log.Info("stopped") |
| 309 | return nil |
| 310 | } |
| 311 | |
| 312 | func (n *Nylon) Cleanup() error { |
| 313 | n.PingBuf.Stop() |