mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-04 04:04:00 +00:00
https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
234 lines
6.6 KiB
Go
234 lines
6.6 KiB
Go
// Commons for HTTP handling
|
|
package rpcserver
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"runtime/debug"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/net/netutil"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
types "github.com/tendermint/tendermint/rpc/lib/types"
|
|
)
|
|
|
|
// Config is a RPC server configuration.
|
|
type Config struct {
|
|
// see netutil.LimitListener
|
|
MaxOpenConnections int
|
|
// mirrors http.Server#ReadTimeout
|
|
ReadTimeout time.Duration
|
|
// mirrors http.Server#WriteTimeout
|
|
WriteTimeout time.Duration
|
|
// MaxBodyBytes controls the maximum number of bytes the
|
|
// server will read parsing the request body.
|
|
MaxBodyBytes int64
|
|
// mirrors http.Server#MaxHeaderBytes
|
|
MaxHeaderBytes int
|
|
}
|
|
|
|
// DefaultConfig returns a default configuration.
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
MaxOpenConnections: 0, // unlimited
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxBodyBytes: int64(1000000), // 1MB
|
|
MaxHeaderBytes: 1 << 20, // same as the net/http default
|
|
}
|
|
}
|
|
|
|
// StartHTTPServer takes a listener and starts an HTTP server with the given handler.
|
|
// It wraps handler with RecoverAndLogHandler.
|
|
// NOTE: This function blocks - you may want to call it in a go-routine.
|
|
func StartHTTPServer(listener net.Listener, handler http.Handler, logger log.Logger, config *Config) error {
|
|
logger.Info(fmt.Sprintf("Starting RPC HTTP server on %s", listener.Addr()))
|
|
s := &http.Server{
|
|
Handler: RecoverAndLogHandler(maxBytesHandler{h: handler, n: config.MaxBodyBytes}, logger),
|
|
ReadTimeout: config.ReadTimeout,
|
|
WriteTimeout: config.WriteTimeout,
|
|
MaxHeaderBytes: config.MaxHeaderBytes,
|
|
}
|
|
err := s.Serve(listener)
|
|
logger.Info("RPC HTTP server stopped", "err", err)
|
|
return err
|
|
}
|
|
|
|
// StartHTTPAndTLSServer takes a listener and starts an HTTPS server with the given handler.
|
|
// It wraps handler with RecoverAndLogHandler.
|
|
// NOTE: This function blocks - you may want to call it in a go-routine.
|
|
func StartHTTPAndTLSServer(
|
|
listener net.Listener,
|
|
handler http.Handler,
|
|
certFile, keyFile string,
|
|
logger log.Logger,
|
|
config *Config,
|
|
) error {
|
|
logger.Info(fmt.Sprintf("Starting RPC HTTPS server on %s (cert: %q, key: %q)",
|
|
listener.Addr(), certFile, keyFile))
|
|
s := &http.Server{
|
|
Handler: RecoverAndLogHandler(maxBytesHandler{h: handler, n: config.MaxBodyBytes}, logger),
|
|
ReadTimeout: config.ReadTimeout,
|
|
WriteTimeout: config.WriteTimeout,
|
|
MaxHeaderBytes: config.MaxHeaderBytes,
|
|
}
|
|
err := s.ServeTLS(listener, certFile, keyFile)
|
|
|
|
logger.Error("RPC HTTPS server stopped", "err", err)
|
|
return err
|
|
}
|
|
|
|
func WriteRPCResponseHTTPError(
|
|
w http.ResponseWriter,
|
|
httpCode int,
|
|
res types.RPCResponse,
|
|
) {
|
|
jsonBytes, err := json.MarshalIndent(res, "", " ")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(httpCode)
|
|
if _, err := w.Write(jsonBytes); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse) {
|
|
jsonBytes, err := json.MarshalIndent(res, "", " ")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
if _, err := w.Write(jsonBytes); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// WriteRPCResponseArrayHTTP will do the same as WriteRPCResponseHTTP, except it
|
|
// can write arrays of responses for batched request/response interactions via
|
|
// the JSON RPC.
|
|
func WriteRPCResponseArrayHTTP(w http.ResponseWriter, res []types.RPCResponse) {
|
|
if len(res) == 1 {
|
|
WriteRPCResponseHTTP(w, res[0])
|
|
} else {
|
|
jsonBytes, err := json.MarshalIndent(res, "", " ")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
if _, err := w.Write(jsonBytes); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// RecoverAndLogHandler wraps an HTTP handler, adding error logging.
|
|
// If the inner function panics, the outer function recovers, logs, sends an
|
|
// HTTP 500 error response.
|
|
func RecoverAndLogHandler(handler http.Handler, logger log.Logger) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Wrap the ResponseWriter to remember the status
|
|
rww := &ResponseWriterWrapper{-1, w}
|
|
begin := time.Now()
|
|
|
|
rww.Header().Set("X-Server-Time", fmt.Sprintf("%v", begin.Unix()))
|
|
|
|
defer func() {
|
|
// Send a 500 error if a panic happens during a handler.
|
|
// Without this, Chrome & Firefox were retrying aborted ajax requests,
|
|
// at least to my localhost.
|
|
if e := recover(); e != nil {
|
|
|
|
// If RPCResponse
|
|
if res, ok := e.(types.RPCResponse); ok {
|
|
WriteRPCResponseHTTP(rww, res)
|
|
} else {
|
|
// For the rest,
|
|
logger.Error(
|
|
"Panic in RPC HTTP handler", "err", e, "stack",
|
|
string(debug.Stack()),
|
|
)
|
|
WriteRPCResponseHTTPError(
|
|
rww,
|
|
http.StatusInternalServerError,
|
|
types.RPCInternalError(types.JSONRPCIntID(-1), e.(error)),
|
|
)
|
|
}
|
|
}
|
|
|
|
// Finally, log.
|
|
durationMS := time.Since(begin).Nanoseconds() / 1000000
|
|
if rww.Status == -1 {
|
|
rww.Status = 200
|
|
}
|
|
logger.Info("Served RPC HTTP response",
|
|
"method", r.Method, "url", r.URL,
|
|
"status", rww.Status, "duration", durationMS,
|
|
"remoteAddr", r.RemoteAddr,
|
|
)
|
|
}()
|
|
|
|
handler.ServeHTTP(rww, r)
|
|
})
|
|
}
|
|
|
|
// Remember the status for logging
|
|
type ResponseWriterWrapper struct {
|
|
Status int
|
|
http.ResponseWriter
|
|
}
|
|
|
|
func (w *ResponseWriterWrapper) WriteHeader(status int) {
|
|
w.Status = status
|
|
w.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
// implements http.Hijacker
|
|
func (w *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
return w.ResponseWriter.(http.Hijacker).Hijack()
|
|
}
|
|
|
|
type maxBytesHandler struct {
|
|
h http.Handler
|
|
n int64
|
|
}
|
|
|
|
func (h maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, h.n)
|
|
h.h.ServeHTTP(w, r)
|
|
}
|
|
|
|
// Listen starts a new net.Listener on the given address.
|
|
// It returns an error if the address is invalid or the call to Listen() fails.
|
|
func Listen(addr string, config *Config) (listener net.Listener, err error) {
|
|
parts := strings.SplitN(addr, "://", 2)
|
|
if len(parts) != 2 {
|
|
return nil, errors.Errorf(
|
|
"invalid listening address %s (use fully formed addresses, including the tcp:// or unix:// prefix)",
|
|
addr,
|
|
)
|
|
}
|
|
proto, addr := parts[0], parts[1]
|
|
listener, err = net.Listen(proto, addr)
|
|
if err != nil {
|
|
return nil, errors.Errorf("failed to listen on %v: %v", addr, err)
|
|
}
|
|
if config.MaxOpenConnections > 0 {
|
|
listener = netutil.LimitListener(listener, config.MaxOpenConnections)
|
|
}
|
|
|
|
return listener, nil
|
|
}
|