Skip to content

Commit

Permalink
rpc,config: disable RPC response compression by default, add setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Feb 14, 2025
1 parent 980cd75 commit 3abc3ca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/node/prof.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func startProfilers(mode profMode, pprofFile string) (func(), error) {
// handler with the root path on the default mux.
http.Handle("/", http.RedirectHandler("/debug/pprof/", http.StatusSeeOther))
go func() {
fmt.Println("starting http profiler on localhost:6060")
if err := http.ListenAndServe("localhost:6060", nil); err != nil {
fmt.Printf("http.ListenAndServe: %v\n", err)
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ type RPCConfig struct {
Timeout types.Duration `toml:"timeout" comment:"user request duration limit after which it is cancelled"`
MaxReqSize int `toml:"max_req_size" comment:"largest permissible user request size"`
Private bool `toml:"private" comment:"enable private mode that requires challenge authentication for each call"`
Compression bool `toml:"compression" comment:"use compression in RPC responses"`
ChallengeExpiry types.Duration `toml:"challenge_expiry" comment:"lifetime of a server-generated challenge"`
ChallengeRateLimit float64 `toml:"challenge_rate_limit" comment:"maximum number of challenges per second that a user can request"`
}
Expand Down
6 changes: 6 additions & 0 deletions core/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ func formatArgs(args ...any) string {
var _ KVLogger = (*plainLogger)(nil)

func (l *plainLogger) Debug(msg string, args ...any) {
// Avoid malloc and memmove with formatArgs and string concatenation. Since
// Debug is typically used liberally (it would be display very often) on
// frequently used paths, we want to avoid this unless the level is low enough.
if l.log.Level() > sublog.LevelDebug {
return
}
// args are pairs of key-values, so we will print them in pairs after the message.
msg += formatArgs(args...)
l.log.Debugf(msg)
Expand Down
15 changes: 14 additions & 1 deletion node/services/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type serverConfig struct {
tlsConfig *tls.Config
timeout time.Duration
enableCORS bool
compress bool
specInfo *openrpc.Info
reqSzLimit int
proxyCount int
Expand Down Expand Up @@ -146,6 +147,15 @@ func WithCORS() Opt {
}
}

// WithCompression enables gzip compression of responses. The adds some
// computational overhead, but may be useful if there is no reverse proxy to
// offload this work.
func WithCompression() Opt {
return func(c *serverConfig) {
c.compress = true
}
}

// checkAddr cleans the address, and indicates if it is a unix socket (local
// filesystem path). The addr for NewServer should be a host:port style string,
// but if it is a URL, this will attempt to get the host and port from it.
Expand Down Expand Up @@ -292,7 +302,10 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) {
h = corsHandler(h)
}

compMW := middleware.Compress(5)
compMW := func(h http.Handler) http.Handler { return h }
if cfg.compress {
compMW = middleware.Compress(5)
}
h = compMW(h)
h = reqCounter(h, metrics[reqCounterName])
h = realIPHandler(h, cfg.proxyCount) // for effective rate limiting
Expand Down

0 comments on commit 3abc3ca

Please sign in to comment.