mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
3d13e6afb2
Month of testing and profiling show that fasthttp doesn't gives us significatnt profit in memory and performance while being incompatible with many third-side packages and http/2
45 lines
734 B
Go
45 lines
734 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"hash"
|
|
"sync"
|
|
)
|
|
|
|
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) string {
|
|
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))
|
|
|
|
return hex.EncodeToString(c.hash.Sum(nil))
|
|
}
|