beacon/light/request: add checks for nil event callback function

This commit is contained in:
Zsolt Felfoldi 2024-06-06 15:15:08 +02:00
parent 766869ce39
commit d673c4875c

View file

@ -186,12 +186,16 @@ func (s *serverWithTimeout) eventCallback(event Event) {
// call will just do nothing // call will just do nothing
timer.Stop() timer.Stop()
delete(s.timeouts, id) delete(s.timeouts, id)
if s.childEventCb != nil {
s.childEventCb(event) s.childEventCb(event)
} }
}
default: default:
if s.childEventCb != nil {
s.childEventCb(event) s.childEventCb(event)
} }
} }
}
// startTimeout starts a timeout timer for the given request. // startTimeout starts a timeout timer for the given request.
func (s *serverWithTimeout) startTimeout(reqData RequestResponse) { func (s *serverWithTimeout) startTimeout(reqData RequestResponse) {
@ -211,11 +215,15 @@ func (s *serverWithTimeout) startTimeout(reqData RequestResponse) {
delete(s.timeouts, id) delete(s.timeouts, id)
childEventCb := s.childEventCb childEventCb := s.childEventCb
s.lock.Unlock() s.lock.Unlock()
if childEventCb != nil {
childEventCb(Event{Type: EvFail, Data: reqData}) childEventCb(Event{Type: EvFail, Data: reqData})
}
}) })
childEventCb := s.childEventCb childEventCb := s.childEventCb
s.lock.Unlock() s.lock.Unlock()
if childEventCb != nil {
childEventCb(Event{Type: EvTimeout, Data: reqData}) childEventCb(Event{Type: EvTimeout, Data: reqData})
}
}) })
} }
@ -229,7 +237,6 @@ func (s *serverWithTimeout) unsubscribe() {
timer.Stop() timer.Stop()
} }
} }
s.childEventCb = nil
s.parent.Unsubscribe() s.parent.Unsubscribe()
} }
@ -328,10 +335,10 @@ func (s *serverWithLimits) eventCallback(event Event) {
} }
childEventCb := s.childEventCb childEventCb := s.childEventCb
s.lock.Unlock() s.lock.Unlock()
if passEvent { if passEvent && childEventCb != nil {
childEventCb(event) childEventCb(event)
} }
if sendCanRequestAgain { if sendCanRequestAgain && childEventCb != nil {
childEventCb(Event{Type: EvCanRequestAgain}) childEventCb(Event{Type: EvCanRequestAgain})
} }
} }
@ -383,7 +390,7 @@ func (s *serverWithLimits) canRequestNow() bool {
} }
childEventCb := s.childEventCb childEventCb := s.childEventCb
s.lock.Unlock() s.lock.Unlock()
if sendCanRequestAgain { if sendCanRequestAgain && childEventCb != nil {
childEventCb(Event{Type: EvCanRequestAgain}) childEventCb(Event{Type: EvCanRequestAgain})
} }
return canRequest return canRequest
@ -415,7 +422,7 @@ func (s *serverWithLimits) delay(delay time.Duration) {
} }
childEventCb := s.childEventCb childEventCb := s.childEventCb
s.lock.Unlock() s.lock.Unlock()
if sendCanRequestAgain { if sendCanRequestAgain && childEventCb != nil {
childEventCb(Event{Type: EvCanRequestAgain}) childEventCb(Event{Type: EvCanRequestAgain})
} }
}) })