1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/router/routing_table.go
2016-11-27 21:39:09 +01:00

71 lines
1.1 KiB
Go

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