Fix updater guard pattern (#255)
This commit is contained in:
@@ -30,6 +30,7 @@ func findCyberghostServers(ctx context.Context, lookupIP lookupIPFunc) (servers
|
||||
results := make(chan models.CyberghostServer)
|
||||
const maxGoroutines = 10
|
||||
guard := make(chan struct{}, maxGoroutines)
|
||||
defer close(guard)
|
||||
for groupID, groupName := range groups {
|
||||
for countryCode, region := range possibleCountryCodes {
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -38,8 +39,7 @@ func findCyberghostServers(ctx context.Context, lookupIP lookupIPFunc) (servers
|
||||
const domain = "cg-dialup.net"
|
||||
host := fmt.Sprintf("%s-%s.%s", groupID, countryCode, domain)
|
||||
guard <- struct{}{}
|
||||
go tryCyberghostHostname(ctx, lookupIP, host, groupName, region, results)
|
||||
<-guard
|
||||
go tryCyberghostHostname(ctx, lookupIP, host, groupName, region, results, guard)
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(groups)*len(possibleCountryCodes); i++ {
|
||||
@@ -60,7 +60,10 @@ func findCyberghostServers(ctx context.Context, lookupIP lookupIPFunc) (servers
|
||||
|
||||
func tryCyberghostHostname(ctx context.Context, lookupIP lookupIPFunc,
|
||||
host, groupName, region string,
|
||||
results chan<- models.CyberghostServer) {
|
||||
results chan<- models.CyberghostServer, guard chan<- struct{}) {
|
||||
defer func() {
|
||||
guard <- struct{}{}
|
||||
}()
|
||||
IPs, err := resolveRepeat(ctx, lookupIP, host, 2)
|
||||
if err != nil || len(IPs) == 0 {
|
||||
results <- models.CyberghostServer{}
|
||||
|
||||
@@ -77,8 +77,7 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
|
||||
region := strings.TrimSuffix(fileName, ".ovpn")
|
||||
guard <- struct{}{}
|
||||
wg.Add(1)
|
||||
go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh)
|
||||
<-guard
|
||||
go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh, guard)
|
||||
}
|
||||
for range contents {
|
||||
select {
|
||||
@@ -101,8 +100,11 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
|
||||
|
||||
func resolvePIAHostname(ctx context.Context, wg *sync.WaitGroup,
|
||||
region string, hosts []string, lookupIP lookupIPFunc,
|
||||
errors chan<- error, serversCh chan<- models.PIAServer) {
|
||||
defer wg.Done()
|
||||
errors chan<- error, serversCh chan<- models.PIAServer, guard chan<- struct{}) {
|
||||
defer func() {
|
||||
guard <- struct{}{}
|
||||
wg.Done()
|
||||
}()
|
||||
var IPs []net.IP //nolint:prealloc
|
||||
// usually one single host in this case
|
||||
// so no need to run in goroutines the for loop below
|
||||
|
||||
Reference in New Issue
Block a user