mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 05:17:49 +08:00
Turns out that `pool.Put(buf)` had to *allocate* because we needed to turn
`[]byte` into `interface{}`. Apparently, we've never done this correctly we just
never noticed because we never really used buffer pools extensively.
However, since migrating yamux to a buffer-pool backed buffer, this started
showing up in allocation profiles.
License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
48 lines
802 B
Go
48 lines
802 B
Go
package namesys
|
|
|
|
import (
|
|
"time"
|
|
|
|
path "gx/ipfs/QmTy6VoHV2E5baEFDXbp1xmHhxSVff5qTSrTsoXxD1eB2P/go-path"
|
|
)
|
|
|
|
func (ns *mpns) cacheGet(name string) (path.Path, bool) {
|
|
if ns.cache == nil {
|
|
return "", false
|
|
}
|
|
|
|
ientry, ok := ns.cache.Get(name)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
|
|
entry, ok := ientry.(cacheEntry)
|
|
if !ok {
|
|
// should never happen, purely for sanity
|
|
log.Panicf("unexpected type %T in cache for %q.", ientry, name)
|
|
}
|
|
|
|
if time.Now().Before(entry.eol) {
|
|
return entry.val, true
|
|
}
|
|
|
|
ns.cache.Remove(name)
|
|
|
|
return "", false
|
|
}
|
|
|
|
func (ns *mpns) cacheSet(name string, val path.Path, ttl time.Duration) {
|
|
if ns.cache == nil || ttl <= 0 {
|
|
return
|
|
}
|
|
ns.cache.Add(name, cacheEntry{
|
|
val: val,
|
|
eol: time.Now().Add(ttl),
|
|
})
|
|
}
|
|
|
|
type cacheEntry struct {
|
|
val path.Path
|
|
eol time.Time
|
|
}
|