From edd9885d0ecc95b4e6a026aef55fc71662ad4647 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 24 Jul 2016 10:06:50 +0200 Subject: [PATCH] retry on port allocation --- shell/point/inbound_detour_dynamic.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/shell/point/inbound_detour_dynamic.go b/shell/point/inbound_detour_dynamic.go index 4d90b336b..b1fd5f500 100644 --- a/shell/point/inbound_detour_dynamic.go +++ b/shell/point/inbound_detour_dynamic.go @@ -8,6 +8,7 @@ import ( "github.com/v2ray/v2ray-core/common/dice" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/proxy" proxyrepo "github.com/v2ray/v2ray-core/proxy/repo" ) @@ -98,20 +99,25 @@ func (this *InboundDetourHandlerDynamic) refresh() error { newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency) for idx, _ := range newIchs { - port := this.pickUnusedPort() - ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{ - Address: config.ListenOn, Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings}) + err := retry.Timed(5, 100).On(func() error { + port := this.pickUnusedPort() + ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{ + Address: config.ListenOn, Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings}) + if err != nil { + return err + } + err = ich.Start() + if err != nil { + return err + } + this.portsInUse[port] = true + newIchs[idx] = ich + return nil + }) if err != nil { log.Error("Point: Failed to create inbound connection handler: ", err) return err } - err = ich.Start() - if err != nil { - log.Error("Point: Failed to start inbound connection handler: ", err) - return err - } - this.portsInUse[port] = true - newIchs[idx] = ich } this.Lock()