Skip to content

Commit

Permalink
feat: prevent proxy context cancelled (#144)
Browse files Browse the repository at this point in the history
* correct the port on docker-compose

* wrap context to prevent cancel

* add ctx to new file

* catch client error before roundtrip
  • Loading branch information
krtkvrm authored Aug 16, 2022
1 parent 0e99d45 commit 6d32ca1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/proxy/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package proxy

import (
"context"
"time"
)

type Context struct {
ctx context.Context
}

func (c Context) Deadline() (time.Time, bool) {
return time.Time{}, false
}

func (c Context) Done() <-chan struct{} {
return nil
}

func (c Context) Err() error {
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}
}
5 changes: 5 additions & 0 deletions internal/proxy/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func (t *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, erro
// we need to apply errors if it failed in Director
if err, ok := req.Context().Value(ctxRequestErrorKey).(error); ok {
return nil, err
} else if req.Context().Err() != nil {
return nil, req.Context().Err()
}

t.log.Debug("proxy request", "host", req.URL.Host, "path", req.URL.Path,
"scheme", req.URL.Scheme, "protocol", req.Proto)

Expand All @@ -42,6 +45,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

0 comments on commit 6d32ca1

Please sign in to comment.