-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache test Add json rpc cache to executable Make cache config optional, debug logs in cache, fix dispatcher url path Refactor json rpc cache Fix cache keys Use mappers from the forta-core-go Move jsonrpc changes of proxy to cache package Tweak events polling in cache Use go-cache package Rename cbe (Combined Block Event) to BlockData Add retries to r2_client Check 404 error body, check if node has assigned bots Inject sdk cache configuration to agents Refactor Fix registry usage Add logging Update forta-core-go Metrics draft Fix nil panic Fix params string Add log on cache miss Improve polling Increase bucket time if no bots assigned Decrease max backoff to 10 seconds Fix bug in r2 client Retry on 'Block too old' Clean metrics in jsonrpc cache
- Loading branch information
Showing
18 changed files
with
886 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package blocksdata | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/andybalholm/brotli" | ||
backoff "github.com/cenkalti/backoff/v4" | ||
"github.com/forta-network/forta-core-go/protocol" | ||
"github.com/forta-network/forta-core-go/utils/httpclient" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
const ( | ||
minBackoff = 1 * time.Second | ||
maxBackoff = 10 * time.Second | ||
maxElapsedTime = 5 * time.Minute | ||
) | ||
|
||
type blocksDataClient struct { | ||
dispatcherURL *url.URL | ||
} | ||
|
||
func NewBlocksDataClient(dispatcherURL string) *blocksDataClient { | ||
u, _ := url.Parse(dispatcherURL) | ||
|
||
return &blocksDataClient{ | ||
dispatcherURL: u, | ||
} | ||
} | ||
|
||
type PresignedURLItem struct { | ||
Bucket int64 `json:"bucket"` | ||
PresignedURL string `json:"presignedURL"` | ||
ExpiresAt int64 `json:"expiresAt"` | ||
} | ||
|
||
func (c *blocksDataClient) GetBlocksData(bucket int64) (_ *protocol.BlocksData, err error) { | ||
dispatcherUrl, err := url.JoinPath(c.dispatcherURL.String(), fmt.Sprintf("%d", bucket)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bo := backoff.NewExponentialBackOff() | ||
bo.InitialInterval = minBackoff | ||
bo.MaxInterval = maxBackoff | ||
bo.MaxElapsedTime = maxElapsedTime | ||
|
||
var item PresignedURLItem | ||
|
||
err = backoff.Retry(func() error { | ||
resp, err := httpclient.Default.Get(dispatcherUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode == 404 && bytes.Contains(b, []byte("too old")) { | ||
return fmt.Errorf("%s", b) | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, b) | ||
} | ||
|
||
err = json.Unmarshal(b, &item) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if item.ExpiresAt < time.Now().Unix() { | ||
return backoff.Permanent(fmt.Errorf("presigned URL expired")) | ||
} | ||
|
||
return nil | ||
}, bo) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var blocks protocol.BlocksData | ||
|
||
err = backoff.Retry(func() error { | ||
resp, err := httpclient.Default.Get(item.PresignedURL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
b, err := io.ReadAll(brotli.NewReader(resp.Body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = proto.Unmarshal(b, &blocks) | ||
if err != nil { | ||
return backoff.Permanent(err) | ||
} | ||
|
||
return nil | ||
}, bo) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &blocks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.