Skip to content

Commit 93e665e

Browse files
author
jose.castillo
committed
fix: possible memory leaks
1 parent 0180922 commit 93e665e

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

group.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,16 @@ func (g *group) lookupCache(key string) (value transport.ByteView, ok bool) {
547547
return
548548
}
549549

550+
func (g *group) isOwner(key string) bool {
551+
// PickPeer devuelve (cliente, isRemote)
552+
// Si isRemote == false, este nodo es el dueño.
553+
_, remote := g.instance.PickPeer(key)
554+
return !remote
555+
}
556+
550557
// RemoteSet is called by the transport to set values in the local and hot caches when
551558
// a remote peer sends us a pb.SetRequest
552-
func (g *group) RemoteSet(key string, value []byte, expire time.Time) {
559+
/*func (g *group) RemoteSet(key string, value []byte, expire time.Time) {
553560
if g.maxCacheBytes <= 0 {
554561
return
555562
}
@@ -566,6 +573,29 @@ func (g *group) RemoteSet(key string, value []byte, expire time.Time) {
566573
// It's possible the value could be in the hot cache.
567574
g.hotCache.Remove(key)
568575
})
576+
}*/
577+
578+
func (g *group) RemoteSet(key string, value []byte, expire time.Time) {
579+
if g.maxCacheBytes <= 0 {
580+
return
581+
}
582+
583+
// Construimos el ByteView una sola vez
584+
bv := transport.ByteViewWithExpire(value, expire)
585+
586+
// Bloqueamos los Gets mientras actualizamos las cachés locales
587+
g.loadGroup.Lock(func() {
588+
if g.isOwner(key) {
589+
// Este nodo es el propietario ► almacena en mainCache
590+
g.mainCache.Add(key, bv)
591+
// Por si la clave ya estaba de paso en hotCache
592+
g.hotCache.Remove(key)
593+
} else {
594+
// Nodo no-dueño ► replica en hotCache (límite 1/8)
595+
g.hotCache.Add(key, bv)
596+
// Nunca la guardamos en mainCache aquí
597+
}
598+
})
569599
}
570600

571601
func (g *group) LocalRemove(key string) {

0 commit comments

Comments
 (0)