Skip to content

Commit

Permalink
fix: updating bypass function for using and checking context timeout,…
Browse files Browse the repository at this point in the history
… if it happens it should stop retrying to load the proxy async
  • Loading branch information
WendelHime committed Nov 11, 2024
1 parent 38dba03 commit f67faab
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions bypass/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,49 @@ func (b *bypass) OnProxies(infos map[string]*commonconfig.ProxyConfig, configDir
continue
}

// if dialer is not ready, try again in 60s
// if dialer is not ready, try to load it async
if ready, err := dialer.IsReady(); err == nil && !ready {
log.Debugf("dialer %q is not ready, starting in background", name)
go func() {
for {
time.Sleep(60 * time.Second)
ready, err := dialer.IsReady()
if err != nil {
log.Errorf("dialer isn't ready and returned an error: %w", err)
break
}
if !ready {
b.startProxy(name, config, configDir, userConfig, dialer)
break
}
}
}()
go b.loadProxyAsync(name, config, configDir, userConfig, dialer)
continue
}

b.startProxy(name, config, configDir, userConfig, dialer)
}
}

func (b *bypass) loadProxyAsync(proxyName string, config *commonconfig.ProxyConfig, configDir string, userConfig common.UserConfig, dialer bandit.Dialer) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
readyChan := make(chan struct{})
retry := atomic.NewBool(true)
go func() {
for retry.Load() {
time.Sleep(15 * time.Second)
ready, err := dialer.IsReady()
if err != nil {
log.Errorf("dialer isn't ready and returned an error: %w", err)
break
}
if !ready {
b.startProxy(proxyName, config, configDir, userConfig, dialer)
readyChan <- struct{}{}
break
}
}
}()
select {
case _, ok := <-readyChan:
if !ok {
log.Errorf("ready channel for proxy %q is closed", proxyName)
}
case <-ctx.Done():
log.Errorf("proxy %q took to long to get ready", proxyName)
}
retry.Store(false)
close(readyChan)
}

func (b *bypass) startProxy(proxyName string, config *commonconfig.ProxyConfig, configDir string, userConfig common.UserConfig, dialer bandit.Dialer) {
pc := chained.CopyConfig(config)
// Set the name in the info since we know it here.
Expand Down

0 comments on commit f67faab

Please sign in to comment.