-
Notifications
You must be signed in to change notification settings - Fork 13
/
pool.go
51 lines (43 loc) · 1.34 KB
/
pool.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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package pac
import (
"net"
"net/url"
"sync"
)
type ProxyResolverPool struct {
pool sync.Pool
}
func NewProxyResolverPool(cfg *ProxyResolverConfig, r *net.Resolver, opts ...Option) (*ProxyResolverPool, error) {
if _, err := NewProxyResolver(cfg, r, opts...); err != nil {
return nil, err
}
f := func() any {
p, err := NewProxyResolver(cfg, r, opts...)
if err != nil {
panic(err)
}
return p
}
return &ProxyResolverPool{
pool: sync.Pool{
New: f,
},
}, nil
}
// FindProxyForURL calls FindProxyForURL or FindProxyForURLEx function in the PAC script with the alternate hostname.
// The hostname is optional, if empty it will be extracted from URL.
// This is to handle cases when the hostname is not a valid hostname, but a URL.
func (pool *ProxyResolverPool) FindProxyForURL(u *url.URL, hostname string) (p string, err error) {
pr := pool.get()
p, err = pr.FindProxyForURL(u, hostname)
pool.pool.Put(pr)
return
}
func (pool *ProxyResolverPool) get() *ProxyResolver {
return pool.pool.Get().(*ProxyResolver) //nolint:forcetypeassert // we know it's a ProxyResolver
}