@@ -19,6 +19,8 @@ package runtime
1919import (
2020 "fmt"
2121 "runtime"
22+ "sync"
23+ "time"
2224
2325 "github.com/golang/glog"
2426)
@@ -79,7 +81,15 @@ func getCallers(r interface{}) string {
7981
8082// ErrorHandlers is a list of functions which will be invoked when an unreturnable
8183// error occurs.
82- var ErrorHandlers = []func (error ){logError }
84+ // TODO(lavalamp): for testability, this and the below HandleError function
85+ // should be packaged up into a testable and reusable object.
86+ var ErrorHandlers = []func (error ){
87+ logError ,
88+ (& rudimentaryErrorBackoff {
89+ lastErrorTime : time .Now (),
90+ minPeriod : 500 * time .Millisecond ,
91+ }).OnError ,
92+ }
8393
8494// HandlerError is a method to invoke when a non-user facing piece of code cannot
8595// return an error and needs to indicate it has been ignored. Invoking this method
@@ -101,6 +111,26 @@ func logError(err error) {
101111 glog .ErrorDepth (2 , err )
102112}
103113
114+ type rudimentaryErrorBackoff struct {
115+ minPeriod time.Duration // immutable
116+ // TODO(lavalamp): use the clock for testability. Need to move that
117+ // package for that to be accessible here.
118+ lastErrorTimeLock sync.Mutex
119+ lastErrorTime time.Time
120+ }
121+
122+ // OnError will block if it is called more often than the embedded period time.
123+ // This will prevent overly tight hot error loops.
124+ func (r * rudimentaryErrorBackoff ) OnError (error ) {
125+ r .lastErrorTimeLock .Lock ()
126+ defer r .lastErrorTimeLock .Unlock ()
127+ d := time .Since (r .lastErrorTime )
128+ if d < r .minPeriod {
129+ time .Sleep (r .minPeriod - d )
130+ }
131+ r .lastErrorTime = time .Now ()
132+ }
133+
104134// GetCaller returns the caller of the function that calls it.
105135func GetCaller () string {
106136 var pc [1 ]uintptr
0 commit comments