Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/broker/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (r *Receiver) serveHTTP(ctx context.Context, event cloudevents.Event, resp

// Remove the TTL attribute that is used by the Broker.
originalV2 := event.Context.AsV02()
ttl, present := originalV2.Extensions[V02TTLAttribute]
if !present {
ttl, ttlKey := GetTTL(event.Context)
if ttl == nil {
// Only messages sent by the Broker should be here. If the attribute isn't here, then the
// event wasn't sent by the Broker, so we can drop it.
r.logger.Warn("No TTL seen, dropping", zap.Any("triggerRef", triggerRef), zap.Any("event", event))
Expand All @@ -144,7 +144,7 @@ func (r *Receiver) serveHTTP(ctx context.Context, event cloudevents.Event, resp
// framework returns a 500 to the caller, so the Channel would send this repeatedly.
return nil
}
delete(originalV2.Extensions, V02TTLAttribute)
delete(originalV2.Extensions, ttlKey)
event.Context = originalV2

r.logger.Debug("Received message", zap.Any("triggerRef", triggerRef))
Expand Down
14 changes: 14 additions & 0 deletions pkg/broker/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package broker

import (
"strings"

cloudevents "github.com/cloudevents/sdk-go"
)

Expand All @@ -26,6 +28,18 @@ const (
V02TTLAttribute = "knativebrokerttl"
)

// GetTTL finds the TTL in the EventContext using a case insensitive comparison
// for the key. The second return param, is the case preserved key that matched.
// Depending on the encoding/transport, the extension case could be changed.
func GetTTL(ctx cloudevents.EventContext) (interface{}, string) {
for k, v := range ctx.AsV02().Extensions {
if lower := strings.ToLower(k); lower == V02TTLAttribute {
return v, k
}
}
return nil, V02TTLAttribute
}

// SetTTL sets the TTL into the EventContext. ttl should be a positive integer.
func SetTTL(ctx cloudevents.EventContext, ttl interface{}) (cloudevents.EventContext, error) {
err := ctx.SetExtension(V02TTLAttribute, ttl)
Expand Down