@@ -215,7 +215,9 @@ func (h *IngressHandler) addEvent(responseWriter http.ResponseWriter, event *clo
215215 pr := & proxiedRequest {
216216 received : time .Now (),
217217 responseWriter : responseWriter ,
218- replyEvent : make (chan * cloudevents.Event , 1 ),
218+ // capacity must stay 1: handleReplyEvent relies on a full buffer to
219+ // detect and drop duplicate replies without blocking.
220+ replyEvent : make (chan * cloudevents.Event , 1 ),
219221 }
220222 if h .entries [rr .GetNamespacedName ()] == nil {
221223 h .entries [rr .GetNamespacedName ()] = make (map [string ]* proxiedRequest )
@@ -232,6 +234,14 @@ func (h *IngressHandler) deleteEvent(event *cloudevents.Event, rr *v1alpha1.Requ
232234 delete (h .entries [rr .GetNamespacedName ()], event .ID ())
233235}
234236
237+ func (h * IngressHandler ) getEvent (id string , rr * v1alpha1.RequestReply ) (* proxiedRequest , bool ) {
238+ h .requestLock .RLock ()
239+ defer h .requestLock .RUnlock ()
240+
241+ pr , ok := h .entries [rr .GetNamespacedName ()][id ]
242+ return pr , ok
243+ }
244+
235245func (h * IngressHandler ) handleNewEvent (ctx context.Context , responseWriter http.ResponseWriter , event * cloudevents.Event , rr * v1alpha1.RequestReply , headers http.Header ) {
236246 h .logger .Debug ("handling new event" )
237247 pr := h .addEvent (responseWriter , event , rr )
@@ -289,9 +299,6 @@ func (h *IngressHandler) handleNewEvent(ctx context.Context, responseWriter http
289299}
290300
291301func (h * IngressHandler ) handleReplyEvent (responseWriter http.ResponseWriter , event * cloudevents.Event , rr * v1alpha1.RequestReply ) {
292- h .requestLock .RLock ()
293- defer h .requestLock .RUnlock ()
294-
295302 h .logger .Debug ("handling a response event" )
296303
297304 // TODO: with OIDC enabled, we can skip validation of the key if we validate the identity of the trigger making the request
@@ -336,15 +343,17 @@ func (h *IngressHandler) handleReplyEvent(responseWriter http.ResponseWriter, ev
336343 return
337344 }
338345
339- responseWriter .WriteHeader (http .StatusAccepted )
340-
341346 id := strings .Split (replyIdString , ":" )[0 ]
342- pr , ok := h .entries [ rr . GetNamespacedName ()][ id ]
347+ pr , ok := h .getEvent ( id , rr )
343348 if ! ok {
344349 h .logger .Warn ("no event found matching the reply id, discarding event" , zap .String ("reply id" , id ))
345- return
350+ } else {
351+ select {
352+ case pr .replyEvent <- event :
353+ default :
354+ h .logger .Warn ("reply event already delivered or duplicate reply received, discarding event" , zap .String ("reply id" , id ))
355+ }
346356 }
347357
348- // send the reply event back to the original response writer
349- pr .replyEvent <- event
358+ responseWriter .WriteHeader (http .StatusAccepted )
350359}
0 commit comments