You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/router/routing_table.go

71 lines
1.1 KiB

8 years ago
package router
import (
"sync"
"time"
)
type RoutingEntry struct {
tag string
err error
expire time.Time
}
8 years ago
func (v *RoutingEntry) Extend() {
v.expire = time.Now().Add(time.Hour)
}
8 years ago
func (v *RoutingEntry) Expired() bool {
return v.expire.Before(time.Now())
}
type RoutingTable struct {
sync.RWMutex
table map[string]*RoutingEntry
}
func NewRoutingTable() *RoutingTable {
return &RoutingTable{
table: make(map[string]*RoutingEntry),
}
}
8 years ago
func (v *RoutingTable) Cleanup() {
v.Lock()
defer v.Unlock()
8 years ago
for key, value := range v.table {
if value.Expired() {
8 years ago
delete(v.table, key)
}
}
}
8 years ago
func (v *RoutingTable) Set(destination string, tag string, err error) {
v.Lock()
defer v.Unlock()
entry := &RoutingEntry{
tag: tag,
err: err,
}
entry.Extend()
8 years ago
v.table[destination] = entry
8 years ago
if len(v.table) > 1000 {
go v.Cleanup()
}
}
8 years ago
func (v *RoutingTable) Get(destination string) (bool, string, error) {
v.RLock()
defer v.RUnlock()
8 years ago
entry, found := v.table[destination]
if !found {
return false, "", nil
}
entry.Extend()
return true, entry.tag, entry.err
}