Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional use QueueSubscribe for NATS events source #3131

Merged
merged 13 commits into from
May 21, 2024
Merged
13 changes: 13 additions & 0 deletions api/event-source.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/event-source.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/jsonschema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,10 @@
"description": "Metadata holds the user defined metadata which will passed along the event payload.",
"type": "object"
},
"queue": {
"description": "Queue is the name of the queue group to subscribe as if specified. Uses QueueSubscribe logic to subscribe as queue group. If the queue is empty, uses default Subscribe logic.",
"type": "string"
},
"subject": {
"description": "Subject holds the name of the subject onto which messages are published",
"type": "string"
Expand Down
4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions eventsources/sources/nats/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt
log.Info("assuming all events have a json body...")
}

log.Info("subscribing to messages on the queue...")
_, err := conn.Subscribe(natsEventSource.Subject, func(msg *natslib.Msg) {
handler := func(msg *natslib.Msg) {
defer func(start time.Time) {
el.Metrics.EventProcessingDuration(el.GetEventSourceName(), el.GetEventName(), float64(time.Since(start)/time.Millisecond))
}(time.Now())
Expand All @@ -157,7 +156,17 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt
log.Errorw("failed to dispatch a NATS event", zap.Error(err))
el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName())
}
})
}

var err error
if natsEventSource.Queue != nil {
log.Infof("subscribing to messages on the subject %s queue %s", natsEventSource.Subject, *natsEventSource.Queue)
_, err = conn.QueueSubscribe(natsEventSource.Subject, *natsEventSource.Queue, handler)
} else {
log.Infof("subscribing to messages on the subject %s", natsEventSource.Subject)
_, err = conn.Subscribe(natsEventSource.Subject, handler)
}

if err != nil {
return fmt.Errorf("failed to subscribe to the subject %s for event source %s, %w", natsEventSource.Subject, el.GetEventName(), err)
}
Expand Down
3 changes: 3 additions & 0 deletions eventsources/sources/nats/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ func validate(eventSource *v1alpha1.NATSEventsSource) error {
if eventSource.TLS != nil {
return apicommon.ValidateTLSConfig(eventSource.TLS)
}
if eventSource.Queue != nil && *eventSource.Queue == "" {
return fmt.Errorf("queue group cannot be empty if specified")
}
return nil
}
9 changes: 9 additions & 0 deletions examples/event-sources/nats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ spec:
# name: my-secret
# key: my-credential

# example-queued-subscription:
# url: nats://nats.argo-events.svc:4222
# jsonBody: true
# subject: "foo"
# queue: "my-queue"
# auth:
# credential:
# name: my-secret
# key: my-credential
965 changes: 505 additions & 460 deletions pkg/apis/eventsource/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg/apis/eventsource/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/eventsource/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/eventsource/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ type NATSEventsSource struct {
// Filter
// +optional
Filter *EventSourceFilter `json:"filter,omitempty" protobuf:"bytes,8,opt,name=filter"`
// Queue is the name of the queue group to subscribe as if specified. Uses QueueSubscribe
// logic to subscribe as queue group. If the queue is empty, uses default Subscribe logic.
// +optional
Queue *string `json:"queue" protobuf:"bytes,9,opt,name=queue"`
}

// NATSAuth refers to the auth info for NATS EventSource
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/eventsource/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading