-
Notifications
You must be signed in to change notification settings - Fork 630
InMemoryChannel Dispatcher refactor #1933
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
Merged
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
b45ddce
renaming to event dispatcher and event receiver
nachocano be6e780
pass on event dispatcher
nachocano 6eb5eeb
not working
nachocano 9edd50f
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano 467801a
updates
nachocano 584d1c3
compiling test
nachocano 041bf9c
updating UTs
nachocano 730cd8e
more updates
nachocano eed350a
updates
nachocano 97bc2fe
updates
nachocano 683e59f
updates
nachocano cc85cff
more tests passing
nachocano 0b0874d
updates
nachocano 3713490
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano f45745e
updates
nachocano 6a46530
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano 2aa2aff
updates
nachocano d1e9768
fixing tests
nachocano e29ac91
updates
nachocano fbc4c35
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano 53440f1
updates
nachocano 6c1f12b
updates
nachocano 4fb8587
updates
nachocano 200237f
imc refactor
nachocano a54c854
Update pkg/utils/context.go
nachocano 4650d15
renaming
nachocano 7fb687c
Merge branch 'imc-dispatcher' of github.com:nachocano/eventing into i…
nachocano d668535
skipping as of now until we fix it
nachocano 65b09f1
Merge branch 'imc-dispatcher' of github.com:nachocano/eventing into i…
nachocano dd7e498
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano cfb3d5b
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano 1800e06
review comments
nachocano 942675f
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano d8463ac
fixing e2e
nachocano e37fe06
merge
nachocano 137fe50
Merge branch 'imc-dispatcher' of github.com:nachocano/eventing into i…
nachocano ed6205e
Update pkg/utils/context.go
nachocano 5af3284
bad commit
nachocano 6aa7987
fixing cloudevents
nachocano 93a3cfd
address some of the comments after review
nachocano 96f968a
updates
nachocano 59ef1bb
commenting out part of e2e
nachocano 143e92c
missing TODO in the code
nachocano 3343373
updates
nachocano 51ca89b
Merge remote-tracking branch 'upstream/master' into imc-dispatcher
nachocano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright 2018 The Knative Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package channel | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| cloudevents "github.com/cloudevents/sdk-go" | ||
| cehttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" | ||
| "go.opencensus.io/plugin/ochttp/propagation/b3" | ||
| "go.opencensus.io/trace" | ||
| "go.uber.org/zap" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "knative.dev/eventing/pkg/kncloudevents" | ||
| "knative.dev/eventing/pkg/utils" | ||
| ) | ||
|
|
||
| const correlationIDHeaderName = "Knative-Correlation-Id" | ||
|
|
||
| type Dispatcher interface { | ||
| // DispatchEvent dispatches an event to a destination over HTTP. | ||
| // | ||
| // The destination and reply are URLs. | ||
| DispatchEvent(ctx context.Context, event cloudevents.Event, destination, reply string) error | ||
| } | ||
|
|
||
| // EventDispatcher is the 'real' Dispatcher used everywhere except unit tests. | ||
| var _ Dispatcher = &EventDispatcher{} | ||
|
|
||
| var propagation = &b3.HTTPFormat{} | ||
|
|
||
| // EventDispatcher dispatches events to a destination over HTTP. | ||
| type EventDispatcher struct { | ||
| ceClient cloudevents.Client | ||
| supportedSchemes sets.String | ||
|
|
||
| logger *zap.Logger | ||
| } | ||
|
|
||
| // NewEventDispatcher creates a new event dispatcher that can dispatch | ||
| // events to HTTP destinations. | ||
| func NewEventDispatcher(logger *zap.Logger) *EventDispatcher { | ||
| ceClient, err := kncloudevents.NewDefaultClient() | ||
| if err != nil { | ||
| logger.Fatal("failed to create cloudevents client", zap.Error(err)) | ||
| } | ||
| return &EventDispatcher{ | ||
| ceClient: ceClient, | ||
| supportedSchemes: sets.NewString("http", "https"), | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| // DispatchEvent dispatches an event to a destination over HTTP. | ||
| // | ||
| // The destination and reply are URLs. | ||
| func (d *EventDispatcher) DispatchEvent(ctx context.Context, event cloudevents.Event, destination, reply string) error { | ||
| var err error | ||
| // Default to replying with the original event. If there is a destination, then replace it | ||
| // with the response from the call to the destination instead. | ||
| response := &event | ||
| if destination != "" { | ||
| destinationURL := d.resolveURL(destination) | ||
| ctx, response, err = d.executeRequest(ctx, destinationURL, event) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to complete request to %s: %v", destinationURL, err) | ||
| } | ||
| } | ||
|
|
||
| if reply == "" && response != nil { | ||
| d.logger.Debug("cannot forward response as reply is empty", zap.Any("response", response)) | ||
| return nil | ||
| } | ||
|
|
||
| if reply != "" && response != nil { | ||
| replyURL := d.resolveURL(reply) | ||
| _, _, err = d.executeRequest(ctx, replyURL, *response) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to forward reply to %s: %v", replyURL, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (d *EventDispatcher) executeRequest(ctx context.Context, url *url.URL, event cloudevents.Event) (context.Context, *cloudevents.Event, error) { | ||
| d.logger.Debug("Dispatching event", zap.String("event.id", event.ID()), zap.String("url", url.String())) | ||
|
|
||
| tctx := cloudevents.HTTPTransportContextFrom(ctx) | ||
| sctx := utils.ContextFrom(tctx, url) | ||
| sctx = addOutGoingTracing(sctx, url) | ||
|
|
||
| rctx, reply, err := d.ceClient.Send(sctx, event) | ||
| if err != nil { | ||
| return rctx, nil, err | ||
| } | ||
| rtctx := cloudevents.HTTPTransportContextFrom(rctx) | ||
| if isFailure(rtctx.StatusCode) { | ||
| // Reject non-successful responses. | ||
| return rctx, nil, fmt.Errorf("unexpected HTTP response, expected 2xx, got %d", rtctx.StatusCode) | ||
| } | ||
| headers := utils.PassThroughHeaders(rtctx.Header) | ||
| if correlationID, ok := tctx.Header[correlationIDHeaderName]; ok { | ||
| headers[correlationIDHeaderName] = correlationID | ||
| } | ||
| rtctx.Header = http.Header(headers) | ||
| rctx = cehttp.WithTransportContext(rctx, rtctx) | ||
| return rctx, reply, nil | ||
| } | ||
|
|
||
| func addOutGoingTracing(ctx context.Context, url *url.URL) context.Context { | ||
| tctx := cloudevents.HTTPTransportContextFrom(ctx) | ||
| // Creating a dummy request to leverage propagation.SpanContextFromRequest method. | ||
| req := &http.Request{ | ||
| Header: tctx.Header, | ||
| } | ||
| // TODO use traceparent header. Issue: https://github.com/knative/eventing/issues/1951 | ||
| // Attach the Span context that is currently saved in the request's headers. | ||
| if sc, ok := propagation.SpanContextFromRequest(req); ok { | ||
|
nachocano marked this conversation as resolved.
|
||
| newCtx, _ := trace.StartSpanWithRemoteParent(ctx, url.Path, sc) | ||
| return newCtx | ||
| } | ||
| return ctx | ||
| } | ||
|
|
||
| // isFailure returns true if the status code is not a successful HTTP status. | ||
| func isFailure(statusCode int) bool { | ||
| return statusCode < http.StatusOK /* 200 */ || | ||
| statusCode >= http.StatusMultipleChoices /* 300 */ | ||
| } | ||
|
|
||
| func (d *EventDispatcher) resolveURL(destination string) *url.URL { | ||
| if url, err := url.Parse(destination); err == nil && d.supportedSchemes.Has(url.Scheme) { | ||
| // Already a URL with a known scheme. | ||
| return url | ||
| } | ||
| return &url.URL{ | ||
| Scheme: "http", | ||
| Host: destination, | ||
| Path: "/", | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.