Skip to content

Commit

Permalink
[release-1.14] Copy message before passing it to eventtype autocreate…
Browse files Browse the repository at this point in the history
… handler (#7937)

Copy message before passing it to eventtype autocreate handler

Co-authored-by: Christoph Stäbler <[email protected]>
  • Loading branch information
knative-prow-robot and creydr authored May 22, 2024
1 parent 3ac9df6 commit 7d97879
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions pkg/kncloudevents/event_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
"net/http"
"time"

"github.com/cloudevents/sdk-go/v2/binding/buffering"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/buffering"
"github.com/cloudevents/sdk-go/v2/event"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
"github.com/hashicorp/go-retryablehttp"
Expand Down Expand Up @@ -248,7 +249,11 @@ func (d *Dispatcher) send(ctx context.Context, message binding.Message, destinat
messagesToFinish = append(messagesToFinish, responseMessage)

if config.eventTypeAutoHandler != nil {
d.handleAutocreate(ctx, responseMessage, config)
// messages can only be read once, so we need to make a copy of it
responseMessage, err = buffering.CopyMessage(ctx, responseMessage)
if err == nil {
d.handleAutocreate(ctx, responseMessage, config)
}
}

if config.reply == nil {
Expand Down Expand Up @@ -354,15 +359,8 @@ func (d *Dispatcher) executeRequest(ctx context.Context, target duckv1.Addressab
return ctx, responseMessage, &dispatchInfo, nil
}

func (d *Dispatcher) handleAutocreate(ctx context.Context, responseMessage binding.Message, config *senderConfig) {
// messages can only be read once, so we need to make a copy of it
messageCopy, err := buffering.CopyMessage(ctx, responseMessage)
if err != nil {
return
}
defer responseMessage.Finish(nil)

responseEvent, err := binding.ToEvent(ctx, messageCopy)
func (d *Dispatcher) handleAutocreate(ctx context.Context, msg binding.Message, config *senderConfig) {
responseEvent, err := binding.ToEvent(ctx, msg)
if err != nil {
return
}
Expand Down

0 comments on commit 7d97879

Please sign in to comment.