Sfoglia il codice sorgente

Fix and optimize ETag calc

DarthSim 6 anni fa
parent
commit
85d2738a72
1 ha cambiato i file con 36 aggiunte e 9 eliminazioni
  1. 36 9
      etag.go

+ 36 - 9
etag.go

@@ -3,20 +3,47 @@ package main
 import (
 	"context"
 	"crypto/sha256"
-	"encoding/binary"
-	"fmt"
+	"encoding/hex"
+	"encoding/json"
+	"hash"
+	"sync"
 )
 
 var notModifiedErr = newError(304, "Not modified", "Not modified")
 
+type eTagCalc struct {
+	hash hash.Hash
+	enc  *json.Encoder
+}
+
+var eTagCalcPool = sync.Pool{
+	New: func() interface{} {
+		h := sha256.New()
+
+		enc := json.NewEncoder(h)
+		enc.SetEscapeHTML(false)
+		enc.SetIndent("", "")
+
+		return &eTagCalc{h, enc}
+	},
+}
+
 func calcETag(ctx context.Context) []byte {
-	footprint := sha256.Sum256(getImageData(ctx).Bytes())
+	c := eTagCalcPool.Get().(*eTagCalc)
+	defer eTagCalcPool.Put(c)
+
+	c.hash.Reset()
+	c.hash.Write(getImageData(ctx).Bytes())
+	footprint := c.hash.Sum(nil)
+
+	c.hash.Reset()
+	c.hash.Write(footprint)
+	c.hash.Write([]byte(version))
+	c.enc.Encode(conf)
+	c.enc.Encode(getProcessingOptions(ctx))
 
-	hash := sha256.New()
-	hash.Write(footprint[:])
-	hash.Write([]byte(version))
-	binary.Write(hash, binary.LittleEndian, conf)
-	binary.Write(hash, binary.LittleEndian, *getProcessingOptions(ctx))
+	etag := make([]byte, 64)
+	hex.Encode(etag, c.hash.Sum(nil))
 
-	return []byte(fmt.Sprintf("%x", hash.Sum(nil)))
+	return etag
 }