-
Notifications
You must be signed in to change notification settings - Fork 224
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
Perf improvements in HTTP protocol #444
Merged
slinkydeveloper
merged 4 commits into
cloudevents:master
from
slinkydeveloper:trying_to_gain_cpu_cycles
Apr 7, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 contains 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,76 @@ | ||
package spec | ||
|
||
import ( | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
) | ||
|
||
type matchExactVersion struct { | ||
version | ||
} | ||
|
||
func (v *matchExactVersion) Attribute(name string) Attribute { return v.attrMap[name] } | ||
|
||
var _ Version = (*matchExactVersion)(nil) | ||
|
||
func newMatchExactVersionVersion( | ||
prefix string, | ||
attributeNameMatchMapper func(string) string, | ||
context event.EventContext, | ||
convert func(event.EventContextConverter) event.EventContext, | ||
attrs ...*attribute, | ||
) *matchExactVersion { | ||
v := &matchExactVersion{ | ||
version: version{ | ||
prefix: prefix, | ||
context: context, | ||
convert: convert, | ||
attrMap: map[string]Attribute{}, | ||
attrs: make([]Attribute, len(attrs)), | ||
}, | ||
} | ||
for i, a := range attrs { | ||
a.version = v | ||
v.attrs[i] = a | ||
v.attrMap[attributeNameMatchMapper(a.name)] = a | ||
} | ||
return v | ||
} | ||
|
||
// WithPrefixMatchExact returns a set of versions with prefix added to all attribute names. | ||
func WithPrefixMatchExact(attributeNameMatchMapper func(string) string, prefix string) *Versions { | ||
attr := func(name string, kind Kind) *attribute { | ||
return &attribute{accessor: acc[kind], name: name} | ||
} | ||
vs := &Versions{ | ||
m: map[string]Version{}, | ||
prefix: prefix, | ||
all: []Version{ | ||
newMatchExactVersionVersion(prefix, attributeNameMatchMapper, event.EventContextV1{}.AsV1(), | ||
func(c event.EventContextConverter) event.EventContext { return c.AsV1() }, | ||
attr("id", ID), | ||
attr("source", Source), | ||
attr("specversion", SpecVersion), | ||
attr("type", Type), | ||
attr("datacontenttype", DataContentType), | ||
attr("dataschema", DataSchema), | ||
attr("subject", Subject), | ||
attr("time", Time), | ||
), | ||
newMatchExactVersionVersion(prefix, attributeNameMatchMapper, event.EventContextV03{}.AsV03(), | ||
func(c event.EventContextConverter) event.EventContext { return c.AsV03() }, | ||
attr("specversion", SpecVersion), | ||
attr("type", Type), | ||
attr("source", Source), | ||
attr("schemaurl", DataSchema), | ||
attr("subject", Subject), | ||
attr("id", ID), | ||
attr("time", Time), | ||
attr("datacontenttype", DataContentType), | ||
), | ||
}, | ||
} | ||
for _, v := range vs.all { | ||
vs.m[v.String()] = v | ||
} | ||
return vs | ||
} |
This file contains 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,36 @@ | ||
package spec_test | ||
|
||
import ( | ||
"net/textproto" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cloudevents/sdk-go/v2/binding/spec" | ||
"github.com/cloudevents/sdk-go/v2/binding/test" | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
) | ||
|
||
func TestMatchExactVersion(t *testing.T) { | ||
test.EachEvent(t, test.AllVersions([]event.Event{test.FullEvent()}), func(t *testing.T, e event.Event) { | ||
e = e.Clone() | ||
s := spec.WithPrefixMatchExact( | ||
func(s string) string { | ||
if s == "datacontenttype" { | ||
return "Content-Type" | ||
} else { | ||
return textproto.CanonicalMIMEHeaderKey("Ce-" + s) | ||
} | ||
}, | ||
"Ce-", | ||
) | ||
sv := s.Version(e.SpecVersion()) | ||
require.NotNil(t, sv) | ||
|
||
require.Equal(t, e.ID(), sv.Attribute("Ce-Id").Get(e.Context)) | ||
require.Equal(t, "id", sv.Attribute("Ce-Id").Name()) | ||
|
||
require.Equal(t, e.DataContentType(), sv.Attribute("Content-Type").Get(e.Context)) | ||
require.Equal(t, "datacontenttype", sv.Attribute("Content-Type").Name()) | ||
}) | ||
} |
This file contains 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,22 @@ | ||
package http | ||
|
||
import ( | ||
"net/textproto" | ||
|
||
"github.com/cloudevents/sdk-go/v2/binding/spec" | ||
) | ||
|
||
var attributeHeadersMapping map[string]string | ||
|
||
func init() { | ||
attributeHeadersMapping = make(map[string]string) | ||
for _, v := range specs.Versions() { | ||
for _, a := range v.Attributes() { | ||
if a.Kind() == spec.DataContentType { | ||
attributeHeadersMapping[a.Name()] = ContentType | ||
} else { | ||
attributeHeadersMapping[a.Name()] = textproto.CanonicalMIMEHeaderKey(prefix + a.Name()) | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only true for binary mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that,
Version
and related interfaces are used only for binary messages