Skip to content

Commit

Permalink
serve assets gzipped #342 (#381)
Browse files Browse the repository at this point in the history
* serve assets gzipped #342

* Extract method getHandler to make asset compression testable

* Tests: Asset compression
  • Loading branch information
gawicks authored Sep 8, 2021
1 parent 38d6714 commit 0039ec5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ require (
github.com/jsonnet-bundler/jsonnet-bundler v0.4.0
github.com/kardianos/service v1.2.0
github.com/kisielk/godepgraph v0.0.0-20190626013829-57a7e4a651a9
github.com/klauspost/compress v1.13.5
github.com/kr/pretty v0.2.0 // indirect
github.com/kyoh86/richgo v0.3.3
github.com/kyoh86/xdg v1.2.0 // indirect
github.com/markbates/pkger v0.17.1
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/mattn/goreman v0.3.5
github.com/mgechev/revive v1.0.3
Expand All @@ -40,7 +40,6 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/onsi/ginkgo v1.16.2
github.com/onsi/gomega v1.12.0
github.com/peterbourgon/ff/v3 v3.0.0
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0
github.com/pyroscope-io/dotnetdiag v1.2.1
Expand Down
11 changes: 3 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ github.com/deadcheat/gonch v0.0.0-20180528124129-c2ff7a019863/go.mod h1:/5mH3gAu
github.com/dgraph-io/badger/v2 v2.2007.2 h1:EjjK0KqwaFMlPin1ajhP943VPENHJdEz1KLIegjaI3k=
github.com/dgraph-io/badger/v2 v2.2007.2/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
Expand Down Expand Up @@ -165,8 +165,6 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -306,6 +304,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/godepgraph v0.0.0-20190626013829-57a7e4a651a9 h1:ZkWH0x1yafBo+Y2WdGGdszlJrMreMXWl7/dqpEkwsIk=
github.com/kisielk/godepgraph v0.0.0-20190626013829-57a7e4a651a9/go.mod h1:Gb5YEgxqiSSVrXKWQxDcKoCM94NO5QAwOwTaVmIUAMI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.13.5 h1:9O69jUPDcsT9fEm74W92rZL9FQY7rCdaXVneq+yyzl4=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand All @@ -327,8 +327,6 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down Expand Up @@ -395,8 +393,6 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/peterbourgon/ff/v3 v3.0.0 h1:eQzEmNahuOjQXfuegsKQTSTDbf4dNvr/eNLrmJhiH7M=
github.com/peterbourgon/ff/v3 v3.0.0/go.mod h1:UILIFjRH5a/ar8TjXYLTkIvSvekZqPm5Eb/qbGk6CT0=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -916,7 +912,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
25 changes: 20 additions & 5 deletions pkg/server/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"compress/gzip"
"context"
"encoding/json"
"errors"
Expand All @@ -13,6 +14,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/klauspost/compress/gzhttp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
Expand All @@ -28,9 +30,10 @@ import (
)

const (
jwtCookieName = "pyroscopeJWT"
stateCookieName = "pyroscopeState"
oauthGoogle = iota
jwtCookieName = "pyroscopeJWT"
stateCookieName = "pyroscopeState"
gzHttpCompressionThreshold = 2000
oauthGoogle = iota
oauthGithub
oauthGitlab
)
Expand Down Expand Up @@ -191,12 +194,24 @@ func (ctrl *Controller) getAuthRoutes() ([]route, error) {
return authRoutes, nil
}

func (ctrl *Controller) getHandler() (http.Handler, error) {
handler, err := ctrl.mux()
if err != nil {
return nil, err
}

gzhttpMiddleware, err := gzhttp.NewWrapper(gzhttp.MinSize(gzHttpCompressionThreshold), gzhttp.CompressionLevel(gzip.BestSpeed))
if err != nil {
return nil, err
}
return gzhttpMiddleware(handler), nil
}

func (ctrl *Controller) Start() error {
logger := logrus.New()
w := logger.Writer()
defer w.Close()

handler, err := ctrl.mux()
handler, err := ctrl.getHandler()
if err != nil {
return err
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/server/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package server

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"

"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/storage"
"github.com/pyroscope-io/pyroscope/pkg/testing"
)

const assetAtCompressionThreshold, assetLtCompressionThreshold = "AssetAtCompressionThreshold", "AssetLTCompressionThreshold"

var _ = Describe("server", func() {
testing.WithConfig(func(cfg **config.Config) {
var temp_asset_dir *testing.TmpDirectory
BeforeSuite(func() {
temp_asset_dir = testing.TmpDirSync()
ioutil.WriteFile(filepath.Join(temp_asset_dir.Path, assetLtCompressionThreshold), make([]byte, gzHttpCompressionThreshold-1), 0644)
ioutil.WriteFile(filepath.Join(temp_asset_dir.Path, assetAtCompressionThreshold), make([]byte, gzHttpCompressionThreshold), 0644)
})
AfterSuite(func() {
temp_asset_dir.Close()
})
DescribeTable("compress assets",
func(filename string, uncompressed bool) {
done := make(chan interface{})
go func(filename string, uncompressed bool) {
defer GinkgoRecover()

(*cfg).Server.APIBindAddr = ":10045"
s, err := storage.New(&(*cfg).Server, prometheus.NewRegistry())
Expect(err).ToNot(HaveOccurred())
c, _ := New(&(*cfg).Server, s, s, logrus.New(), prometheus.NewRegistry())
c.dir = http.Dir(temp_asset_dir.Path)
h, _ := c.getHandler()
httpServer := httptest.NewServer(h)
defer httpServer.Close()

res, err := http.Get(fmt.Sprintf("%s/%s", httpServer.URL, filename))
Expect(err).ToNot(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))
Expect(res.Uncompressed).To(Equal(uncompressed))

close(done)
}(filename, uncompressed)
Eventually(done, 2).Should(BeClosed())
},
Entry("Should compress assets greater than or equal to threshold", assetAtCompressionThreshold, true),
Entry("Should not compress assets less than threshold", assetLtCompressionThreshold, false),
)
})
})

0 comments on commit 0039ec5

Please sign in to comment.