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,14 +1,12 @@
package ivpn
import (
"context"
"net"
"time"
"github.com/qdm12/gluetun/internal/updater/resolver"
)
func getResolveSettings(minServers int) (settings resolver.ParallelSettings) {
func newParallelResolver() (parallelResolver resolver.Parallel) {
const (
maxFailRatio = 0.1
maxDuration = 20 * time.Second
@@ -16,9 +14,8 @@ func getResolveSettings(minServers int) (settings resolver.ParallelSettings) {
maxNoNew = 2
maxFails = 2
)
return resolver.ParallelSettings{
settings := resolver.ParallelSettings{
MaxFailRatio: maxFailRatio,
MinFound: minServers,
Repeat: resolver.RepeatSettings{
MaxDuration: maxDuration,
BetweenDuration: betweenDuration,
@@ -27,11 +24,5 @@ func getResolveSettings(minServers int) (settings resolver.ParallelSettings) {
SortIPs: true,
},
}
}
func resolveHosts(ctx context.Context, presolver resolver.Parallel,
hosts []string, minServers int) (hostToIPs map[string][]net.IP,
warnings []string, err error) {
settings := getResolveSettings(minServers)
return presolver.Resolve(ctx, hosts, settings)
return resolver.NewParallelResolver(settings)
}

View File

@@ -1,57 +0,0 @@
package ivpn
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

@@ -38,7 +38,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
common.ErrNotEnoughServers, len(hosts), minServers)
}
hostToIPs, warnings, err := resolveHosts(ctx, u.presolver, hosts, minServers)
hostToIPs, warnings, err := u.presolver.Resolve(ctx, hosts, minServers)
for _, warning := range warnings {
u.warner.Warn(warning)
}

View File

@@ -12,7 +12,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/updater/resolver"
"github.com/qdm12/gluetun/internal/updater/resolver/mock_resolver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -35,7 +34,6 @@ func Test_Updater_GetServers(t *testing.T) {
// Resolution
expectResolve bool
hostsToResolve []string
resolveSettings resolver.ParallelSettings
hostToIPs map[string][]net.IP
resolveWarnings []string
resolveErr error
@@ -61,7 +59,6 @@ func Test_Updater_GetServers(t *testing.T) {
responseStatus: http.StatusOK,
expectResolve: true,
hostsToResolve: []string{"hosta"},
resolveSettings: getResolveSettings(0),
resolveWarnings: []string{"resolve warning"},
resolveErr: errors.New("dummy"),
err: errors.New("dummy"),
@@ -87,10 +84,9 @@ func Test_Updater_GetServers(t *testing.T) {
{"country":"Country2","city":"City B","hostnames":{"openvpn":"hostb"},"wg_public_key":"xyz"},
{"country":"Country3","city":"City C","hostnames":{"wireguard":"hostc"},"wg_public_key":"xyz"}
]}`,
responseStatus: http.StatusOK,
expectResolve: true,
hostsToResolve: []string{"hosta", "hostb", "hostc"},
resolveSettings: getResolveSettings(1),
responseStatus: http.StatusOK,
expectResolve: true,
hostsToResolve: []string{"hosta", "hostb", "hostc"},
hostToIPs: map[string][]net.IP{
"hosta": {{1, 1, 1, 1}, {2, 2, 2, 2}},
"hostb": {{3, 3, 3, 3}, {4, 4, 4, 4}},
@@ -134,13 +130,17 @@ func Test_Updater_GetServers(t *testing.T) {
presolver := mock_resolver.NewMockParallel(ctrl)
if testCase.expectResolve {
presolver.EXPECT().Resolve(ctx, testCase.hostsToResolve, testCase.resolveSettings).
presolver.EXPECT().Resolve(ctx, testCase.hostsToResolve, testCase.minServers).
Return(testCase.hostToIPs, testCase.resolveWarnings, testCase.resolveErr)
}
warner := testCase.warnerBuilder(ctrl)
updater := New(client, presolver, warner)
updater := &Updater{
client: client,
presolver: presolver,
warner: warner,
}
servers, err := updater.FetchServers(ctx, testCase.minServers)

View File

@@ -16,11 +16,10 @@ type Warner interface {
Warn(s string)
}
func New(client *http.Client, presolver resolver.Parallel,
warner Warner) *Updater {
func New(client *http.Client, warner Warner) *Updater {
return &Updater{
client: client,
presolver: presolver,
presolver: newParallelResolver(),
warner: warner,
}
}