Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
269769b3
Commit
269769b3
authored
Jan 12, 2022
by
inphi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add redis cache impl
parent
18abe83b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
8 deletions
+51
-8
cache.go
go/proxyd/cache.go
+40
-6
proxyd.go
go/proxyd/proxyd.go
+10
-1
server.go
go/proxyd/server.go
+1
-1
No files found.
go/proxyd/cache.go
View file @
269769b3
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"context"
"context"
"encoding/json"
"encoding/json"
"github.com/go-redis/redis/v8"
"github.com/golang/snappy"
"github.com/golang/snappy"
lru
"github.com/hashicorp/golang-lru"
lru
"github.com/hashicorp/golang-lru"
)
)
...
@@ -13,7 +14,8 @@ type Cache interface {
...
@@ -13,7 +14,8 @@ type Cache interface {
Put
(
ctx
context
.
Context
,
key
string
,
value
string
)
error
Put
(
ctx
context
.
Context
,
key
string
,
value
string
)
error
}
}
const
memoryCacheLimit
=
1024
*
1024
// assuming an average RPCRes size of 3 KB
const
memoryCacheLimit
=
4096
var
supportedRPCMethods
=
map
[
string
]
bool
{
var
supportedRPCMethods
=
map
[
string
]
bool
{
"eth_chainId"
:
true
,
"eth_chainId"
:
true
,
...
@@ -43,6 +45,35 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
...
@@ -43,6 +45,35 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
return
nil
return
nil
}
}
type
redisCache
struct
{
rdb
*
redis
.
Client
}
func
newRedisCache
(
url
string
)
(
*
redisCache
,
error
)
{
opts
,
err
:=
redis
.
ParseURL
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
rdb
:=
redis
.
NewClient
(
opts
)
if
err
:=
rdb
.
Ping
(
context
.
Background
())
.
Err
();
err
!=
nil
{
return
nil
,
wrapErr
(
err
,
"error connecting to redis"
)
}
return
&
redisCache
{
rdb
},
nil
}
func
(
c
*
redisCache
)
Get
(
ctx
context
.
Context
,
key
string
)
(
string
,
error
)
{
val
,
err
:=
c
.
rdb
.
Get
(
ctx
,
key
)
.
Result
()
if
err
!=
nil
{
return
""
,
err
}
return
val
,
nil
}
func
(
c
*
redisCache
)
Put
(
ctx
context
.
Context
,
key
string
,
value
string
)
error
{
err
:=
c
.
rdb
.
Set
(
ctx
,
key
,
value
,
0
)
.
Err
()
return
err
}
type
RPCCache
struct
{
type
RPCCache
struct
{
cache
Cache
cache
Cache
}
}
...
@@ -95,13 +126,12 @@ func (c *RPCCache) isCacheable(req *RPCReq) bool {
...
@@ -95,13 +126,12 @@ func (c *RPCCache) isCacheable(req *RPCReq) bool {
return
false
return
false
}
}
var
params
[]
interface
{}
if
err
:=
json
.
Unmarshal
(
req
.
Params
,
&
params
);
err
!=
nil
{
return
false
}
switch
req
.
Method
{
switch
req
.
Method
{
case
"eth_getBlockByNumber"
:
case
"eth_getBlockByNumber"
:
var
params
[]
interface
{}
if
err
:=
json
.
Unmarshal
(
req
.
Params
,
&
params
);
err
!=
nil
{
return
false
}
if
len
(
params
)
!=
2
{
if
len
(
params
)
!=
2
{
return
false
return
false
}
}
...
@@ -114,6 +144,10 @@ func (c *RPCCache) isCacheable(req *RPCReq) bool {
...
@@ -114,6 +144,10 @@ func (c *RPCCache) isCacheable(req *RPCReq) bool {
}
}
case
"eth_getBlockRange"
:
case
"eth_getBlockRange"
:
var
params
[]
interface
{}
if
err
:=
json
.
Unmarshal
(
req
.
Params
,
&
params
);
err
!=
nil
{
return
false
}
if
len
(
params
)
!=
3
{
if
len
(
params
)
!=
3
{
return
false
return
false
}
}
...
...
go/proxyd/proxyd.go
View file @
269769b3
...
@@ -156,7 +156,16 @@ func Start(config *Config) error {
...
@@ -156,7 +156,16 @@ func Start(config *Config) error {
var
rpcCache
*
RPCCache
var
rpcCache
*
RPCCache
if
config
.
Cache
!=
nil
&&
config
.
Cache
.
Enabled
{
if
config
.
Cache
!=
nil
&&
config
.
Cache
.
Enabled
{
rpcCache
=
newRPCCache
(
newMemoryCache
())
var
cache
Cache
if
config
.
Redis
!=
nil
{
if
cache
,
err
=
newRedisCache
(
config
.
Redis
.
URL
);
err
!=
nil
{
return
err
}
}
else
{
log
.
Warn
(
"redis is not configured, using in-memory cache"
)
cache
=
newMemoryCache
()
}
rpcCache
=
newRPCCache
(
cache
)
}
}
srv
:=
NewServer
(
srv
:=
NewServer
(
...
...
go/proxyd/server.go
View file @
269769b3
...
@@ -172,7 +172,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
...
@@ -172,7 +172,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
return
return
}
}
if
s
.
rpcCache
!=
nil
{
if
s
.
rpcCache
!=
nil
&&
backendRes
.
Error
==
nil
{
if
err
=
s
.
rpcCache
.
PutRPC
(
ctx
,
req
,
backendRes
);
err
!=
nil
{
if
err
=
s
.
rpcCache
.
PutRPC
(
ctx
,
req
,
backendRes
);
err
!=
nil
{
log
.
Warn
(
log
.
Warn
(
"cache put error"
,
"cache put error"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment