From 045e1dcc5329b68fee3fc0f94b1c50d6ee303b27 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 5 Jun 2016 23:39:38 +0200 Subject: [PATCH] refine dynamic strategy --- shell/point/inbound_detour_dynamic.go | 33 +++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/shell/point/inbound_detour_dynamic.go b/shell/point/inbound_detour_dynamic.go index e32500c50..f22de50ac 100644 --- a/shell/point/inbound_detour_dynamic.go +++ b/shell/point/inbound_detour_dynamic.go @@ -18,6 +18,7 @@ type InboundDetourHandlerDynamic struct { config *InboundDetourConfig portsInUse map[v2net.Port]bool ichs []proxy.InboundHandler + ich2Recyle []proxy.InboundHandler lastRefresh time.Time } @@ -74,11 +75,25 @@ func (this *InboundDetourHandlerDynamic) Close() { } } +func (this *InboundDetourHandlerDynamic) RecyleHandles() { + if this.ich2Recyle != nil { + for _, ich := range this.ich2Recyle { + if ich == nil { + continue + } + port := ich.Port() + ich.Close() + delete(this.portsInUse, port) + } + this.ich2Recyle = nil + } +} + func (this *InboundDetourHandlerDynamic) refresh() error { this.lastRefresh = time.Now() config := this.config - ich2Recycle := this.ichs + this.ich2Recyle = this.ichs newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency) for idx, _ := range newIchs { @@ -102,19 +117,6 @@ func (this *InboundDetourHandlerDynamic) refresh() error { this.ichs = newIchs this.Unlock() - go func() { - time.Sleep(time.Minute) - for _, ich := range ich2Recycle { - if ich == nil { - continue - } - port := ich.Port() - ich.Close() - delete(this.portsInUse, port) - } - ich2Recycle = nil - }() - return nil } @@ -127,7 +129,8 @@ func (this *InboundDetourHandlerDynamic) Start() error { go func() { for { - time.Sleep(time.Duration(this.config.Allocation.Refresh) * time.Minute) + time.Sleep(time.Duration(this.config.Allocation.Refresh)*time.Minute - 1) + this.RecyleHandles() err := this.refresh() if err != nil { log.Error("Point: Failed to refresh dynamic allocations: ", err)