Welcome to mirror list, hosted at ThFree Co, Russian Federation.

transport.go « serverless « serving « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7fabb13bb5cfb357f1849fdf93d6388ceb8a823 (plain)
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
package serverless

import (
	"context"
	"net"
	"net/http"
	"time"

	"gitlab.com/gitlab-org/gitlab-pages/metrics"
)

// Transport is a struct that handle the proxy connection round trip to Knative
// cluster
type Transport struct {
	cluster   Cluster
	transport *http.Transport
}

// NewTransport fabricates as new transport type
func NewTransport(cluster Cluster) *Transport {
	dialer := net.Dialer{
		Timeout:   4 * time.Minute,
		KeepAlive: 6 * time.Minute,
	}

	dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
		address = cluster.Host()

		return dialer.DialContext(ctx, network, address)
	}

	return &Transport{
		cluster: cluster,
		transport: &http.Transport{
			DialContext:         dialContext,
			TLSHandshakeTimeout: 5 * time.Second,
			TLSClientConfig:     cluster.TLSConfig(),
		},
	}
}

// RoundTrip performs a connection to a Knative cluster and returns a response
func (t *Transport) RoundTrip(request *http.Request) (*http.Response, error) {
	start := time.Now()

	response, err := t.transport.RoundTrip(request)

	metrics.ServerlessLatency.Observe(time.Since(start).Seconds())

	return response, err
}