diff --git a/cmd/server/helper.go b/cmd/server/helper.go index 4de690d..9cc88b3 100644 --- a/cmd/server/helper.go +++ b/cmd/server/helper.go @@ -50,6 +50,8 @@ type NameServiceInfo struct { type ChainConfig struct { ChainID int RPC string + RPCMaxConcurrentRequests int + SystemRPC string NSConfig map[string]NameServiceInfo } diff --git a/cmd/server/main.go b/cmd/server/main.go index adbcf7d..3a55ff1 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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{}, } diff --git a/cmd/server/protocol.go b/cmd/server/protocol.go index b3676c6..2e8b668 100644 --- a/cmd/server/protocol.go +++ b/cmd/server/protocol.go @@ -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 @@ -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 @@ -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 @@ -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 { @@ -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 } @@ -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 } } @@ -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 @@ -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) diff --git a/cmd/server/sub_protocols.go b/cmd/server/sub_protocols.go index 0b63fa9..0a58738 100644 --- a/cmd/server/sub_protocols.go +++ b/cmd/server/sub_protocols.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io" + "errors" "net/http" "strings" @@ -17,12 +18,12 @@ 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 != "" { @@ -30,7 +31,7 @@ func handleOrdinals(w http.ResponseWriter, req *http.Request, path string) { } _, 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 } diff --git a/config.toml.sample b/config.toml.sample index 6160c7d..90c4edd 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -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" diff --git a/go.mod b/go.mod index caf76a3..432faa0 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 8d69f43..995dfdd 100644 --- a/go.sum +++ b/go.sum @@ -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=