Merge pull request #196 from mrd0ll4r/refactor-peerstore-gc

memory: fix garbage collection, add config check
This commit is contained in:
Jimmy Zelinskie 2016-08-17 17:30:47 -04:00 committed by GitHub
commit 0916afafee
2 changed files with 13 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package memory
import (
"encoding/binary"
"errors"
"log"
"net"
"runtime"
@ -12,6 +13,10 @@ import (
"github.com/chihaya/chihaya/storage"
)
// ErrInvalidGCInterval is returned for a GarbageCollectionInterval that is
// less than or equal to zero.
var ErrInvalidGCInterval = errors.New("invalid garbage collection interval")
// Config holds the configuration of a memory PeerStore.
type Config struct {
GarbageCollectionInterval time.Duration `yaml:"gc_interval"`
@ -27,6 +32,10 @@ func New(cfg Config) (storage.PeerStore, error) {
shardCount = cfg.ShardCount
}
if cfg.GarbageCollectionInterval <= 0 {
return nil, ErrInvalidGCInterval
}
ps := &peerStore{
shards: make([]*peerShard, shardCount*2),
closed: make(chan struct{}),
@ -43,7 +52,7 @@ func New(cfg Config) (storage.PeerStore, error) {
case <-ps.closed:
return
case <-time.After(cfg.GarbageCollectionInterval):
before := time.Now().Add(-cfg.GarbageCollectionInterval)
before := time.Now().Add(-cfg.PeerLifetime)
log.Println("memory: purging peers with no announces since ", before)
ps.collectGarbage(before)
}

View file

@ -3,11 +3,13 @@ package memory
import (
"testing"
"time"
s "github.com/chihaya/chihaya/storage"
)
func createNew() s.PeerStore {
ps, err := New(Config{ShardCount: 1024})
ps, err := New(Config{ShardCount: 1024, GarbageCollectionInterval: 10 * time.Minute})
if err != nil {
panic(err)
}