fix: do not allow nil Callbacks functions

If user specifies a nil callback, then error out
rather than crashing at runtime due to a nil reference.
This commit is contained in:
Harry Bagdi 2019-07-29 11:27:28 -07:00
parent 3be827e912
commit 9dbbc652ef

View File

@ -89,6 +89,12 @@ func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
if lec.RetryPeriod < 1 {
return nil, fmt.Errorf("retryPeriod must be greater than zero")
}
if lec.Callbacks.OnStartedLeading == nil {
return nil, fmt.Errorf("OnStartedLeading callback must not be nil")
}
if lec.Callbacks.OnStoppedLeading == nil {
return nil, fmt.Errorf("OnStoppedLeading callback must not be nil")
}
if lec.Lock == nil {
return nil, fmt.Errorf("Lock must not be nil.")