updater: refactoring and set DNS server correctly
- Fix CLI operation not setting DNS server - Fix periodic operation not setting DNS server - Set DNS address for resolution once at start for both CLI and periodic operation - Inject resolver to each provider instead of creating it within - Use resolver settings on every call to `.Resolve` method, instead of passing it to constructor - Move out minServers check from resolver
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
func newParallelResolver() (parallelResolver *resolver.Parallel) {
|
||||
func parallelResolverSettings(hosts []string) (settings resolver.ParallelSettings) {
|
||||
const (
|
||||
maxFailRatio = 0.1
|
||||
maxDuration = 20 * time.Second
|
||||
@@ -14,7 +14,8 @@ func newParallelResolver() (parallelResolver *resolver.Parallel) {
|
||||
maxNoNew = 2
|
||||
maxFails = 2
|
||||
)
|
||||
settings := resolver.ParallelSettings{
|
||||
return resolver.ParallelSettings{
|
||||
Hosts: hosts,
|
||||
MaxFailRatio: maxFailRatio,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: maxDuration,
|
||||
@@ -24,5 +25,4 @@ func newParallelResolver() (parallelResolver *resolver.Parallel) {
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return resolver.NewParallelResolver(settings)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,8 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
||||
common.ErrNotEnoughServers, len(hosts), minServers)
|
||||
}
|
||||
|
||||
hostToIPs, warnings, err := u.presolver.Resolve(ctx, hosts, minServers)
|
||||
resolveSettings := parallelResolverSettings(hosts)
|
||||
hostToIPs, warnings, err := u.presolver.Resolve(ctx, resolveSettings)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
@@ -46,6 +47,11 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(hostToIPs) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
servers = make([]models.Server, 0, len(hosts))
|
||||
for _, serverData := range data.Servers {
|
||||
vpnType := vpn.OpenVPN
|
||||
|
||||
@@ -8,11 +8,13 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -33,7 +35,7 @@ func Test_Updater_GetServers(t *testing.T) {
|
||||
|
||||
// Resolution
|
||||
expectResolve bool
|
||||
hostsToResolve []string
|
||||
resolveSettings resolver.ParallelSettings
|
||||
hostToIPs map[string][]net.IP
|
||||
resolveWarnings []string
|
||||
resolveErr error
|
||||
@@ -56,9 +58,19 @@ func Test_Updater_GetServers(t *testing.T) {
|
||||
responseBody: `{"servers":[
|
||||
{"hostnames":{"openvpn":"hosta"}}
|
||||
]}`,
|
||||
responseStatus: http.StatusOK,
|
||||
expectResolve: true,
|
||||
hostsToResolve: []string{"hosta"},
|
||||
responseStatus: http.StatusOK,
|
||||
expectResolve: true,
|
||||
resolveSettings: resolver.ParallelSettings{
|
||||
Hosts: []string{"hosta"},
|
||||
MaxFailRatio: 0.1,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: 20 * time.Second,
|
||||
BetweenDuration: time.Second,
|
||||
MaxNoNew: 2,
|
||||
MaxFails: 2,
|
||||
SortIPs: true,
|
||||
},
|
||||
},
|
||||
resolveWarnings: []string{"resolve warning"},
|
||||
resolveErr: errors.New("dummy"),
|
||||
err: errors.New("dummy"),
|
||||
@@ -86,7 +98,17 @@ func Test_Updater_GetServers(t *testing.T) {
|
||||
]}`,
|
||||
responseStatus: http.StatusOK,
|
||||
expectResolve: true,
|
||||
hostsToResolve: []string{"hosta", "hostb", "hostc"},
|
||||
resolveSettings: resolver.ParallelSettings{
|
||||
Hosts: []string{"hosta", "hostb", "hostc"},
|
||||
MaxFailRatio: 0.1,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: 20 * time.Second,
|
||||
BetweenDuration: time.Second,
|
||||
MaxNoNew: 2,
|
||||
MaxFails: 2,
|
||||
SortIPs: true,
|
||||
},
|
||||
},
|
||||
hostToIPs: map[string][]net.IP{
|
||||
"hosta": {{1, 1, 1, 1}, {2, 2, 2, 2}},
|
||||
"hostb": {{3, 3, 3, 3}, {4, 4, 4, 4}},
|
||||
@@ -130,7 +152,7 @@ func Test_Updater_GetServers(t *testing.T) {
|
||||
|
||||
presolver := common.NewMockParallelResolver(ctrl)
|
||||
if testCase.expectResolve {
|
||||
presolver.EXPECT().Resolve(ctx, testCase.hostsToResolve, testCase.minServers).
|
||||
presolver.EXPECT().Resolve(ctx, testCase.resolveSettings).
|
||||
Return(testCase.hostToIPs, testCase.resolveWarnings, testCase.resolveErr)
|
||||
}
|
||||
|
||||
|
||||
@@ -12,10 +12,11 @@ type Updater struct {
|
||||
warner common.Warner
|
||||
}
|
||||
|
||||
func New(client *http.Client, warner common.Warner) *Updater {
|
||||
func New(client *http.Client, warner common.Warner,
|
||||
parallelResolver common.ParallelResolver) *Updater {
|
||||
return &Updater{
|
||||
client: client,
|
||||
presolver: newParallelResolver(),
|
||||
presolver: parallelResolver,
|
||||
warner: warner,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user