mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-11 23:46:39 +08:00
51 lines
993 B
Go
51 lines
993 B
Go
package corerepo
|
|
|
|
import (
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
"github.com/jbenet/go-ipfs/core"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
|
|
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
|
)
|
|
|
|
var log = eventlog.Logger("corerepo")
|
|
|
|
type KeyRemoved struct {
|
|
Key u.Key
|
|
}
|
|
|
|
func GarbageCollectAsync(n *core.IpfsNode, ctx context.Context) (<-chan *KeyRemoved, error) {
|
|
|
|
keychan, err := n.Blockstore.AllKeysChan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
output := make(chan *KeyRemoved)
|
|
go func() {
|
|
defer close(output)
|
|
for {
|
|
select {
|
|
case k, ok := <-keychan:
|
|
if !ok {
|
|
return
|
|
}
|
|
if !n.Pinning.IsPinned(k) {
|
|
err := n.Blockstore.DeleteBlock(k)
|
|
if err != nil {
|
|
log.Errorf("Error removing key from blockstore: %s", err)
|
|
continue
|
|
}
|
|
select {
|
|
case output <- &KeyRemoved{k}:
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return output, nil
|
|
}
|