forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
55 lines (45 loc) · 1.68 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gitserver
import (
"net/http"
"net/http/httputil"
"github.com/neelance/parallel"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
// DefaultReverseProxy is the default ReverseProxy. It uses the same transport and HTTP
// limiter as the default client.
var DefaultReverseProxy = NewReverseProxy(defaultTransport, DefaultClient.HTTPLimiter)
// NewReverseProxy returns a new gitserver.ReverseProxy instantiated with the given
// transport and HTTP limiter.
func NewReverseProxy(transport http.RoundTripper, httpLimiter *parallel.Run) *ReverseProxy {
return &ReverseProxy{
Transport: transport,
HTTPLimiter: httpLimiter,
}
}
// ReverseProxy is a gitserver reverse proxy.
type ReverseProxy struct {
Transport http.RoundTripper
// Limits concurrency of outstanding HTTP posts
HTTPLimiter *parallel.Run
}
// ServeHTTP creates a one-shot proxy with the given director and proxies the given request
// to gitserver. The director must rewrite the request to the correct gitserver address, which
// should be obtained via a gitserver client's AddrForRepo method.
func (p *ReverseProxy) ServeHTTP(repo api.RepoName, method, op string, director func(req *http.Request), res http.ResponseWriter, req *http.Request) {
span, _ := ot.StartSpanFromContext(req.Context(), "ReverseProxy.ServeHTTP")
defer func() {
span.LogKV("repo", string(repo), "method", method, "op", op)
span.Finish()
}()
if p.HTTPLimiter != nil {
p.HTTPLimiter.Acquire()
defer p.HTTPLimiter.Release()
span.LogKV("event", "Acquired HTTP limiter")
}
proxy := &httputil.ReverseProxy{
Director: director,
Transport: p.Transport,
}
proxy.ServeHTTP(res, req)
}