Adder: adding with --local forcefully allocates the local peer to the pin

It is not good to add something locally only to pin it somewhere else:
  * The locally used space is not GCed automatically or anything and is lost
  * Pay the penalty of having to copy things somewhere else
This commit is contained in:
Hector Sanjuan 2022-02-01 20:38:41 +01:00
parent 7071f6777b
commit 07b4971277

View File

@ -53,12 +53,28 @@ func (dgs *DAGService) Add(ctx context.Context, node ipld.Node) error {
if err != nil {
return err
}
dgs.dests = dests
if dgs.local {
// If this is a local pin, make sure that the local peer is
// among the allocations.
localPid := dgs.rpcClient.ID()
hasLocal := false
for _, d := range dests {
if d == localPid {
hasLocal = true
break
}
}
if !hasLocal && localPid != "" {
// replace last allocation with local peer
dgs.dests[len(dgs.dests)-1] = localPid
}
dgs.ba = adder.NewBlockAdder(dgs.rpcClient, []peer.ID{""})
} else {
dgs.ba = adder.NewBlockAdder(dgs.rpcClient, dests)
dgs.ba = adder.NewBlockAdder(dgs.rpcClient, dgs.dests)
}
}