Fix updater guard pattern (#255)

This commit is contained in:
Quentin McGaw
2020-10-01 17:56:14 -04:00
committed by GitHub
parent ecf76896a2
commit fbecbc1c82
2 changed files with 12 additions and 7 deletions

View File

@@ -30,6 +30,7 @@ func findCyberghostServers(ctx context.Context, lookupIP lookupIPFunc) (servers
results := make(chan models.CyberghostServer) results := make(chan models.CyberghostServer)
const maxGoroutines = 10 const maxGoroutines = 10
guard := make(chan struct{}, maxGoroutines) guard := make(chan struct{}, maxGoroutines)
defer close(guard)
for groupID, groupName := range groups { for groupID, groupName := range groups {
for countryCode, region := range possibleCountryCodes { for countryCode, region := range possibleCountryCodes {
if err := ctx.Err(); err != nil { if err := ctx.Err(); err != nil {
@@ -38,8 +39,7 @@ func findCyberghostServers(ctx context.Context, lookupIP lookupIPFunc) (servers
const domain = "cg-dialup.net" const domain = "cg-dialup.net"
host := fmt.Sprintf("%s-%s.%s", groupID, countryCode, domain) host := fmt.Sprintf("%s-%s.%s", groupID, countryCode, domain)
guard <- struct{}{} guard <- struct{}{}
go tryCyberghostHostname(ctx, lookupIP, host, groupName, region, results) go tryCyberghostHostname(ctx, lookupIP, host, groupName, region, results, guard)
<-guard
} }
} }
for i := 0; i < len(groups)*len(possibleCountryCodes); i++ { 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, func tryCyberghostHostname(ctx context.Context, lookupIP lookupIPFunc,
host, groupName, region string, 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) IPs, err := resolveRepeat(ctx, lookupIP, host, 2)
if err != nil || len(IPs) == 0 { if err != nil || len(IPs) == 0 {
results <- models.CyberghostServer{} results <- models.CyberghostServer{}

View File

@@ -77,8 +77,7 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
region := strings.TrimSuffix(fileName, ".ovpn") region := strings.TrimSuffix(fileName, ".ovpn")
guard <- struct{}{} guard <- struct{}{}
wg.Add(1) wg.Add(1)
go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh) go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh, guard)
<-guard
} }
for range contents { for range contents {
select { select {
@@ -101,8 +100,11 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
func resolvePIAHostname(ctx context.Context, wg *sync.WaitGroup, func resolvePIAHostname(ctx context.Context, wg *sync.WaitGroup,
region string, hosts []string, lookupIP lookupIPFunc, region string, hosts []string, lookupIP lookupIPFunc,
errors chan<- error, serversCh chan<- models.PIAServer) { errors chan<- error, serversCh chan<- models.PIAServer, guard chan<- struct{}) {
defer wg.Done() defer func() {
guard <- struct{}{}
wg.Done()
}()
var IPs []net.IP //nolint:prealloc var IPs []net.IP //nolint:prealloc
// usually one single host in this case // usually one single host in this case
// so no need to run in goroutines the for loop below // so no need to run in goroutines the for loop below