Commit a46f5f2d authored by Matthew Slipper's avatar Matthew Slipper Committed by GitHub

Merge pull request #1912 from mslipper/bugfix/proxyd-fixes

go/proxyd: Various fixes
parents a91726d3 4b56ed84
---
'@eth-optimism/proxyd': minor
---
Various proxyd fixes
...@@ -389,7 +389,7 @@ func (b *BackendGroup) Forward(ctx context.Context, rpcReq *RPCReq) (*RPCRes, er ...@@ -389,7 +389,7 @@ func (b *BackendGroup) Forward(ctx context.Context, rpcReq *RPCReq) (*RPCRes, er
if err != nil { if err != nil {
log.Error( log.Error(
"error forwarding request to backend", "error forwarding request to backend",
"name", b.Name, "name", back.Name,
"req_id", GetReqID(ctx), "req_id", GetReqID(ctx),
"auth", GetAuthCtx(ctx), "auth", GetAuthCtx(ctx),
"err", err, "err", err,
...@@ -442,7 +442,7 @@ func (b *BackendGroup) ProxyWS(ctx context.Context, clientConn *websocket.Conn, ...@@ -442,7 +442,7 @@ func (b *BackendGroup) ProxyWS(ctx context.Context, clientConn *websocket.Conn,
func calcBackoff(i int) time.Duration { func calcBackoff(i int) time.Duration {
jitter := float64(rand.Int63n(250)) jitter := float64(rand.Int63n(250))
ms := math.Min(math.Pow(2, float64(i))*1000+jitter, 10000) ms := math.Min(math.Pow(2, float64(i))*1000+jitter, 3000)
return time.Duration(ms) * time.Millisecond return time.Duration(ms) * time.Millisecond
} }
......
...@@ -27,7 +27,7 @@ type MetricsConfig struct { ...@@ -27,7 +27,7 @@ type MetricsConfig struct {
type BackendOptions struct { type BackendOptions struct {
ResponseTimeoutSeconds int `toml:"response_timeout_seconds"` ResponseTimeoutSeconds int `toml:"response_timeout_seconds"`
MaxResponseSizeBytes int64 `toml:"max_response_size_bytes"` MaxResponseSizeBytes int64 `toml:"max_response_size_bytes"`
MaxRetries int `toml:"backend_retries"` MaxRetries int `toml:"max_retries"`
OutOfServiceSeconds int `toml:"out_of_service_seconds"` OutOfServiceSeconds int `toml:"out_of_service_seconds"`
} }
......
...@@ -106,12 +106,6 @@ var ( ...@@ -106,12 +106,6 @@ var (
"request_source", "request_source",
}) })
httpRequestsTotal = promauto.NewCounter(prometheus.CounterOpts{
Namespace: MetricsNamespace,
Name: "http_requests_total",
Help: "Count of total HTTP requests.",
})
httpResponseCodesTotal = promauto.NewCounterVec(prometheus.CounterOpts{ httpResponseCodesTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: MetricsNamespace, Namespace: MetricsNamespace,
Name: "http_response_codes_total", Name: "http_response_codes_total",
......
...@@ -117,7 +117,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) { ...@@ -117,7 +117,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Info("rejected request with bad rpc request", "source", "rpc", "err", err) log.Info("rejected request with bad rpc request", "source", "rpc", "err", err)
RecordRPCError(ctx, BackendProxyd, MethodUnknown, err) RecordRPCError(ctx, BackendProxyd, MethodUnknown, err)
writeRPCError(w, nil, err) writeRPCError(ctx, w, nil, err)
return return
} }
...@@ -132,7 +132,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) { ...@@ -132,7 +132,7 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
"method", req.Method, "method", req.Method,
) )
RecordRPCError(ctx, BackendProxyd, MethodUnknown, ErrMethodNotWhitelisted) RecordRPCError(ctx, BackendProxyd, MethodUnknown, ErrMethodNotWhitelisted)
writeRPCError(w, req.ID, ErrMethodNotWhitelisted) writeRPCError(ctx, w, req.ID, ErrMethodNotWhitelisted)
return return
} }
...@@ -144,21 +144,11 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) { ...@@ -144,21 +144,11 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
"req_id", GetReqID(ctx), "req_id", GetReqID(ctx),
"err", err, "err", err,
) )
writeRPCError(w, req.ID, err) writeRPCError(ctx, w, req.ID, err)
return return
} }
enc := json.NewEncoder(w) writeRPCRes(ctx, w, backendRes)
if err := enc.Encode(backendRes); err != nil {
log.Error(
"error encoding response",
"req_id", GetReqID(ctx),
"err", err,
)
RecordRPCError(ctx, BackendProxyd, req.Method, err)
writeRPCError(w, req.ID, err)
return
}
} }
func (s *Server) HandleWS(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleWS(w http.ResponseWriter, r *http.Request) {
...@@ -232,20 +222,17 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context ...@@ -232,20 +222,17 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
) )
} }
func writeRPCError(w http.ResponseWriter, id json.RawMessage, err error) { func writeRPCError(ctx context.Context, w http.ResponseWriter, id json.RawMessage, err error) {
var res *RPCRes var res *RPCRes
if r, ok := err.(*RPCErr); ok { if r, ok := err.(*RPCErr); ok {
res = NewRPCErrorRes(id, r) res = NewRPCErrorRes(id, r)
} else { } else {
res = NewRPCErrorRes(id, &RPCErr{ res = NewRPCErrorRes(id, ErrInternal)
Code: JSONRPCErrorInternal,
Message: "internal error",
})
} }
writeRPCRes(w, res) writeRPCRes(ctx, w, res)
} }
func writeRPCRes(w http.ResponseWriter, res *RPCRes) { func writeRPCRes(ctx context.Context, w http.ResponseWriter, res *RPCRes) {
statusCode := 200 statusCode := 200
if res.IsError() && res.Error.HTTPErrorCode != 0 { if res.IsError() && res.Error.HTTPErrorCode != 0 {
statusCode = res.Error.HTTPErrorCode statusCode = res.Error.HTTPErrorCode
...@@ -254,13 +241,14 @@ func writeRPCRes(w http.ResponseWriter, res *RPCRes) { ...@@ -254,13 +241,14 @@ func writeRPCRes(w http.ResponseWriter, res *RPCRes) {
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
if err := enc.Encode(res); err != nil { if err := enc.Encode(res); err != nil {
log.Error("error writing rpc response", "err", err) log.Error("error writing rpc response", "err", err)
RecordRPCError(ctx, BackendProxyd, MethodUnknown, err)
return
} }
httpResponseCodesTotal.WithLabelValues(strconv.Itoa(statusCode)).Inc() httpResponseCodesTotal.WithLabelValues(strconv.Itoa(statusCode)).Inc()
} }
func instrumentedHdlr(h http.Handler) http.HandlerFunc { func instrumentedHdlr(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
httpRequestsTotal.Inc()
respTimer := prometheus.NewTimer(httpRequestDurationSumm) respTimer := prometheus.NewTimer(httpRequestDurationSumm)
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
respTimer.ObserveDuration() respTimer.ObserveDuration()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment