Skip to content

Commit

Permalink
增加压缩策略
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 24, 2020
1 parent 00046e5 commit 08bddea
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
[![codecov](https://codecov.io/gh/antlabs/gout-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/antlabs/gout)

gout请求和响应中间件项目

### 请求body使用gzip压缩
## 请求中间件
### gzip
#### 请求body使用gzip压缩
```go
import (
"github.com/antlabs/gout-middleware/request"
Expand All @@ -19,3 +20,18 @@ func main() {
}

```
#### 设置请求body大于一定字节数才压缩
```go
import (
"github.com/antlabs/gout-middleware/request"
"github.com/guonaihong/gout"
)

func main() {
gout.POST(":6666/compress").
RequestUse(request.GzipCompress(request.EnableGzipGreaterEqual(4))). //大于等于4个字节才压缩
SetBody("hello world").
Do()
}

```
32 changes: 29 additions & 3 deletions request/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@ import (
"net/http"
)

type gzipCompress struct{}
// 大于或等于EnableGzipMore 字节数的才压缩
type EnableGzipGreaterEqual int

type gzipCompress struct {
enableGzipGreaterEqual int
}

func (g *gzipCompress) ModifyRequest(req *http.Request) error {
// 如果已经有一种编码格式,不会生效
if len(req.Header.Get("Content-Encoding")) > 0 {
return nil
}

if req.ContentLength == 0 {
return nil
}

if g.enableGzipGreaterEqual > 0 {
if req.ContentLength < int64(g.enableGzipGreaterEqual) {
return nil
}
}

buf := &bytes.Buffer{}

w := gzip.NewWriter(buf)
Expand All @@ -36,6 +51,17 @@ func (g *gzipCompress) ModifyRequest(req *http.Request) error {
req.Header.Set("Content-Encoding", "gzip")
return nil
}
func GzipCompress() api.RequestMiddler {
return &gzipCompress{}

func GzipCompress(args ...interface{}) api.RequestMiddler {

compress := &gzipCompress{}

for _, a := range args {
switch a.(type) {
case EnableGzipGreaterEqual:
compress.enableGzipGreaterEqual = int(a.(EnableGzipGreaterEqual))
}
}

return compress
}
24 changes: 23 additions & 1 deletion request/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ func createDeCompressServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(r.ServeHTTP))
}

// 测试大于等于某个值才压缩
func TestGzipCompress_GreaterEqual(t *testing.T) {
// 测试大于testGzipData个字节才压缩, 服务端不压缩,返回echo的数据应该是不压缩的
got := ""
ts := createNotDeCompressServer()
gout.POST(ts.URL).RequestUse(GzipCompress(EnableGzipGreaterEqual(len(testGzipData) + 1))).SetBody(testGzipData).BindBody(&got).Do()

assert.Equal(t, got, testGzipData)

// 测试小于testGzipData个字节才压缩, 服务端解压缩,echo的数据是一样的
ts = createNotDeCompressServer()
gout.POST(ts.URL).RequestUse(GzipCompress(EnableGzipGreaterEqual(len(testGzipData) - 1))).SetBody(testGzipData).BindBody(&got).Do()

buf := &bytes.Buffer{}

w := stdgzip.NewWriter(buf)
w.Write([]byte(testGzipData))
w.Close()

assert.Equal(t, got, buf.String())
}

func TestGzipCompress(t *testing.T) {
// 客户端压缩 + 服务不解压缩
got := []byte{}
Expand All @@ -56,8 +78,8 @@ func TestGzipCompress(t *testing.T) {
assert.Equal(t, got, buf.Bytes())

// 客户端压缩 + 服务解压缩
got = []byte{}
ts = createDeCompressServer()
got = []byte{}
gout.POST(ts.URL).RequestUse(GzipCompress()).SetBody(testGzipData).BindBody(&got).Do()

buf = &bytes.Buffer{}
Expand Down

0 comments on commit 08bddea

Please sign in to comment.