1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00

use periodic task in dns.Server

This commit is contained in:
Darien Raymond 2018-02-08 17:00:22 +01:00
parent acc6ea3e6b
commit dffaef60b4
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -11,6 +11,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
)
const (
@ -37,6 +38,7 @@ type Server struct {
hosts map[string]net.IP
records map[string]*DomainRecord
servers []NameServer
task *signal.PeriodicTask
}
func New(ctx context.Context, config *Config) (*Server, error) {
@ -45,6 +47,13 @@ func New(ctx context.Context, config *Config) (*Server, error) {
servers: make([]NameServer, len(config.NameServers)),
hosts: config.GetInternalHosts(),
}
server.task = &signal.PeriodicTask{
Interval: time.Minute * 10,
Execute: func() error {
server.cleanup()
return nil
},
}
v := core.FromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
@ -75,16 +84,14 @@ func New(ctx context.Context, config *Config) (*Server, error) {
return server, nil
}
func (*Server) Interface() interface{} {
return (*Server)(nil)
}
// Start implements common.Runnable.
func (s *Server) Start() error {
return nil
return s.task.Start()
}
func (*Server) Close() error {
return nil
// Close implements common.Runnable.
func (s *Server) Close() error {
return s.task.Close()
}
func (s *Server) GetCached(domain string) []net.IP {
@ -98,18 +105,12 @@ func (s *Server) GetCached(domain string) []net.IP {
return nil
}
func (s *Server) tryCleanup() {
func (s *Server) cleanup() {
s.Lock()
defer s.Unlock()
if len(s.records) > 256 {
domains := make([]string, 0, 256)
for d, r := range s.records {
if r.Expired() {
domains = append(domains, d)
}
}
for _, d := range domains {
for d, r := range s.records {
if r.Expired() {
delete(s.records, d)
}
}
@ -126,8 +127,6 @@ func (s *Server) LookupIP(domain string) ([]net.IP, error) {
return ips, nil
}
s.tryCleanup()
for _, server := range s.servers {
response := server.QueryA(domain)
select {