chore(updater): create resolver in provider updater

- Pass min servers to resolve call
- Set settings when constructing resolver
- Construct resolver in each provider updater
- No more common resolver for all providers
This commit is contained in:
Quentin McGaw
2022-06-09 02:54:39 +00:00
parent e37f557cd5
commit 415cb7a945
53 changed files with 155 additions and 483 deletions

View File

@@ -1,16 +1,12 @@
package wevpn
import (
"context"
"net"
"time"
"github.com/qdm12/gluetun/internal/updater/resolver"
)
func resolveHosts(ctx context.Context, presolver resolver.Parallel,
hosts []string, minServers int) (hostToIPs map[string][]net.IP,
warnings []string, err error) {
func newParallelResolver() (parallelResolver resolver.Parallel) {
const (
maxFailRatio = 0.1
maxDuration = 20 * time.Second
@@ -20,7 +16,6 @@ func resolveHosts(ctx context.Context, presolver resolver.Parallel,
)
settings := resolver.ParallelSettings{
MaxFailRatio: maxFailRatio,
MinFound: minServers,
Repeat: resolver.RepeatSettings{
MaxDuration: maxDuration,
BetweenDuration: betweenDuration,
@@ -29,5 +24,5 @@ func resolveHosts(ctx context.Context, presolver resolver.Parallel,
SortIPs: true,
},
}
return presolver.Resolve(ctx, hosts, settings)
return resolver.NewParallelResolver(settings)
}

View File

@@ -1,57 +0,0 @@
package wevpn
import (
"context"
"errors"
"net"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/qdm12/gluetun/internal/updater/resolver"
"github.com/qdm12/gluetun/internal/updater/resolver/mock_resolver"
"github.com/stretchr/testify/assert"
)
func Test_resolveHosts(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
ctx := context.Background()
presolver := mock_resolver.NewMockParallel(ctrl)
hosts := []string{"host1", "host2"}
const minServers = 10
expectedHostToIPs := map[string][]net.IP{
"host1": {{1, 2, 3, 4}},
"host2": {{2, 3, 4, 5}},
}
expectedWarnings := []string{"warning1", "warning2"}
expectedErr := errors.New("dummy")
const (
maxFailRatio = 0.1
maxDuration = 20 * time.Second
betweenDuration = time.Second
maxNoNew = 2
maxFails = 2
)
expectedSettings := resolver.ParallelSettings{
MaxFailRatio: maxFailRatio,
MinFound: minServers,
Repeat: resolver.RepeatSettings{
MaxDuration: maxDuration,
BetweenDuration: betweenDuration,
MaxNoNew: maxNoNew,
MaxFails: maxFails,
SortIPs: true,
},
}
presolver.EXPECT().Resolve(ctx, hosts, expectedSettings).
Return(expectedHostToIPs, expectedWarnings, expectedErr)
hostToIPs, warnings, err := resolveHosts(ctx, presolver, hosts, minServers)
assert.Equal(t, expectedHostToIPs, hostToIPs)
assert.Equal(t, expectedWarnings, warnings)
assert.Equal(t, expectedErr, err)
}

View File

@@ -31,7 +31,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
hostnameToCity[hostname] = city
}
hostnameToIPs, warnings, err := resolveHosts(ctx, u.presolver, hostnames, minServers)
hostnameToIPs, warnings, err := u.presolver.Resolve(ctx, hostnames, minServers)
for _, warning := range warnings {
u.warner.Warn(warning)
}

View File

@@ -13,9 +13,9 @@ type Warner interface {
Warn(s string)
}
func New(presolver resolver.Parallel, warner Warner) *Updater {
func New(warner Warner) *Updater {
return &Updater{
presolver: presolver,
presolver: newParallelResolver(),
warner: warner,
}
}