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

gateway: add WithContextHint to handler and implement it on BlocksBackend #550

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: 6 additions & 0 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,12 @@
return has
}

var _ WithContextHint = (*BlocksBackend)(nil)

func (bb *BlocksBackend) WrapContextForRequest(ctx context.Context) context.Context {
return blockservice.ContextWithSession(ctx, bb.blockService)

Check warning on line 690 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L689-L690

Added lines #L689 - L690 were not covered by tests
}

func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, error) {
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ type IPFSBackend interface {
GetDNSLinkRecord(context.Context, string) (path.Path, error)
}

// WithContextHint allows an [IPFSBackend] to inject custom [context.Context] configurations.
// This should be considered optional, consumers might only make a best effort attempt at calling WrapContextForRequest on requests.
type WithContextHint interface {
// WrapContextForRequest allows the backend to add request scopped modifications to the context, like debug values or value caches.
// There are no promises on actual usage in consumers.
WrapContextForRequest(context.Context) context.Context
}

// cleanHeaderSet is an helper function that cleans a set of headers by
// (1) canonicalizing, (2) de-duplicating and (3) sorting.
func cleanHeaderSet(headers []string) []string {
Expand Down
5 changes: 5 additions & 0 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
// the hour is a hard fallback, we don't expect it to happen, but just in case
ctx, cancel := context.WithTimeout(r.Context(), time.Hour)
defer cancel()

if withCtxWrap, ok := i.backend.(WithContextHint); ok {
ctx = withCtxWrap.WrapContextForRequest(ctx)
}

Check warning on line 158 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L157-L158

Added lines #L157 - L158 were not covered by tests

r = r.WithContext(ctx)

switch r.Method {
Expand Down
Loading