github.com/sandwich-go/boost@v1.3.29/xpanic/recover.go (about)

     1  package xpanic
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // AutoRecover 执行 f ,当 f 发生panic,启动新的协程重复 AutoRecover.
     8  func AutoRecover(tag string, f func(), opts ...AutoRecoverOption) {
     9  	defer func() {
    10  		if reason := recover(); reason != nil {
    11  			cc := NewAutoRecoverOptions(opts...)
    12  			if cc.OnRecover != nil {
    13  				cc.OnRecover(tag, reason)
    14  			}
    15  			if cc.DelayTime > 0 {
    16  				time.Sleep(cc.DelayTime)
    17  			}
    18  			go AutoRecover(tag, f, opts...)
    19  		}
    20  	}()
    21  	f()
    22  }