|
1 | 1 | package network
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "net"
|
5 | 6 | "net/http"
|
6 | 7 | "path/filepath"
|
7 | 8 | "time"
|
8 | 9 | )
|
9 | 10 |
|
10 |
| -// GetClient returns a new HTTP client that uses a DevPod network socket for communication. |
11 |
| -func GetClient() *http.Client { |
12 |
| - // Set up HTTP transport that uses our network socket. |
| 11 | +// Dial returns a net.Conn to the network proxy socket. |
| 12 | +func Dial() (net.Conn, error) { |
13 | 13 | socketPath := filepath.Join(RootDir, NetworkProxySocket)
|
14 |
| - transport := &http.Transport{ |
| 14 | + return net.Dial("unix", socketPath) |
| 15 | +} |
| 16 | + |
| 17 | +// GetCOntextDialer returns ContextDialer interface function that uses our network socket. |
| 18 | +func GetContextDialer() func(ctx context.Context, addr string) (net.Conn, error) { |
| 19 | + return func(ctx context.Context, addr string) (net.Conn, error) { |
| 20 | + return Dial() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// GetHTTPTransport returns http.Transport that uses our network socket for HTTP requests. |
| 25 | +func GetHTTPTransport() *http.Transport { |
| 26 | + // Set up HTTP transport that uses our network socket. |
| 27 | + return &http.Transport{ |
15 | 28 | Dial: func(network, addr string) (net.Conn, error) {
|
16 |
| - return net.Dial("unix", socketPath) |
| 29 | + return Dial() |
17 | 30 | },
|
18 | 31 | }
|
| 32 | +} |
19 | 33 |
|
20 |
| - client := &http.Client{ |
21 |
| - Transport: transport, |
22 |
| - Timeout: 30 * time.Second, // TODO: extract this to config |
| 34 | +// GetClient returns a new HTTP client that uses our network socket for transport. |
| 35 | +func GetHTTPClient() *http.Client { |
| 36 | + return &http.Client{ |
| 37 | + Transport: GetHTTPTransport(), |
| 38 | + Timeout: 30 * time.Second, |
23 | 39 | }
|
24 |
| - |
25 |
| - return client |
26 | 40 | }
|
0 commit comments