Skip to content

Commit

Permalink
- simplify gzip coder middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Dec 14, 2020
1 parent a945625 commit 5be0bd9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/middleware/coder/gzip/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
}
defer client.Stop()

client.Handler.UseCoder(gzip.New())
client.Handler.UseCoder(gzip.New(1024))

req := ""
for i := 0; i < 2048; i++ {
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/coder/gzip/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func main() {
svr := arpc.NewServer()

svr.Handler.UseCoder(gzip.New())
svr.Handler.UseCoder(gzip.New(1024))

// register router
svr.Handler.Handle("/echo", func(ctx *arpc.Context) {
Expand Down
15 changes: 7 additions & 8 deletions extension/middleware/coder/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ func gzipUnCompress(data []byte) ([]byte, error) {
}

// Gzip represents a gzip coding middleware.
type Gzip struct {
critical int
}
type Gzip int

// Encode implements arpc MessageCoder.
func (c *Gzip) Encode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
if len(msg.Buffer) > c.critical && !msg.IsFlagBitSet(coder.FlagBitGZip) {
func (g *Gzip) Encode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
if len(msg.Buffer) > int(*g) && !msg.IsFlagBitSet(coder.FlagBitGZip) {
buf := gzipCompress(msg.Buffer[arpc.HeaderIndexReserved+1:])
total := len(buf) + arpc.HeaderIndexReserved + 1
if total < len(msg.Buffer) {
Expand All @@ -52,7 +50,7 @@ func (c *Gzip) Encode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
}

// Decode implements arpc MessageCoder.
func (c *Gzip) Decode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
func (g *Gzip) Decode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
if msg.IsFlagBitSet(coder.FlagBitGZip) {
buf, err := gzipUnCompress(msg.Buffer[arpc.HeaderIndexReserved+1:])
if err == nil {
Expand All @@ -65,6 +63,7 @@ func (c *Gzip) Decode(client *arpc.Client, msg *arpc.Message) *arpc.Message {
}

// New returns the gzip coding middleware.
func New() *Gzip {
return &Gzip{critical: 1024}
func New(n int) *Gzip {
var g = Gzip(n)
return &g
}

0 comments on commit 5be0bd9

Please sign in to comment.