chore: upgrade to hashicorp/golang-lru v2 (closes #500)

This commit is contained in:
Toby 2022-11-19 13:01:15 -08:00
parent b386bfbeef
commit a2bc061e74
4 changed files with 12 additions and 861 deletions

4
go.mod
View File

@ -1,6 +1,6 @@
module github.com/apernet/hysteria module github.com/apernet/hysteria
go 1.17 go 1.18
require ( require (
github.com/LiamHaworth/go-tproxy v0.0.0-20190726054950-ef7efd7f24ed github.com/LiamHaworth/go-tproxy v0.0.0-20190726054950-ef7efd7f24ed
@ -13,7 +13,7 @@ require (
github.com/folbricht/routedns v0.1.6-0.20220806202012-361f5b35b4c3 github.com/folbricht/routedns v0.1.6-0.20220806202012-361f5b35b4c3
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/google/gopacket v1.1.19 github.com/google/gopacket v1.1.19
github.com/hashicorp/golang-lru v0.5.4 github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/lucas-clemente/quic-go v0.31.0 github.com/lucas-clemente/quic-go v0.31.0
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
github.com/oschwald/geoip2-golang v1.8.0 github.com/oschwald/geoip2-golang v1.8.0

852
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,9 @@ import (
"os" "os"
"strings" "strings"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/apernet/hysteria/pkg/utils" "github.com/apernet/hysteria/pkg/utils"
lru "github.com/hashicorp/golang-lru"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
) )
@ -16,7 +17,7 @@ const entryCacheSize = 1024
type Engine struct { type Engine struct {
DefaultAction Action DefaultAction Action
Entries []Entry Entries []Entry
Cache *lru.ARCCache Cache *lru.ARCCache[cacheKey, cacheValue]
ResolveIPAddr func(string) (*net.IPAddr, error) ResolveIPAddr func(string) (*net.IPAddr, error)
GeoIPReader *geoip2.Reader GeoIPReader *geoip2.Reader
} }
@ -59,7 +60,7 @@ func LoadFromFile(filename string, resolveIPAddr func(string) (*net.IPAddr, erro
} }
entries = append(entries, entry) entries = append(entries, entry)
} }
cache, err := lru.NewARC(entryCacheSize) cache, err := lru.NewARC[cacheKey, cacheValue](entryCacheSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -78,9 +79,8 @@ func (e *Engine) ResolveAndMatch(host string, port uint16, isUDP bool) (Action,
if ip == nil { if ip == nil {
// Domain // Domain
ipAddr, err := e.ResolveIPAddr(host) ipAddr, err := e.ResolveIPAddr(host)
if v, ok := e.Cache.Get(cacheKey{host, port, isUDP}); ok { if ce, ok := e.Cache.Get(cacheKey{host, port, isUDP}); ok {
// Cache hit // Cache hit
ce := v.(cacheValue)
return ce.Action, ce.Arg, true, ipAddr, err return ce.Action, ce.Arg, true, ipAddr, err
} }
for _, entry := range e.Entries { for _, entry := range e.Entries {
@ -107,9 +107,8 @@ func (e *Engine) ResolveAndMatch(host string, port uint16, isUDP bool) (Action,
return e.DefaultAction, "", true, ipAddr, err return e.DefaultAction, "", true, ipAddr, err
} else { } else {
// IP // IP
if v, ok := e.Cache.Get(cacheKey{ip.String(), port, isUDP}); ok { if ce, ok := e.Cache.Get(cacheKey{ip.String(), port, isUDP}); ok {
// Cache hit // Cache hit
ce := v.(cacheValue)
return ce.Action, ce.Arg, false, &net.IPAddr{ return ce.Action, ce.Arg, false, &net.IPAddr{
IP: ip, IP: ip,
Zone: zone, Zone: zone,

View File

@ -6,11 +6,11 @@ import (
"strings" "strings"
"testing" "testing"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru/v2"
) )
func TestEngine_ResolveAndMatch(t *testing.T) { func TestEngine_ResolveAndMatch(t *testing.T) {
cache, _ := lru.NewARC(16) cache, _ := lru.NewARC[cacheKey, cacheValue](entryCacheSize)
e := &Engine{ e := &Engine{
DefaultAction: ActionDirect, DefaultAction: ActionDirect,
Entries: []Entry{ Entries: []Entry{