compress

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: Apache-2.0 Imports: 11 Imported by: 2

README

elton-compress

Build Status

Compress middleware for elton, it support gzip and br compress function by default.

  • BrCompressor Brotli compression algorithm is better for http, most modern browser support it. Compress level is 1-11,default 0(6).
  • GzipComperssor Gzip compression algorithm is most commonly used for http, all modern browser support it. Compress level is 1-9, default 0(6).
  • SnappyCompressor Snappy compression algorithm is fast, but not aim for maximum compression. It's useful for Intranet. Not support compress level.
  • ZstdCompressor Zstandard is a real-time compression algorithm, providing high compression ratios. Compress level is 1-2, default 0(2).
  • S2Compressor S2 is a high performance replacement for Snappy. Compress level is 1-2, default 0(2).
  • Lz4Compressor LZ4 is lossless compression algorithm, providing compression speed > 500 MB/s per core, scalable with multi-cores CPU. Compress level higher is better, use 0 for fastest compression.
package main

import (
	"bytes"

	"github.com/vicanso/elton"
	compress "github.com/vicanso/elton-compress"
)

func main() {
	e := elton.New()

	e.Use(compress.NewDefault())

	e.GET("/", func(c *elton.Context) (err error) {
		b := new(bytes.Buffer)
		for i := 0; i < 1000; i++ {
			b.WriteString("hello world\n")
		}
		c.SetHeader(elton.HeaderContentType, "text/plain; charset=utf-8")
		c.BodyBuffer = b
		return
	})

	err := e.ListenAndServe(":3000")
	if err != nil {
		panic(err)
	}
}

Example

middleware example

jquery-3.4.1.min.js(88145 bytes)

compression size ratio level
gzip 30827 2.859 6
br 29897 2.948 6
snappy 47709 1.847 -
zstd 32816 2.686 2
s2 42750 2.061 2
lz4 39434 2.235 6

brotli

I use Pure Go Brotli instead of cbrotli, it is a little bit slow.

Test for encode html(113K) to br:

BenchmarkCBrotli-8    	     200	   9555795 ns/op
BenchmarkGoBrotli-8   	     100	  10703582 ns/op

Documentation

Index

Constants

View Source
const (
	// BrEncoding br encoding
	BrEncoding = "br"
)
View Source
const (
	// GzipEncoding gzip encoding
	GzipEncoding = "gzip"
)
View Source
const (
	// Lz4Encoding lz4 encoding
	Lz4Encoding = "lz4"
)
View Source
const (
	// S2Encoding s2 encoding
	S2Encoding = "s2"
)
View Source
const (
	// SnappyEncoding snappy encoding
	SnappyEncoding = "snz"
)
View Source
const (
	// ZstdEncoding zstd encoding
	ZstdEncoding = "zst"
)

Variables

This section is empty.

Functions

func AcceptEncoding

func AcceptEncoding(c *elton.Context, encoding string) (bool, string)

AcceptEncoding check request accept encoding

func New

func New(config Config) elton.Handler

New create a new compress middleware

func NewDefault

func NewDefault() elton.Handler

NewDefault create a default compress middleware, support gzip

func NewWithDefaultCompressor

func NewWithDefaultCompressor(config Config) elton.Handler

NewWithDefaultCompressor create compress middleware with default compressor

Types

type BrCompressor

type BrCompressor struct {
	Level int
}

BrCompressor brotli compress

func (*BrCompressor) Accept

func (b *BrCompressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept check accept econding

func (*BrCompressor) Compress

func (b *BrCompressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress brotli compress

func (*BrCompressor) Pipe

func (b *BrCompressor) Pipe(c *elton.Context) (err error)

Pipe brotli pipe

type Compressor

type Compressor interface {
	// Accept accept check function
	Accept(c *elton.Context) (acceptable bool, encoding string)
	// Compress compress function
	Compress([]byte) (*bytes.Buffer, error)
	// Pipe pipe function
	Pipe(*elton.Context) error
}

Compressor compressor interface

type Config

type Config struct {
	// MinLength min compress length
	MinLength int
	// Checker check the data is compressable
	Checker *regexp.Regexp
	// Compressors compressor list
	Compressors []Compressor
	// Skipper skipper function
	Skipper elton.Skipper
}

Config compress config

func (*Config) AddCompressor added in v0.1.1

func (conf *Config) AddCompressor(compressor Compressor)

AddCompressor add compressor

type GzipCompressor

type GzipCompressor struct {
	Level int
}

GzipCompressor gzip compress

func (*GzipCompressor) Accept

func (g *GzipCompressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept accept gzip encoding

func (*GzipCompressor) Compress

func (g *GzipCompressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress compress data by gzip

func (*GzipCompressor) Pipe

func (g *GzipCompressor) Pipe(c *elton.Context) (err error)

Pipe compress by pipe

type Lz4Compressor added in v0.1.4

type Lz4Compressor struct {
	Level int
}

Lz4Compressor lz4 compress

func (*Lz4Compressor) Accept added in v0.1.4

func (*Lz4Compressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept check accept encoding

func (*Lz4Compressor) Compress added in v0.1.4

func (l *Lz4Compressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress lz4 compress

func (*Lz4Compressor) Pipe added in v0.1.4

func (l *Lz4Compressor) Pipe(c *elton.Context) (err error)

Pipe lz4 pipe compress

type S2Compressor added in v0.1.4

type S2Compressor struct {
	Level int
}

S2Compressor s2 compressor

func (*S2Compressor) Accept added in v0.1.4

func (*S2Compressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept check accept encoding

func (*S2Compressor) Compress added in v0.1.4

func (s *S2Compressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress s2 compress

func (*S2Compressor) Pipe added in v0.1.4

func (s *S2Compressor) Pipe(c *elton.Context) (err error)

Pipe s2 pipe

type SnappyCompressor

type SnappyCompressor struct {
}

SnappyCompressor snappy compress

func (*SnappyCompressor) Accept

func (s *SnappyCompressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept check accept encoding

func (*SnappyCompressor) Compress

func (s *SnappyCompressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress snappy compress

func (*SnappyCompressor) Pipe

func (s *SnappyCompressor) Pipe(c *elton.Context) (err error)

Pipe snappy pipe

type ZstdCompressor added in v0.1.4

type ZstdCompressor struct {
	Level int
}

ZstdCompressor zstd compress

func (*ZstdCompressor) Accept added in v0.1.4

func (*ZstdCompressor) Accept(c *elton.Context) (acceptable bool, encoding string)

Accept check accept encoding

func (*ZstdCompressor) Compress added in v0.1.4

func (z *ZstdCompressor) Compress(buf []byte) (*bytes.Buffer, error)

Compress zstd compress

func (*ZstdCompressor) Pipe added in v0.1.4

func (z *ZstdCompressor) Pipe(c *elton.Context) (err error)

Pipe zstd pike

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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