Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump web3protocol-go: Better graceful handling of 429, group processing of identical requests. #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/server/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type NameServiceInfo struct {
type ChainConfig struct {
ChainID int
RPC string
RPCMaxConcurrentRequests int
SystemRPC string
NSConfig map[string]NameServiceInfo
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ func initWeb3protocolClient() {
// Config the chain
web3pChainConfig := web3protocol.ChainConfig{
ChainId: chainConfig.ChainID,
RPC: chainConfig.RPC,
RPC: web3protocol.ChainRPCConfig {
Url: chainConfig.RPC,
MaxConcurrentRequests: chainConfig.RPCMaxConcurrentRequests,
},
SystemRPC: chainConfig.SystemRPC,
DomainNameServices: map[web3protocol.DomainNameService]web3protocol.DomainNameServiceChainConfig{},
}

Expand Down
17 changes: 9 additions & 8 deletions cmd/server/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
p, _, er := handleSubdomain(h, path)
if er != nil {
log.Errorf("%s%s => Error converting subdomain: %s", h, req.URL.String(), er)
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, er.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: er})
return
}
// Make it a full web3 URL
Expand Down Expand Up @@ -130,7 +130,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
// Send the output
_, err := w.Write(cacheEntry.Body)
if err != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, err.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: err})
return
}
return
Expand Down Expand Up @@ -173,7 +173,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
// Send the output
_, err = w.Write(cacheEntry.Body)
if err != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, err.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: err})
return
}
return
Expand Down Expand Up @@ -268,7 +268,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
// Fetch data from web3protocol-go
n, err := fetchedWeb3Url.Output.Read(buf)
if err != nil && err != io.EOF {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, err.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: err})
return
}
if n == 0 {
Expand All @@ -288,7 +288,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
// Feed the data to the HTTP client
_, err = w.Write(buf[:n])
if err != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, err.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: err})
return
}

Expand All @@ -300,7 +300,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
if willCacheResponseAsType != "" {
_, err = cacheResponseWriter.Write(buf[:n])
if err != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, err.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: err})
return
}
}
Expand Down Expand Up @@ -337,6 +337,7 @@ func handle(w http.ResponseWriter, req *http.Request) {
"domain": "web3urlGateway",
"vary-headers": pageCacheKey.AcceptEncodingHeader,
"type": string(willCacheResponseAsType),
"size": len(newCacheEntry.Body),
}
if willCacheResponseAsType == PageCacheEntryTypeHttpCaching {
logFields["etag"] = newCacheEntry.ETag
Expand Down Expand Up @@ -364,8 +365,8 @@ func handle(w http.ResponseWriter, req *http.Request) {
func respondWithErrorPage(w http.ResponseWriter, err error) {
httpCode := 400
switch err.(type) {
case *web3protocol.ErrorWithHttpCode:
httpCode = err.(*web3protocol.ErrorWithHttpCode).HttpCode
case *web3protocol.Web3ProtocolError:
httpCode = err.(*web3protocol.Web3ProtocolError).HttpCode
}

w.WriteHeader(httpCode)
Expand Down
7 changes: 4 additions & 3 deletions cmd/server/sub_protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"errors"
"net/http"
"strings"

Expand All @@ -17,20 +18,20 @@ import (
func handleOrdinals(w http.ResponseWriter, req *http.Request, path string) {
temp := strings.Split(path, "/")
if len(temp) != 3 || (temp[1] != "txid" && temp[1] != "number") {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, "invalid ordinals query"})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: errors.New("invalid ordinals query")})
return
}
ocontent, otype, oerr := getInscription(temp[2])
if oerr != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, oerr.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: oerr})
return
}
if otype != "" {
w.Header().Set("Content-Type", otype)
}
_, e := w.Write(ocontent)
if e != nil {
respondWithErrorPage(w, &web3protocol.ErrorWithHttpCode{http.StatusBadRequest, e.Error()})
respondWithErrorPage(w, &web3protocol.Web3ProtocolError{HttpCode: http.StatusServiceUnavailable, Err: e})
return
}

Expand Down
6 changes: 6 additions & 0 deletions config.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ immutableUrlRegexps = []
[chainConfigs.1]
"ChainID" = 1
"RPC" = "https://mainnet.infura.io/v3/************"
# The maximum number of concurrent requests to the RPC. Default is 5, tuning this may
# help to prevent too much 429 Too Many Connection errors.
"RPCMaxConcurrentRequests" = 5
# System RPC is the RPC used by system workers (such as ERC-7774 cache event tracking)
# It should be different of the main RPC. If empty, will use the main RPC.
"SystemRPC" = "https://another.rpc"
[chainConfigs.1.NSConfig."eth"]
"NSType" = "ens"
"NSAddr" = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ethereum/go-ethereum v1.12.2
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/web3-protocol/web3protocol-go v0.2.11
github.com/web3-protocol/web3protocol-go v0.2.13
golang.org/x/net v0.16.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ github.com/web3-protocol/web3protocol-go v0.2.9 h1:2PrXI9cbDhzyEibU2+nR7mNUpF66t
github.com/web3-protocol/web3protocol-go v0.2.9/go.mod h1:IAGxUc/5IoLANkn/L+Xx4+B98V52j/hLCFkPeN5aCw0=
github.com/web3-protocol/web3protocol-go v0.2.11 h1:6SpcSBbxoxgljnpO/AKO3lUOV/rY60TyCw1SRMtHfEM=
github.com/web3-protocol/web3protocol-go v0.2.11/go.mod h1:IAGxUc/5IoLANkn/L+Xx4+B98V52j/hLCFkPeN5aCw0=
github.com/web3-protocol/web3protocol-go v0.2.12 h1:jb5kTHsampJ7bEimav2RLBdmucsMdtPyb8Rf0mfU2io=
github.com/web3-protocol/web3protocol-go v0.2.12/go.mod h1:IAGxUc/5IoLANkn/L+Xx4+B98V52j/hLCFkPeN5aCw0=
github.com/web3-protocol/web3protocol-go v0.2.13 h1:Xsw0KcgUGLua4jUsuz0bcqb5ZxyAJqgv1X78E0mOZj0=
github.com/web3-protocol/web3protocol-go v0.2.13/go.mod h1:IAGxUc/5IoLANkn/L+Xx4+B98V52j/hLCFkPeN5aCw0=
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down