remove use of AfterFunc

pull/73/head
v2ray 9 years ago
parent c1831551c1
commit f7d3f22d84

@ -87,30 +87,15 @@ func (this *InboundDetourHandlerDynamic) Close() {
} }
} }
func (this *InboundDetourHandlerDynamic) Start() error { func (this *InboundDetourHandlerDynamic) refresh() error {
this.Lock() this.Lock()
defer this.Unlock() 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() 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 { for _, ich := range this.ichInUse {
port := this.pickUnusedPort() ich.port = this.pickUnusedPort()
ich.port = port
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
err := ich.handler.Listen(ich.port) err := ich.handler.Listen(ich.port)
if err != nil { if err != nil {
@ -126,3 +111,26 @@ func (this *InboundDetourHandlerDynamic) Start() error {
return nil 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
}

Loading…
Cancel
Save