diff --git a/shell/point/inbound_detour_dynamic.go b/shell/point/inbound_detour_dynamic.go index 0a6e9301..a4489b34 100644 --- a/shell/point/inbound_detour_dynamic.go +++ b/shell/point/inbound_detour_dynamic.go @@ -87,30 +87,15 @@ func (this *InboundDetourHandlerDynamic) Close() { } } -func (this *InboundDetourHandlerDynamic) Start() error { +func (this *InboundDetourHandlerDynamic) refresh() error { this.Lock() defer this.Unlock() - this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle - time.AfterFunc(time.Minute, func() { - this.Lock() - defer this.Unlock() - for _, ich := range this.ich2Recycle { - if ich != nil { - ich.handler.Close() - delete(this.portsInUse, ich.port) - } - } - }) - this.lastRefresh = time.Now() - time.AfterFunc(time.Duration(this.config.Allocation.Refresh)*time.Minute, func() { - this.Start() - }) + this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle for _, ich := range this.ichInUse { - port := this.pickUnusedPort() - ich.port = port + ich.port = this.pickUnusedPort() err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { err := ich.handler.Listen(ich.port) if err != nil { @@ -126,3 +111,26 @@ func (this *InboundDetourHandlerDynamic) Start() error { return nil } + +func (this *InboundDetourHandlerDynamic) Start() error { + err := this.refresh() + if err != nil { + return err + } + + go func() { + for range time.Tick(time.Duration(this.config.Allocation.Refresh) * time.Minute) { + this.refresh() + + <-time.After(time.Minute) + for _, ich := range this.ich2Recycle { + if ich != nil { + ich.handler.Close() + delete(this.portsInUse, ich.port) + } + } + } + }() + + return nil +}