mirror of https://github.com/v2ray/v2ray-core
Add a retry mechanism
parent
9e078d533d
commit
93625dd656
|
@ -0,0 +1,44 @@
|
||||||
|
package retry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
RetryFailed = errors.New("All retry attempts failed.")
|
||||||
|
)
|
||||||
|
|
||||||
|
type RetryStrategy interface {
|
||||||
|
On(func() error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryer struct {
|
||||||
|
NextDelay func(int) int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *retryer) On(method func() error) error {
|
||||||
|
attempt := 0
|
||||||
|
for {
|
||||||
|
err := method()
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
delay := r.NextDelay(attempt)
|
||||||
|
if delay < 0 {
|
||||||
|
return RetryFailed
|
||||||
|
}
|
||||||
|
<-time.After(time.Duration(delay) * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Timed(attempts int, delay int) RetryStrategy {
|
||||||
|
return &retryer{
|
||||||
|
NextDelay: func(attempt int) int {
|
||||||
|
if attempt >= attempts {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return delay
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
7
point.go
7
point.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/v2ray/v2ray-core/common/errors"
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
|
"github.com/v2ray/v2ray-core/common/retry"
|
||||||
"github.com/v2ray/v2ray-core/config"
|
"github.com/v2ray/v2ray-core/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -90,9 +91,9 @@ func (vp *Point) Start() error {
|
||||||
return errors.NewBadConfigurationError()
|
return errors.NewBadConfigurationError()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := vp.ich.Listen(vp.port)
|
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
||||||
// TODO: handle error
|
return vp.ich.Listen(vp.port)
|
||||||
return err
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
|
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
|
||||||
|
|
Loading…
Reference in New Issue