/
floating_proxy.go
53 lines (45 loc) · 1.64 KB
/
floating_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
// Copyright (c) 2022 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tornado
import (
"context"
"net"
)
// NewFloatingProxy creates new instance of FloatingProxy.
func NewFloatingProxy(pool *Pool) *FloatingProxy {
return &FloatingProxy{pool: pool}
}
// A FloatingProxy is an abstraction for making it easy to create connections
// through different proxies but within the same instance of the dial function.
//
// But do not forget that different implementations of clients for network
// interaction can keep the connection open for different requests while using
// one common Proxy chain, for example http.Client with http.DefaultTransport.
type FloatingProxy struct {
pool *Pool
}
// Dial connects to the address on the named network.
//
// Dial uses context.Background internally; to specify the context, use
// DialContext.
//
// See func Dial of the net package of standard library for a
// description of the network and address parameters.
func (p *FloatingProxy) Dial(network, address string) (c net.Conn, err error) {
return p.DialContext(context.Background(), network, address)
}
// DialContext connects to the address on the named network using
// the provided context.
//
// See func Dial of the net package of standard library for a
// description of the network and address parameters.
func (p *FloatingProxy) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if ctx == nil {
panic("tornado: nil context")
}
prx := p.pool.Get()
conn, err := prx.DialContext(ctx, network, address)
p.pool.Put(prx)
return conn, err
}