-
Notifications
You must be signed in to change notification settings - Fork 581
feat: add transport extension with interceptor pre/post hooks #292
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
5 commits
Select commit
Hold shift + click to select a range
65075a9
feat: add transport extension with interceptor pre/post hooks
tuxedomm 9f694ea
test: add behavior tests for transport extension interceptor
tuxedomm dce80a6
fix: resolve gofmt and fatcontext lint issues
tuxedomm b0b3a6d
fix: clone request before PreRoundTrip to isolate caller's headers
tuxedomm f6ca1bb
style: fix gofmt formatting in transport.go
tuxedomm 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,28 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package transport | ||
|
|
||
| import "sync" | ||
|
|
||
| var ( | ||
| mu sync.Mutex | ||
| provider Provider | ||
| ) | ||
|
|
||
| // Register registers a transport Provider. | ||
| // Later registrations override earlier ones. | ||
| // Typically called from init() via blank import. | ||
| func Register(p Provider) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| provider = p | ||
| } | ||
|
|
||
| // GetProvider returns the currently registered Provider. | ||
| // Returns nil if no provider has been registered. | ||
| func GetProvider() Provider { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| return provider | ||
| } |
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,77 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package transport | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| type stubInterceptor struct{} | ||
|
|
||
| func (s *stubInterceptor) PreRoundTrip(req *http.Request) func(*http.Response, error) { | ||
| return nil | ||
| } | ||
|
|
||
| type stubProvider struct { | ||
| name string | ||
| } | ||
|
|
||
| func (s *stubProvider) Name() string { return s.name } | ||
| func (s *stubProvider) ResolveInterceptor(context.Context) Interceptor { return &stubInterceptor{} } | ||
|
|
||
| func TestGetProvider_NilByDefault(t *testing.T) { | ||
| mu.Lock() | ||
| provider = nil | ||
| mu.Unlock() | ||
|
|
||
| if got := GetProvider(); got != nil { | ||
| t.Fatalf("expected nil, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestRegisterAndGet(t *testing.T) { | ||
| mu.Lock() | ||
| provider = nil | ||
| mu.Unlock() | ||
|
|
||
| p := &stubProvider{name: "a"} | ||
| Register(p) | ||
|
|
||
| got := GetProvider() | ||
| if got != p { | ||
| t.Fatalf("expected registered provider, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestLastRegistrationWins(t *testing.T) { | ||
| mu.Lock() | ||
| provider = nil | ||
| mu.Unlock() | ||
|
|
||
| a := &stubProvider{name: "a"} | ||
| b := &stubProvider{name: "b"} | ||
| Register(a) | ||
| Register(b) | ||
|
|
||
| got := GetProvider() | ||
| if got != b { | ||
| t.Fatalf("expected provider b, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInterceptor_ReturnsNonNil(t *testing.T) { | ||
| mu.Lock() | ||
| provider = nil | ||
| mu.Unlock() | ||
|
|
||
| p := &stubProvider{name: "test"} | ||
| Register(p) | ||
|
|
||
| ic := GetProvider().ResolveInterceptor(context.Background()) | ||
| if ic == nil { | ||
| t.Fatal("expected non-nil Interceptor") | ||
| } | ||
| } |
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,32 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package transport | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // Provider creates Interceptor instances. | ||
| // Follows the same API style as extension/credential.Provider and extension/fileio.Provider. | ||
| type Provider interface { | ||
| Name() string | ||
| ResolveInterceptor(ctx context.Context) Interceptor | ||
| } | ||
|
|
||
| // Interceptor defines network-layer customization via a pre/post hook pair. | ||
| // The built-in transport chain always executes between PreRoundTrip and the | ||
| // returned post function, and cannot be skipped or overridden by the extension. | ||
| // | ||
| // PreRoundTrip is called before the built-in chain. Use it to add custom | ||
| // headers, rewrite the host, or start trace spans. Built-in decorators run | ||
| // after this and will override any same-named security headers set here. | ||
| // The extension must not replace req.Context() — the middleware restores | ||
| // the original context after PreRoundTrip returns. | ||
| // | ||
| // The returned function (if non-nil) is called after the built-in chain | ||
| // completes. Use it for logging, ending trace spans, or recording metrics. | ||
| type Interceptor interface { | ||
| PreRoundTrip(req *http.Request) func(resp *http.Response, err error) | ||
| } |
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
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.