mirror of https://github.com/v2ray/v2ray-core
avoid goroutine leak
parent
18d75cb7b4
commit
09ea65687c
|
@ -17,6 +17,8 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultTTL = uint32(3600)
|
DefaultTTL = uint32(3600)
|
||||||
|
CleanupInterval = time.Second * 120
|
||||||
|
CleanupThreshold = 512
|
||||||
)
|
)
|
||||||
|
|
||||||
type ARecord struct {
|
type ARecord struct {
|
||||||
|
@ -38,6 +40,7 @@ type UDPNameServer struct {
|
||||||
address v2net.Destination
|
address v2net.Destination
|
||||||
requests map[uint16]*PendingRequest
|
requests map[uint16]*PendingRequest
|
||||||
udpServer *hub.UDPServer
|
udpServer *hub.UDPServer
|
||||||
|
nextCleanup time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
|
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
|
||||||
|
@ -46,14 +49,11 @@ func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDis
|
||||||
requests: make(map[uint16]*PendingRequest),
|
requests: make(map[uint16]*PendingRequest),
|
||||||
udpServer: hub.NewUDPServer(dispatcher),
|
udpServer: hub.NewUDPServer(dispatcher),
|
||||||
}
|
}
|
||||||
go s.Cleanup()
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Private
|
// @Private
|
||||||
func (this *UDPNameServer) Cleanup() {
|
func (this *UDPNameServer) Cleanup() {
|
||||||
for {
|
|
||||||
time.Sleep(time.Second * 60)
|
|
||||||
expiredRequests := make([]uint16, 0, 16)
|
expiredRequests := make([]uint16, 0, 16)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
this.Lock()
|
this.Lock()
|
||||||
|
@ -68,13 +68,17 @@ func (this *UDPNameServer) Cleanup() {
|
||||||
}
|
}
|
||||||
this.Unlock()
|
this.Unlock()
|
||||||
expiredRequests = nil
|
expiredRequests = nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Private
|
// @Private
|
||||||
func (this *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
|
func (this *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
|
||||||
var id uint16
|
var id uint16
|
||||||
this.Lock()
|
this.Lock()
|
||||||
|
if len(this.requests) > CleanupThreshold && this.nextCleanup.Before(time.Now()) {
|
||||||
|
this.nextCleanup = time.Now().Add(CleanupInterval)
|
||||||
|
go this.Cleanup()
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
id = uint16(rand.Intn(65536))
|
id = uint16(rand.Intn(65536))
|
||||||
if _, found := this.requests[id]; found {
|
if _, found := this.requests[id]; found {
|
||||||
|
|
Loading…
Reference in New Issue