Skip to content

Commit

Permalink
shovel/jrpc2: disable jrpc2 caching via url
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotsmith committed Feb 28, 2024
1 parent 57755b4 commit ec99c09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions jrpc2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (

func New(url string) *Client {
return &Client{
d: strings.Contains(url, "debug"),
d: strings.Contains(url, "debug"),
nocache: strings.Contains(url, "nocache"),
hc: &http.Client{
Timeout: 10 * time.Second,
Transport: gzhttp.Transport(http.DefaultTransport),
Expand All @@ -42,10 +43,11 @@ func New(url string) *Client {
}

type Client struct {
d bool
hc *http.Client
url string
wsurl string
nocache bool
d bool
hc *http.Client
url string
wsurl string

lcache NumHash
bcache cache
Expand Down Expand Up @@ -358,12 +360,12 @@ func (c *Client) Get(
)
switch {
case filter.UseBlocks:
blocks, err = c.bcache.get(ctx, start, limit, c.blocks)
blocks, err = c.bcache.get(c.nocache, ctx, start, limit, c.blocks)
if err != nil {
return nil, fmt.Errorf("getting blocks: %w", err)
}
case filter.UseHeaders:
blocks, err = c.hcache.get(ctx, start, limit, c.headers)
blocks, err = c.hcache.get(c.nocache, ctx, start, limit, c.headers)
if err != nil {
return nil, fmt.Errorf("getting blocks: %w", err)
}
Expand Down Expand Up @@ -435,7 +437,10 @@ func (c *cache) prune() {
}
}

func (c *cache) get(ctx context.Context, start, limit uint64, f getter) ([]eth.Block, error) {
func (c *cache) get(nocache bool, ctx context.Context, start, limit uint64, f getter) ([]eth.Block, error) {
if nocache {
return f(ctx, start, limit)
}
c.Lock()
if c.segments == nil {
c.segments = make(map[key]*segment)
Expand Down
4 changes: 2 additions & 2 deletions jrpc2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func TestCache_Prune(t *testing.T) {
ctx := context.Background()
tg := testGetter{}
c := cache{}
blocks, err := c.get(ctx, 1, 1, tg.get)
blocks, err := c.get(false, ctx, 1, 1, tg.get)
diff.Test(t, t.Fatalf, nil, err)
diff.Test(t, t.Errorf, 1, len(blocks))
diff.Test(t, t.Errorf, 1, tg.callCount)
diff.Test(t, t.Errorf, 1, len(c.segments))

for i := uint64(0); i < 9; i++ {
blocks, err := c.get(ctx, 2+i, 1, tg.get)
blocks, err := c.get(false, ctx, 2+i, 1, tg.get)
diff.Test(t, t.Fatalf, nil, err)
diff.Test(t, t.Errorf, 1, len(blocks))
}
Expand Down

0 comments on commit ec99c09

Please sign in to comment.