Merge pull request #196 from mrd0ll4r/refactor-peerstore-gc
memory: fix garbage collection, add config check
This commit is contained in:
commit
0916afafee
2 changed files with 13 additions and 2 deletions
|
@ -2,6 +2,7 @@ package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -12,6 +13,10 @@ import (
|
||||||
"github.com/chihaya/chihaya/storage"
|
"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.
|
// Config holds the configuration of a memory PeerStore.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
GarbageCollectionInterval time.Duration `yaml:"gc_interval"`
|
GarbageCollectionInterval time.Duration `yaml:"gc_interval"`
|
||||||
|
@ -27,6 +32,10 @@ func New(cfg Config) (storage.PeerStore, error) {
|
||||||
shardCount = cfg.ShardCount
|
shardCount = cfg.ShardCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.GarbageCollectionInterval <= 0 {
|
||||||
|
return nil, ErrInvalidGCInterval
|
||||||
|
}
|
||||||
|
|
||||||
ps := &peerStore{
|
ps := &peerStore{
|
||||||
shards: make([]*peerShard, shardCount*2),
|
shards: make([]*peerShard, shardCount*2),
|
||||||
closed: make(chan struct{}),
|
closed: make(chan struct{}),
|
||||||
|
@ -43,7 +52,7 @@ func New(cfg Config) (storage.PeerStore, error) {
|
||||||
case <-ps.closed:
|
case <-ps.closed:
|
||||||
return
|
return
|
||||||
case <-time.After(cfg.GarbageCollectionInterval):
|
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)
|
log.Println("memory: purging peers with no announces since ", before)
|
||||||
ps.collectGarbage(before)
|
ps.collectGarbage(before)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,13 @@ package memory
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
s "github.com/chihaya/chihaya/storage"
|
s "github.com/chihaya/chihaya/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createNew() s.PeerStore {
|
func createNew() s.PeerStore {
|
||||||
ps, err := New(Config{ShardCount: 1024})
|
ps, err := New(Config{ShardCount: 1024, GarbageCollectionInterval: 10 * time.Minute})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue