Maintenance: using channels instead of wrap functions

This commit is contained in:
Quentin McGaw
2021-01-04 01:49:05 +00:00
parent 657937d272
commit 3b91e351b7
2 changed files with 11 additions and 12 deletions

View File

@@ -232,9 +232,8 @@ func _main(background context.Context, buildInfo models.BuildInformation,
} }
} }
tunnelReadyCh, dnsReadyCh := make(chan struct{}), make(chan struct{}) tunnelReadyCh := make(chan struct{})
signalTunnelReady := func() { tunnelReadyCh <- struct{}{} } dnsReadyCh := make(chan struct{})
signalDNSReady := func() { dnsReadyCh <- struct{}{} }
defer close(tunnelReadyCh) defer close(tunnelReadyCh)
defer close(dnsReadyCh) defer close(dnsReadyCh)
@@ -262,7 +261,7 @@ func _main(background context.Context, buildInfo models.BuildInformation,
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(1) wg.Add(1)
go collectStreamLines(ctx, wg, streamMerger, logger, signalTunnelReady) go collectStreamLines(ctx, wg, streamMerger, logger, tunnelReadyCh)
openvpnLooper := openvpn.NewLooper(allSettings.OpenVPN, nonRootUsername, puid, pgid, allServers, openvpnLooper := openvpn.NewLooper(allSettings.OpenVPN, nonRootUsername, puid, pgid, allServers,
ovpnConf, firewallConf, routingConf, logger, httpClient, os.OpenFile, streamMerger, cancel) ovpnConf, firewallConf, routingConf, logger, httpClient, os.OpenFile, streamMerger, cancel)
@@ -280,7 +279,7 @@ func _main(background context.Context, buildInfo models.BuildInformation,
logger, streamMerger, nonRootUsername, puid, pgid) logger, streamMerger, nonRootUsername, puid, pgid)
wg.Add(1) wg.Add(1)
// wait for unboundLooper.Restart or its ticker launched with RunRestartTicker // wait for unboundLooper.Restart or its ticker launched with RunRestartTicker
go unboundLooper.Run(ctx, wg, signalDNSReady) go unboundLooper.Run(ctx, wg, dnsReadyCh)
publicIPLooper := publicip.NewLooper( publicIPLooper := publicip.NewLooper(
httpClient, logger, allSettings.PublicIP, puid, pgid, os) httpClient, logger, allSettings.PublicIP, puid, pgid, os)
@@ -349,7 +348,7 @@ func printVersions(ctx context.Context, logger logging.Logger,
func collectStreamLines(ctx context.Context, wg *sync.WaitGroup, func collectStreamLines(ctx context.Context, wg *sync.WaitGroup,
streamMerger command.StreamMerger, streamMerger command.StreamMerger,
logger logging.Logger, signalTunnelReady func()) { logger logging.Logger, tunnelReadyCh chan<- struct{}) {
defer wg.Done() defer wg.Done()
// Blocking line merging paramsReader for openvpn and unbound // Blocking line merging paramsReader for openvpn and unbound
logger.Info("Launching standard output merger") logger.Info("Launching standard output merger")
@@ -370,10 +369,10 @@ func collectStreamLines(ctx context.Context, wg *sync.WaitGroup,
} }
switch { switch {
case strings.Contains(line, "Initialization Sequence Completed"): case strings.Contains(line, "Initialization Sequence Completed"):
signalTunnelReady() tunnelReadyCh <- struct{}{}
case strings.Contains(line, "TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)"): case strings.Contains(line, "TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)"): //nolint:lll
logger.Warn("This means that either...") logger.Warn("This means that either...")
logger.Warn("1. The VPN server IP address you are trying to connect to is no longer valid, see https://github.com/qdm12/gluetun/wiki/Update-servers-information") logger.Warn("1. The VPN server IP address you are trying to connect to is no longer valid, see https://github.com/qdm12/gluetun/wiki/Update-servers-information") //nolint:lll
logger.Warn("2. The VPN server crashed, try changing region") logger.Warn("2. The VPN server crashed, try changing region")
logger.Warn("3. Your Internet connection is not working, ensure it works") logger.Warn("3. Your Internet connection is not working, ensure it works")
logger.Warn("Feel free to create an issue at https://github.com/qdm12/gluetun/issues/new/choose") logger.Warn("Feel free to create an issue at https://github.com/qdm12/gluetun/issues/new/choose")

View File

@@ -17,7 +17,7 @@ import (
) )
type Looper interface { type Looper interface {
Run(ctx context.Context, wg *sync.WaitGroup, signalDNSReady func()) Run(ctx context.Context, wg *sync.WaitGroup, dnsReadyCh chan<- struct{})
RunRestartTicker(ctx context.Context, wg *sync.WaitGroup) RunRestartTicker(ctx context.Context, wg *sync.WaitGroup)
GetStatus() (status models.LoopStatus) GetStatus() (status models.LoopStatus)
SetStatus(status models.LoopStatus) (outcome string, err error) SetStatus(status models.LoopStatus) (outcome string, err error)
@@ -87,7 +87,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
} }
} }
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup, signalDNSReady func()) { func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup, dnsReadyCh chan<- struct{}) {
defer wg.Done() defer wg.Done()
const fallback = false const fallback = false
@@ -131,7 +131,7 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup, signalDNSReady fun
l.useUnencryptedDNS(fallback) l.useUnencryptedDNS(fallback)
} }
signalDNSReady() dnsReadyCh <- struct{}{}
stayHere := true stayHere := true
for stayHere { for stayHere {