Skip to content

Commit

Permalink
services/jsonrpc: compress middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Nov 25, 2024
1 parent 6804aa5 commit a5ca11b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cometbft/cometbft v0.38.12
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/ethereum/go-ethereum v1.14.8
github.com/go-chi/chi/v5 v5.1.0
github.com/jackc/pglogrepl v0.0.0-20240307033717-828fbfe908e9
github.com/jackc/pgx/v5 v5.6.0
github.com/jpillora/backoff v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps=
github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4=
Expand Down
7 changes: 7 additions & 0 deletions internal/services/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"syscall"
"time"

"github.com/go-chi/chi/v5/middleware"
"github.com/prometheus/client_golang/prometheus"

"github.com/kwilteam/kwil-db/core/log"
Expand Down Expand Up @@ -286,6 +287,9 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) {
if cfg.enableCORS {
h = corsHandler(h)
}

compMW := middleware.Compress(5)
h = compMW(h)
h = reqCounter(h, metrics[reqCounterName])
h = realIPHandler(h, cfg.proxyCount) // for effective rate limiting
h = recoverer(h, log) // first, wrap with defer and call next ^
Expand All @@ -305,6 +309,7 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) {
w.Header().Set("content-type", "application/json; charset=utf-8")
http.ServeContent(w, r, "openrpc.json", time.Time{}, bytes.NewReader(s.spec))
})
h = compMW(h)
if cfg.enableCORS {
specHandler = corsHandler(specHandler)
}
Expand All @@ -317,6 +322,7 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) {
if cfg.enableCORS {
healthHandler = corsHandler(healthHandler)
}
h = compMW(h)
healthHandler = recoverer(healthHandler, log)
mux.Handle(pathHealthV1, healthHandler)

Expand All @@ -326,6 +332,7 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) {
if cfg.enableCORS {
userHealthHandler = corsHandler(userHealthHandler)
}
h = compMW(h)
userHealthHandler = recoverer(userHealthHandler, log)
mux.Handle(pathSvcHealthV1, userHealthHandler)

Expand Down

0 comments on commit a5ca11b

Please sign in to comment.