summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-pools/henc.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-05-08 19:49:45 +0200
committerLibravatar GitHub <noreply@github.com>2022-05-08 18:49:45 +0100
commit5004e0a9da665ccc0e18cd4075ee636641b71f0a (patch)
treeb7c8269b954ced61afa9fffd7305bd88acca6f8e /vendor/codeberg.org/gruf/go-pools/henc.go
parent[bugfix] Fix existing bio text showing as HTML (#531) (diff)
downloadgotosocial-5004e0a9da665ccc0e18cd4075ee636641b71f0a.tar.xz
[bugfix] Fix remote media pruning failing if media already gone (#548)
* fix error check of prune to allow missing files * update go-store library, add test for pruning item with db entry but no file Signed-off-by: kim <grufwub@gmail.com> * remove now-unneccessary error check Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-pools/henc.go')
-rw-r--r--vendor/codeberg.org/gruf/go-pools/henc.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-pools/henc.go b/vendor/codeberg.org/gruf/go-pools/henc.go
new file mode 100644
index 000000000..cad905af4
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-pools/henc.go
@@ -0,0 +1,46 @@
+package pools
+
+import (
+ "hash"
+ "sync"
+
+ "codeberg.org/gruf/go-hashenc"
+)
+
+// HashEncoderPool is a pooled allocator for hashenc.HashEncoder objects.
+type HashEncoderPool interface {
+ // Get fetches a hashenc.HashEncoder from pool
+ Get() hashenc.HashEncoder
+
+ // Put places supplied hashenc.HashEncoder back in pool
+ Put(hashenc.HashEncoder)
+}
+
+// NewHashEncoderPool returns a newly instantiated hashenc.HashEncoder pool.
+func NewHashEncoderPool(hash func() hash.Hash, enc func() hashenc.Encoder) HashEncoderPool {
+ return &hencPool{
+ pool: sync.Pool{
+ New: func() interface{} {
+ return hashenc.New(hash(), enc())
+ },
+ },
+ size: hashenc.New(hash(), enc()).Size(),
+ }
+}
+
+// hencPool is our implementation of HashEncoderPool.
+type hencPool struct {
+ pool sync.Pool
+ size int
+}
+
+func (p *hencPool) Get() hashenc.HashEncoder {
+ return p.pool.Get().(hashenc.HashEncoder)
+}
+
+func (p *hencPool) Put(henc hashenc.HashEncoder) {
+ if henc.Size() < p.size {
+ return
+ }
+ p.pool.Put(henc)
+}