wipp
This commit is contained in:
@@ -2,6 +2,7 @@ package shadowsocks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -21,22 +22,9 @@ type Loop struct {
|
|||||||
stop, stopped chan struct{}
|
stop, stopped chan struct{}
|
||||||
start chan struct{}
|
start chan struct{}
|
||||||
backoffTime time.Duration
|
backoffTime time.Duration
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Loop) logAndWait(ctx context.Context, err error) {
|
runCancel context.CancelFunc
|
||||||
if err != nil {
|
runDone <-chan struct{}
|
||||||
l.logger.Error(err.Error())
|
|
||||||
}
|
|
||||||
l.logger.Info("retrying in " + l.backoffTime.String())
|
|
||||||
timer := time.NewTimer(l.backoffTime)
|
|
||||||
l.backoffTime *= 2
|
|
||||||
select {
|
|
||||||
case <-timer.C:
|
|
||||||
case <-ctx.Done():
|
|
||||||
if !timer.Stop() {
|
|
||||||
<-timer.C
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultBackoffTime = 10 * time.Second
|
const defaultBackoffTime = 10 * time.Second
|
||||||
@@ -56,23 +44,39 @@ func NewLoop(settings settings.Shadowsocks, logger Logger) *Loop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
func (l *Loop) Start(ctx context.Context) (runError <-chan error, err error) {
|
||||||
defer close(done)
|
runCtx, runCancel := context.WithCancel(context.Background())
|
||||||
|
l.runCancel = runCancel
|
||||||
|
ready := make(chan struct{})
|
||||||
|
done := make(chan struct{})
|
||||||
|
l.runDone = done
|
||||||
|
|
||||||
crashed := false
|
go l.run(runCtx, ready, done)
|
||||||
|
|
||||||
|
<-ready
|
||||||
|
|
||||||
if *l.GetSettings().Enabled {
|
if *l.GetSettings().Enabled {
|
||||||
go func() {
|
_, err = l.SetStatus(ctx, constants.Running)
|
||||||
_, _ = l.SetStatus(ctx, constants.Running)
|
if err != nil {
|
||||||
}()
|
return nil, fmt.Errorf("setting running status: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, nil //nolint:nilnil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Loop) run(ctx context.Context, ready, done chan<- struct{}) {
|
||||||
|
defer close(done)
|
||||||
|
close(ready)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-l.start:
|
case <-l.start:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
crashed := false
|
||||||
|
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
settings := l.GetSettings()
|
settings := l.GetSettings()
|
||||||
server, err := shadowsockslib.NewServer(settings.Settings, l.logger)
|
server, err := shadowsockslib.NewServer(settings.Settings, l.logger)
|
||||||
@@ -138,8 +142,28 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
shadowsocksCancel() // repetition for linter only
|
shadowsocksCancel() // repetition for linter only
|
||||||
if !isStableTimer.Stop() {
|
isStableTimer.Stop()
|
||||||
<-isStableTimer.C
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Loop) Stop() (err error) {
|
||||||
|
l.runCancel()
|
||||||
|
<-l.runDone
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Loop) logAndWait(ctx context.Context, err error) {
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
l.logger.Info("retrying in " + l.backoffTime.String())
|
||||||
|
timer := time.NewTimer(l.backoffTime)
|
||||||
|
l.backoffTime *= 2
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
case <-ctx.Done():
|
||||||
|
if !timer.Stop() {
|
||||||
|
<-timer.C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user