Bläddra i källkod

Use fastcache instead of ristretto

tags/1.0^2
9seconds 6 år sedan
förälder
incheckning
ae479adeaa
5 ändrade filer med 28 tillägg och 35 borttagningar
  1. 10
    12
      antireplay/cache.go
  2. 2
    16
      antireplay/init.go
  3. 2
    2
      config/config.go
  4. 1
    1
      go.mod
  5. 13
    4
      go.sum

+ 10
- 12
antireplay/cache.go Visa fil

@@ -1,6 +1,6 @@
1 1
 package antireplay
2 2
 
3
-import "github.com/dgraph-io/ristretto"
3
+import "github.com/VictoriaMetrics/fastcache"
4 4
 
5 5
 var (
6 6
 	prefixObfuscated2 = []byte{0x00}
@@ -8,31 +8,29 @@ var (
8 8
 )
9 9
 
10 10
 type cache struct {
11
-	data *ristretto.Cache
11
+	data *fastcache.Cache
12 12
 }
13 13
 
14 14
 func (c *cache) AddObfuscated2(data []byte) {
15
-	c.data.Set(keyObfuscated2(data), nil, int64(len(data)))
15
+	c.data.Set(keyObfuscated2(data), nil)
16 16
 }
17 17
 
18 18
 func (c *cache) AddTLS(data []byte) {
19
-	c.data.Set(keyTLS(data), nil, int64(len(data)))
19
+	c.data.Set(keyTLS(data), nil)
20 20
 }
21 21
 
22 22
 func (c *cache) HasObfuscated2(data []byte) bool {
23
-	_, ok := c.data.Get(keyObfuscated2(data))
24
-	return ok
23
+	return c.data.Has(keyObfuscated2(data))
25 24
 }
26 25
 
27 26
 func (c *cache) HasTLS(data []byte) bool {
28
-	_, ok := c.data.Get(keyTLS(data))
29
-	return ok
27
+	return c.data.Has(keyTLS(data))
30 28
 }
31 29
 
32
-func keyObfuscated2(data []byte) string {
33
-	return string(append(prefixObfuscated2, data...))
30
+func keyObfuscated2(data []byte) []byte {
31
+	return append(prefixObfuscated2, data...)
34 32
 }
35 33
 
36
-func keyTLS(data []byte) string {
37
-	return string(append(prefixTLS, data...))
34
+func keyTLS(data []byte) []byte {
35
+	return append(prefixTLS, data...)
38 36
 }

+ 2
- 16
antireplay/init.go Visa fil

@@ -1,10 +1,9 @@
1 1
 package antireplay
2 2
 
3 3
 import (
4
-	"math"
5 4
 	"sync"
6 5
 
7
-	"github.com/dgraph-io/ristretto"
6
+	"github.com/VictoriaMetrics/fastcache"
8 7
 
9 8
 	"mtg/config"
10 9
 )
@@ -16,19 +15,6 @@ var (
16 15
 
17 16
 func Init() {
18 17
 	initOnce.Do(func() {
19
-		cost := float64(config.C.AntiReplayMaxSize) / 32.0
20
-		cost = math.Ceil(cost)
21
-
22
-		c, err := ristretto.NewCache(&ristretto.Config{
23
-			NumCounters: int64(cost) * 10,
24
-			MaxCost:     config.C.AntiReplayMaxSize,
25
-			BufferItems: 64,
26
-			Metrics:     false,
27
-		})
28
-		if err != nil {
29
-			panic(err)
30
-		}
31
-
32
-		Cache.data = c
18
+		Cache.data = fastcache.New(config.C.AntiReplayMaxSize)
33 19
 	})
34 20
 }

+ 2
- 2
config/config.go Visa fil

@@ -80,7 +80,7 @@ type Config struct {
80 80
 	ReadBuffer  int `json:"read_buffer"`
81 81
 	CloakPort   int `json:"cloak_port"`
82 82
 
83
-	AntiReplayMaxSize int64 `json:"anti_replay_max_size"`
83
+	AntiReplayMaxSize int `json:"anti_replay_max_size"`
84 84
 
85 85
 	MultiplexPerConnection int `json:"multiplex_per_connection"`
86 86
 
@@ -152,7 +152,7 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
152 152
 		case OptionTypeCloakPort:
153 153
 			C.CloakPort = int(opt.Value.(uint16))
154 154
 		case OptionTypeAntiReplayMaxSize:
155
-			C.AntiReplayMaxSize = int64(opt.Value.(units.Base2Bytes))
155
+			C.AntiReplayMaxSize = int(opt.Value.(units.Base2Bytes))
156 156
 		case OptionTypeMultiplexPerConnection:
157 157
 			C.MultiplexPerConnection = int(opt.Value.(uint))
158 158
 		case OptionTypeSecret:

+ 1
- 1
go.mod Visa fil

@@ -3,9 +3,9 @@ module mtg
3 3
 go 1.13
4 4
 
5 5
 require (
6
+	github.com/VictoriaMetrics/fastcache v1.5.2
6 7
 	github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
7 8
 	github.com/beevik/ntp v0.2.0
8
-	github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca
9 9
 	github.com/prometheus/client_golang v1.2.1
10 10
 	go.uber.org/multierr v1.4.0 // indirect
11 11
 	go.uber.org/zap v1.12.0

+ 13
- 4
go.sum Visa fil

@@ -1,5 +1,9 @@
1 1
 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2 2
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4
+github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
5
+github.com/VictoriaMetrics/fastcache v1.5.2 h1:Erd8iIuBAL9kke8JzM4+WxkKuFkHh3ktwLanJvDgR44=
6
+github.com/VictoriaMetrics/fastcache v1.5.2/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE=
3 7
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
4 8
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5 9
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
@@ -10,6 +14,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1C
10 14
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
11 15
 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
12 16
 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
17
+github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
18
+github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
13 19
 github.com/beevik/ntp v0.2.0 h1:sGsd+kAXzT0bfVfzJfce04g+dSRfrs+tbQW8lweuYgw=
14 20
 github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
15 21
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
@@ -18,15 +24,14 @@ github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
18 24
 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
19 25
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
20 26
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
27
+github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
28
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
29
+github.com/cespare/xxhash/v2 v2.0.1-0.20190104013014-3767db7a7e18/go.mod h1:HD5P3vAIAh+Y2GAxg0PrPN1P8WkepXGpjbUPDHJqqKM=
21 30
 github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA=
22 31
 github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM=
23 32
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24 33
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
25 34
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26
-github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca h1:NgFAOh7RDM5WbdADm6VyLG1koLrzG0G24M/f5395PcI=
27
-github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca/go.mod h1:edzKIzGvqUCMzhTVWbiTSe75zD9Xxq0GtSBtFmaUTZs=
28
-github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
29
-github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
30 35
 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
31 36
 github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
32 37
 github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
@@ -40,6 +45,8 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
40 45
 github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
41 46
 github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
42 47
 github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
48
+github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
49
+github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
43 50
 github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
44 51
 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
45 52
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -91,6 +98,8 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa
91 98
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
92 99
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
93 100
 github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
101
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
102
+github.com/spaolacci/murmur3 v1.0.1-0.20190317074736-539464a789e9/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
94 103
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
95 104
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
96 105
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=

Laddar…
Avbryt
Spara