diff --git a/check.go b/check.go index a11ac16..15a8788 100644 --- a/check.go +++ b/check.go @@ -12,6 +12,7 @@ import ( "sync/atomic" "time" + "github.com/klauspost/compress/gzhttp" "go.goblog.app/app/pkgs/bufferpool" "golang.org/x/sync/singleflight" ) @@ -49,14 +50,14 @@ func (a *goBlog) checkLinks(w io.Writer, posts ...*post) error { // Create HTTP client client := &http.Client{ Timeout: 30 * time.Second, - Transport: &http.Transport{ + Transport: gzhttp.Transport(&http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, // Limits DisableKeepAlives: true, MaxConnsPerHost: 1, - }, + }), } // Process all links var wg sync.WaitGroup diff --git a/go.mod b/go.mod index 713304a..78f5177 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( github.com/jlelse/feeds v1.2.1-0.20210704161900-189f94254ad4 github.com/justinas/alice v1.2.0 github.com/kaorimatz/go-opml v0.0.0-20210201121027-bc8e2852d7f9 + github.com/klauspost/compress v1.14.2 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lopezator/migrator v0.3.0 github.com/mattn/go-sqlite3 v1.14.11 @@ -95,7 +96,6 @@ require ( github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect github.com/jsimonetti/rtnetlink v0.0.0-20211203074127-fd9a11f42291 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/compress v1.13.6 // indirect github.com/lestrrat-go/strftime v1.0.5 // indirect github.com/magiconair/properties v1.8.5 // indirect github.com/mdlayher/netlink v1.4.2 // indirect diff --git a/go.sum b/go.sum index 82c801b..aae48a2 100644 --- a/go.sum +++ b/go.sum @@ -275,8 +275,8 @@ github.com/kaorimatz/go-opml v0.0.0-20210201121027-bc8e2852d7f9 h1:+9REu9CK9D1AQ github.com/kaorimatz/go-opml v0.0.0-20210201121027-bc8e2852d7f9/go.mod h1:OvY5ZBrAC9kOvM2PZs9Lw0BH+5K7tjrT6T7SFhn27OA= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi28pRw= +github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= diff --git a/http.go b/http.go index 024fe0c..0a8d05b 100644 --- a/http.go +++ b/http.go @@ -1,7 +1,6 @@ package main import ( - "compress/flate" "database/sql" "errors" "fmt" @@ -15,6 +14,8 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/justinas/alice" + "github.com/klauspost/compress/gzhttp" + "github.com/klauspost/compress/gzip" "go.goblog.app/app/pkgs/maprouter" "golang.org/x/net/context" ) @@ -40,8 +41,11 @@ func (a *goBlog) startServer() (err error) { if a.cfg.Server.Logging { h = h.Append(a.logMiddleware) } - compressor := middleware.NewCompressor(flate.BestCompression) - h = h.Append(middleware.Recoverer, compressor.Handler, middleware.Heartbeat("/ping")) + compressor, err := gzhttp.NewWrapper(gzhttp.CompressionLevel(gzip.BestCompression)) + if err != nil { + return err + } + h = h.Append(middleware.Recoverer, func(next http.Handler) http.Handler { return compressor(next) }, middleware.Heartbeat("/ping")) if a.httpsConfigured(false) { h = h.Append(a.securityHeaders) } diff --git a/httpClient.go b/httpClient.go index 1dea369..2664b54 100644 --- a/httpClient.go +++ b/httpClient.go @@ -3,13 +3,15 @@ package main import ( "net/http" "time" + + "github.com/klauspost/compress/gzhttp" ) func newHttpClient() *http.Client { return &http.Client{ - Timeout: 5 * time.Minute, - Transport: &http.Transport{ + Timeout: time.Minute, + Transport: gzhttp.Transport(&http.Transport{ DisableKeepAlives: true, - }, + }), } } diff --git a/indieAuth.go b/indieAuth.go index a97f46b..d099cb6 100644 --- a/indieAuth.go +++ b/indieAuth.go @@ -4,7 +4,6 @@ import ( "context" "net/http" "strings" - "time" "github.com/hacdias/indieauth" ) @@ -14,12 +13,7 @@ const indieAuthScope contextKey = "scope" func (a *goBlog) initIndieAuth() { a.ias = indieauth.NewServer( false, - &http.Client{ - Timeout: 30 * time.Second, - Transport: &http.Transport{ - DisableKeepAlives: true, - }, - }, + a.httpClient, ) }