GitLabgitlab-org

cli

main
ply6 git calls42 ms99 KiB4 cachedprotocol v2
247 lines7.1 KiBRaw
1package api
3import (
4 "context"
5 "crypto/tls"
6 "crypto/x509"
7 "errors"
8 "fmt"
9 "net"
10 "net/http"
11 "net/url"
12 "os"
13 "slices"
14 "strings"
15 "sync"
16 "time"
19// transportKey holds every input newTransport reads, so two clients with equal
20// keys can share one connection pool.
21type transportKey struct {
22 // proxyURL is empty when the proxy comes from the environment.
23 proxyURL string
24 caFile string
25 clientCertFile string
26 clientKeyFile string
27 clientCertPassword string
28 insecure bool
29 gitlabCom bool
32// TransportCache lets API clients with identical transport settings share a
33// connection pool, so requests to the same host reuse a warm connection
34// instead of paying for a new TLS handshake.
35// Safe for concurrent use.
36type TransportCache struct {
37 mu sync.Mutex
38 byKey map[transportKey]*http.Transport
41func NewTransportCache() *TransportCache {
42 return &TransportCache{
43 byKey: make(map[transportKey]*http.Transport),
44 }
47func (tc *TransportCache) transport(key transportKey) (*http.Transport, error) {
48 tc.mu.Lock()
49 defer tc.mu.Unlock()
51 if t, ok := tc.byKey[key]; ok {
52 return t, nil
53 }
55 t, err := newTransport(key)
56 if err != nil {
57 return nil, err
58 }
60 tc.byKey[key] = t
61 return t, nil
64func newTransport(key transportKey) (*http.Transport, error) {
65 tlsConfig := &tls.Config{
66 MinVersion: tls.VersionTLS12,
67 InsecureSkipVerify: key.insecure,
68 }
70 if key.gitlabCom {
71 tlsConfig.CipherSuites = secureCipherSuites
72 }
74 if key.caFile != "" {
75 caCert, err := os.ReadFile(key.caFile)
76 if err != nil {
77 return nil, fmt.Errorf("error reading cert file: %w", err)
78 }
79 // use system cert pool as a baseline
80 caCertPool, err := x509.SystemCertPool()
81 if err != nil {
82 return nil, err
83 }
84 caCertPool.AppendCertsFromPEM(caCert)
85 tlsConfig.RootCAs = caCertPool
86 }
88 if key.clientCertFile != "" {
89 clientCerts, err := loadClientCertificates(key.clientCertFile, key.clientKeyFile, key.clientCertPassword)
90 if err != nil {
91 return nil, err
92 }
93 tlsConfig.Certificates = clientCerts
94 }
96 // Set appropriate timeouts based on whether custom CA is used
97 dialTimeout := 5 * time.Second
98 keepAlive := 5 * time.Second
99 idleTimeout := 30 * time.Second
100 if key.caFile != "" {
101 dialTimeout = 30 * time.Second
102 keepAlive = 30 * time.Second
103 idleTimeout = 90 * time.Second
104 }
106 proxy := http.ProxyFromEnvironment
107 if key.proxyURL != "" {
108 u, err := url.Parse(key.proxyURL)
109 if err != nil {
110 return nil, err
111 }
112 proxy = http.ProxyURL(u)
113 }
115 dialer := &net.Dialer{
116 Timeout: dialTimeout,
117 KeepAlive: keepAlive,
118 }
119 tlsHandshakeTimeout := 10 * time.Second
120 tlsDialer := &proxyAwareTLSDialer{
121 dialer: dialer,
122 config: tlsConfig,
123 handshakeTimeout: tlsHandshakeTimeout,
124 }
126 return &http.Transport{
127 Proxy: tlsDialer.recordHTTPSProxies(proxy),
128 DialContext: dialer.DialContext,
129 DialTLSContext: tlsDialer.DialTLSContext,
130 ForceAttemptHTTP2: true,
131 MaxIdleConns: 100,
132 MaxIdleConnsPerHost: 25,
133 IdleConnTimeout: idleTimeout,
134 TLSHandshakeTimeout: tlsHandshakeTimeout,
135 ExpectContinueTimeout: 1 * time.Second,
136 TLSClientConfig: tlsConfig,
137 }, nil
140// proxyAwareTLSDialer opens the TLS connections http.Transport makes itself:
141// direct HTTPS connections and connections to HTTPS proxies. The Transport
142// offers the target's ALPN protocols to the proxy too, so a proxy that picks
143// h2 receives an HTTP/1.1 CONNECT it cannot parse. The handshake with a proxy
144// therefore offers only HTTP/1.1; the tunnelled handshake with GitLab still
145// offers HTTP/2.
146type proxyAwareTLSDialer struct {
147 dialer *net.Dialer
148 config *tls.Config
149 handshakeTimeout time.Duration
150 // httpsProxyAddrs holds the host:port of every https:// proxy the Proxy
151 // func has returned, in the form the Transport passes to DialTLSContext.
152 httpsProxyAddrs sync.Map
155func (d *proxyAwareTLSDialer) recordHTTPSProxies(proxy func(*http.Request) (*url.URL, error)) func(*http.Request) (*url.URL, error) {
156 return func(req *http.Request) (*url.URL, error) {
157 u, err := proxy(req)
158 if err == nil && u != nil && u.Scheme == "https" {
159 port := u.Port()
160 if port == "" {
161 port = "443"
162 }
163 d.httpsProxyAddrs.LoadOrStore(net.JoinHostPort(u.Hostname(), port), struct{}{})
164 }
165 return u, err
166 }
169func (d *proxyAwareTLSDialer) DialTLSContext(ctx context.Context, network, addr string) (net.Conn, error) {
170 host, _, err := net.SplitHostPort(addr)
171 if err != nil {
172 return nil, err
173 }
175 // The Transport adds its ALPN protocols to the shared config before its
176 // first dial, so the clone already offers h2 when HTTP/2 is enabled.
177 cfg := d.config.Clone()
178 if cfg.ServerName == "" {
179 cfg.ServerName = host
180 }
181 if _, ok := d.httpsProxyAddrs.Load(addr); ok {
182 cfg.NextProtos = []string{"http/1.1"}
183 }
184 // http.Transport clears NextProtos for these requests when it dials
185 // itself, but a custom DialTLSContext bypasses that.
186 if ctx.Value(http1OnlyKey{}) != nil {
187 cfg.NextProtos = nil
188 }
190 conn, err := d.dialer.DialContext(ctx, network, addr)
191 if err != nil {
192 return nil, err
193 }
195 handshakeCtx, cancel := context.WithTimeout(ctx, d.handshakeTimeout)
196 defer cancel()
197 tlsConn := tls.Client(conn, cfg)
198 if err := tlsConn.HandshakeContext(handshakeCtx); err != nil {
199 if ctx.Err() == nil && handshakeCtx.Err() != nil {
200 err = tlsHandshakeTimeoutError{}
201 }
202 closeErr := conn.Close()
203 // HandshakeContext has already closed conn when its context ended.
204 if errors.Is(closeErr, net.ErrClosed) {
205 closeErr = nil
206 }
207 return nil, errors.Join(err, closeErr)
208 }
209 return tlsConn, nil
212// tlsHandshakeTimeoutError is what http.Transport returns when its own
213// TLSHandshakeTimeout expires. Unlike the handshake context's error, it does not
214// match context.DeadlineExceeded, which callers read as their own deadline.
215type tlsHandshakeTimeoutError struct{}
217func (tlsHandshakeTimeoutError) Error() string { return "net/http: TLS handshake timeout" }
218func (tlsHandshakeTimeoutError) Timeout() bool { return true }
219func (tlsHandshakeTimeoutError) Temporary() bool { return true }
221// http1OnlyKey marks a request http.Transport keeps off HTTP/2: a websocket
222// upgrade. The Transport dials with the request's context values, and pools
223// these connections separately, so no other request reuses one.
224type http1OnlyKey struct{}
226// requiresHTTP1 is http.Request.requiresHTTP1, which is unexported.
227func requiresHTTP1(req *http.Request) bool {
228 connection := strings.FieldsFunc(req.Header.Get("Connection"), func(r rune) bool {
229 return r == ',' || r == ' ' || r == '\t'
230 })
231 return slices.ContainsFunc(connection, func(token string) bool { return strings.EqualFold(token, "upgrade") }) &&
232 strings.EqualFold(req.Header.Get("Upgrade"), "websocket")
235// pooledTransport hides CloseIdleConnections, which go-retryablehttp calls
236// after every failed request, so that one client's failure does not drop the
237// connections other clients share.
238type pooledTransport struct {
239 rt http.RoundTripper
242func (t *pooledTransport) RoundTrip(req *http.Request) (*http.Response, error) {
243 if requiresHTTP1(req) {
244 req = req.WithContext(context.WithValue(req.Context(), http1OnlyKey{}, true))
245 }
246 return t.rt.RoundTrip(req)
Diagnostics6 git calls · 42 ms
CallStatusSizeTime042 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB8 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=8
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.1 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=1165
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1165
git.pkt.payload.max=1138
git.sideband.band1.bytes=1138
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1138
git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141c
Open pack map
fetch2002.7 KiB · hit29 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=29
git.response.bytes=2749
git.response.cache=HIT
git.response.duration_ms=29
git.fetch.wants=b4abfa987e82d5edf67d2698c4290a8c19cd8e5f
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2749
git.pkt.payload.max=2722
git.sideband.band1.bytes=2722
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e16d1c7a744b2a
Request
472fb48c…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format