Skip to content

Commit

Permalink
gzhttp: Use strings for randomJitter to skip a copy
Browse files Browse the repository at this point in the history
  • Loading branch information
greatroar committed Mar 4, 2023
1 parent d6408a8 commit de7a4b8
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions gzhttp/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gzhttp

import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
Expand Down Expand Up @@ -73,7 +72,7 @@ type GzipResponseWriter struct {
setContentType bool // Add content type, if missing and detected.
suffixETag string // Suffix to add to ETag header if response is compressed.
dropETag bool // Drop ETag header if response is compressed (supersedes suffixETag).
randomJitter []byte // Add random bytes to output as header field.
randomJitter string // Add random bytes to output as header field.
jitterBuffer int // Maximum buffer to accumulate before doing jitter.

contentTypeFilter func(ct string) bool // Only compress if the response is one of these content-types. All are accepted if empty.
Expand Down Expand Up @@ -211,7 +210,7 @@ func (w *GzipResponseWriter) startGzip(remain []byte) error {
// Initialize the GZIP response.
w.init()

// Set random jitter based on CRC of current buffer
// Set random jitter based on SHA-256 of current buffer
// Before first write.
if len(w.randomJitter) > 0 {
var jitRNG uint32
Expand All @@ -236,7 +235,7 @@ func (w *GzipResponseWriter) startGzip(remain []byte) error {
}
jitRNG = binary.LittleEndian.Uint32(tmp[:])
}
jit := string(w.randomJitter[:1+jitRNG%uint32(len(w.randomJitter)-1)])
jit := w.randomJitter[:1+jitRNG%uint32(len(w.randomJitter)-1)]
//fmt.Println("w.buf:", len(w.buf), "remain:", len(remain), "jitter:", len(jit))
w.gw.(writer.GzipWriterExt).SetHeader(writer.Header{Comment: &jit})
}
Expand Down Expand Up @@ -505,7 +504,7 @@ type config struct {
suffixETag string
dropETag bool
jitterBuffer int
randomJitter []byte
randomJitter string
}

func (c *config) validate() error {
Expand Down Expand Up @@ -696,14 +695,13 @@ func DropETag() option {
func RandomJitter(n, buffer int) option {
return func(c *config) {
if n > 0 {
c.randomJitter = bytes.Repeat([]byte("Padding-"), 1+(n/8))
c.randomJitter = c.randomJitter[:(n + 1)]
c.randomJitter = strings.Repeat("Padding-", 1+(n/8))[:n+1]
c.jitterBuffer = buffer
if c.jitterBuffer == 0 {
c.jitterBuffer = 64 << 10
}
} else {
c.randomJitter = nil
c.randomJitter = ""
c.jitterBuffer = 0
}
}
Expand Down

0 comments on commit de7a4b8

Please sign in to comment.