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

feat: prevent proxy context cancelled #144

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ services:
spicedb:
image: quay.io/authzed/spicedb:v1.0.0
ports:
- "8081:8080"
- "50052:50051"
- "50054:50053"
- "8080:8080"
- "50051:50051"
- "50053:50053"
command:
spicedb serve --grpc-preshared-key "shield" --grpc-no-tls --datastore-engine postgres
--datastore-conn-uri postgres://spicedb:@pg2:5432/spicedb?sslmode=disable
Expand Down
27 changes: 27 additions & 0 deletions internal/proxy/roundtripper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"context"
"crypto/tls"
"net"
"net/http"
Expand Down Expand Up @@ -28,6 +29,30 @@ type h2cTransportWrapper struct {
hook hook.Service
}

type Context struct {
ctx context.Context
}

func (c Context) Deadline() (time.Time, bool) {
return time.Time{}, false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing

t, b := c.ctx.Deadline()
return t, b

was throwing stack overflow

}

func (c Context) Done() <-chan struct{} {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also log when done != nil

return nil
}

func (c Context) Err() error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also only prevent "Context Cancelled" errors, but let others to return

return nil
}

func (c Context) Value(key interface{}) interface{} {
return c.ctx.Value(key)
}

func WithoutCancel(ctx context.Context) context.Context {
return Context{ctx: ctx}
}

krtkvrm marked this conversation as resolved.
Show resolved Hide resolved
func (t *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, error) {
// we need to apply errors if it failed in Director
if err, ok := req.Context().Value(ctxRequestErrorKey).(error); ok {
Expand All @@ -42,6 +67,8 @@ func (t *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, erro
transport = t.grpcTransport
}

req = req.WithContext(WithoutCancel(req.Context()))

res, err := transport.RoundTrip(req)
if err != nil {
return res, err
Expand Down