brotli

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2021 License: Apache-2.0 Imports: 9 Imported by: 1

README

Brotli Middleware for Gin

功能

针对http输出数据进行压缩

使用案例

默认
handler.Use(brotli.DefaultHandler().Gin)
自定义
 handler.Use(brotli.NewHandler(brotli.Config{
  CompressionLevel: brotli.DefaultCompression,
  MinContentLength: brotli.DefalutContentLen,
  RequestFilter: []brotli.RequestFilter{
   brotli.NewCommonRequestFilter(),
   brotli.NewRequestApiFilter([]string{
    "/blog/cache/http",
    "/blog/detail/index",
   }),
  },
  ResponseHeaderFilter: []brotli.ResponseHeaderFilter{
   brotli.DefaultContentTypeFilter(),
  },
 }).Gin)

测试

压测速率
bash-4.3# go test -v  -run="GinWithLevelsHandler"
=== RUN   TestGinWithLevelsHandler
=== RUN   TestGinWithLevelsHandler/level_0
=== RUN   TestGinWithLevelsHandler/level_1
=== RUN   TestGinWithLevelsHandler/level_2
=== RUN   TestGinWithLevelsHandler/level_3
=== RUN   TestGinWithLevelsHandler/level_4
=== RUN   TestGinWithLevelsHandler/level_5
=== RUN   TestGinWithLevelsHandler/level_6
=== RUN   TestGinWithLevelsHandler/level_7
=== RUN   TestGinWithLevelsHandler/level_8
=== RUN   TestGinWithLevelsHandler/level_9
=== RUN   TestGinWithLevelsHandler/level_10
=== RUN   TestGinWithLevelsHandler/level_11
--- PASS: TestGinWithLevelsHandler (0.06s)
    --- PASS: TestGinWithLevelsHandler/level_0 (0.00s)
        handler_test.go:215: level_0: compressed 4267 => 1456 ratio=>0.66
    --- PASS: TestGinWithLevelsHandler/level_1 (0.00s)
        handler_test.go:215: level_1: compressed 4267 => 1419 ratio=>0.67
    --- PASS: TestGinWithLevelsHandler/level_2 (0.00s)
        handler_test.go:215: level_2: compressed 4267 => 1410 ratio=>0.67
    --- PASS: TestGinWithLevelsHandler/level_3 (0.00s)
        handler_test.go:215: level_3: compressed 4267 => 1340 ratio=>0.69
    --- PASS: TestGinWithLevelsHandler/level_4 (0.00s)
        handler_test.go:215: level_4: compressed 4267 => 1257 ratio=>0.71
    --- PASS: TestGinWithLevelsHandler/level_5 (0.00s)
        handler_test.go:215: level_5: compressed 4267 => 1187 ratio=>0.72
    --- PASS: TestGinWithLevelsHandler/level_6 (0.01s)
        handler_test.go:215: level_6: compressed 4267 => 1187 ratio=>0.72
    --- PASS: TestGinWithLevelsHandler/level_7 (0.00s)
        handler_test.go:215: level_7: compressed 4267 => 1186 ratio=>0.72
    --- PASS: TestGinWithLevelsHandler/level_8 (0.01s)
        handler_test.go:215: level_8: compressed 4267 => 1186 ratio=>0.72
    --- PASS: TestGinWithLevelsHandler/level_9 (0.01s)
        handler_test.go:215: level_9: compressed 4267 => 1185 ratio=>0.72
    --- PASS: TestGinWithLevelsHandler/level_10 (0.01s)
        handler_test.go:215: level_10: compressed 4268 => 1049 ratio=>0.75
    --- PASS: TestGinWithLevelsHandler/level_11 (0.02s)
        handler_test.go:215: level_11: compressed 4268 => 1042 ratio=>0.76
PASS
ok      github.com/CodeLineage/brotli   0.076s
bash-4.3#
性能测试
bash-4.3# go test -benchmem -bench .
goos: linux
goarch: amd64
pkg: github.com/CodeLineage/brotli
BenchmarkGin_SmallPayload-2                              2917842               435 ns/op              96 B/op          3 allocs/op
BenchmarkGinWithDefaultHandler_SmallPayload-2            1271218               997 ns/op             128 B/op          4 allocs/op
BenchmarkGin_BigPayload-2                                2912677               424 ns/op              96 B/op          3 allocs/op
BenchmarkGinWithDefaultHandler_BigPayload-2                 3063            405511 ns/op             882 B/op          6 allocs/op
PASS
ok      github.com/CodeLineage/brotli   7.122s

依赖

github.com/andybalholm/brotli v1.0.1

参考

https://github.com/nanmu42/gzip

Documentation

Index

Constants

View Source
const (
	BestSpeed          = brotli.BestSpeed
	BestCompression    = brotli.BestCompression
	DefaultCompression = brotli.DefaultCompression
	DefalutContentLen  = 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonRequestFilter

type CommonRequestFilter struct{}

CommonRequestFilter judge via common easy criteria like http method, accept-encoding header, etc.

func NewCommonRequestFilter

func NewCommonRequestFilter() *CommonRequestFilter

NewCommonRequestFilter ...

func (*CommonRequestFilter) ShouldCompress

func (c *CommonRequestFilter) ShouldCompress(req *http.Request) bool

ShouldCompress implements RequestFilter interface

type Config

type Config struct {
	// 压缩等级
	CompressionLevel int
	// 响应内容长度
	MinContentLength int64
	// 根据请求校验是否过滤
	RequestFilter []RequestFilter
	// 根据响应校验是否过滤
	ResponseHeaderFilter []ResponseHeaderFilter
}

Config is used in Handler initialization

type ContentTypeFilter

type ContentTypeFilter struct {
	// contains filtered or unexported fields
}

ContentTypeFilter

func DefaultContentTypeFilter

func DefaultContentTypeFilter() *ContentTypeFilter

DefaultContentTypeFilter

func NewContentTypeFilter

func NewContentTypeFilter(types []string) *ContentTypeFilter

NewContentTypeFilter ...

func (*ContentTypeFilter) ShouldCompress

func (e *ContentTypeFilter) ShouldCompress(header http.Header) bool

ShouldCompress implements RequestFilter interface

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implement brotli compression for gin

func DefaultHandler

func DefaultHandler() *Handler

DefaultHandler 创建一个默认handler

func NewHandler

func NewHandler(config Config) *Handler

func (*Handler) Gin

func (h *Handler) Gin(ctx *gin.Context)

Gin implement gin's middleware

type RequestApiFilter

type RequestApiFilter struct {
	// contains filtered or unexported fields
}

func NewRequestApiFilter

func NewRequestApiFilter(path []string) *RequestApiFilter

func (*RequestApiFilter) ShouldCompress

func (r *RequestApiFilter) ShouldCompress(req *http.Request) bool

type RequestFilter

type RequestFilter interface {
	ShouldCompress(req *http.Request) bool
}

Request filter conditions

type ResponseHeaderFilter

type ResponseHeaderFilter interface {
	ShouldCompress(header http.Header) bool
}

Response filter conditions

type SkipCompressedFilter

type SkipCompressedFilter struct{}

SkipCompressedFilter judges whether content has been already compressed

func NewSkipCompressedFilter

func NewSkipCompressedFilter() *SkipCompressedFilter

NewSkipCompressedFilter ...

func (*SkipCompressedFilter) ShouldCompress

func (s *SkipCompressedFilter) ShouldCompress(header http.Header) bool

ShouldCompress implements ResponseHeaderFilter interface

Content-Encoding: https://tools.ietf.org/html/rfc2616#section-3.5

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL