mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-14 14:47:12 +00:00
remove validity map
This commit is contained in:
1 parent
21ab26f41f
commit
3a6bf38686
3 files changed
+76
-109
No files matched your search
@@ -2,12 +2,10 @@ package rules
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/app/dns"
|
||||
"github.com/v2ray/v2ray-core/app/router"
|
||||
"github.com/v2ray/v2ray-core/common/collect"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
@@ -17,43 +15,16 @@ var (
|
||||
ErrorNoRuleApplicable = errors.New("No rule applicable")
|
||||
)
|
||||
|
||||
type cacheEntry struct {
|
||||
tag string
|
||||
err error
|
||||
validUntil time.Time
|
||||
}
|
||||
|
||||
func newCacheEntry(tag string, err error) *cacheEntry {
|
||||
this := &cacheEntry{
|
||||
tag: tag,
|
||||
err: err,
|
||||
}
|
||||
this.Extend()
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *cacheEntry) IsValid() bool {
|
||||
return this.validUntil.Before(time.Now())
|
||||
}
|
||||
|
||||
func (this *cacheEntry) Extend() {
|
||||
this.validUntil = time.Now().Add(time.Hour)
|
||||
}
|
||||
|
||||
func (this *cacheEntry) Release() {
|
||||
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
config *RouterRuleConfig
|
||||
cache *collect.ValidityMap
|
||||
cache *RoutingTable
|
||||
dnsServer dns.Server
|
||||
}
|
||||
|
||||
func NewRouter(config *RouterRuleConfig, space app.Space) *Router {
|
||||
r := &Router{
|
||||
config: config,
|
||||
cache: collect.NewValidityMap(3600),
|
||||
cache: NewRoutingTable(),
|
||||
}
|
||||
space.InitializeApplication(func() error {
|
||||
if !space.HasApp(dns.APP_ID) {
|
||||
@@ -113,14 +84,13 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro
|
||||
|
||||
func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
|
||||
destStr := dest.String()
|
||||
rawEntry := this.cache.Get(destStr)
|
||||
if rawEntry == nil {
|
||||
found, tag, err := this.cache.Get(destStr)
|
||||
if !found {
|
||||
tag, err := this.takeDetourWithoutCache(dest)
|
||||
this.cache.Set(destStr, newCacheEntry(tag, err))
|
||||
this.cache.Set(destStr, tag, err)
|
||||
return tag, err
|
||||
}
|
||||
entry := rawEntry.(*cacheEntry)
|
||||
return entry.tag, entry.err
|
||||
return tag, err
|
||||
}
|
||||
|
||||
type RouterFactory struct {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RoutingEntry struct {
|
||||
tag string
|
||||
err error
|
||||
expire time.Time
|
||||
}
|
||||
|
||||
func (this *RoutingEntry) Extend() {
|
||||
this.expire = time.Now().Add(time.Hour)
|
||||
}
|
||||
|
||||
func (this *RoutingEntry) Expired() bool {
|
||||
return this.expire.Before(time.Now())
|
||||
}
|
||||
|
||||
type RoutingTable struct {
|
||||
sync.RWMutex
|
||||
table map[string]*RoutingEntry
|
||||
}
|
||||
|
||||
func NewRoutingTable() *RoutingTable {
|
||||
return &RoutingTable{
|
||||
table: make(map[string]*RoutingEntry),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RoutingTable) Cleanup() {
|
||||
this.Lock()
|
||||
defer this.Unlock()
|
||||
|
||||
for key, value := range this.table {
|
||||
if value.Expired() {
|
||||
delete(this.table, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RoutingTable) Set(destination string, tag string, err error) {
|
||||
this.Lock()
|
||||
defer this.Unlock()
|
||||
|
||||
entry := &RoutingEntry{
|
||||
tag: tag,
|
||||
err: err,
|
||||
}
|
||||
entry.Extend()
|
||||
this.table[destination] = entry
|
||||
|
||||
if len(this.table) > 1000 {
|
||||
go this.Cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *RoutingTable) Get(destination string) (bool, string, error) {
|
||||
this.RLock()
|
||||
defer this.RUnlock()
|
||||
|
||||
entry, found := this.table[destination]
|
||||
if !found {
|
||||
return false, "", nil
|
||||
}
|
||||
entry.Extend()
|
||||
return true, entry.tag, entry.err
|
||||
}
|
||||
Reference in new issue
Block a user