memory: add max numwant

This commit is contained in:
Jimmy Zelinskie 2016-08-16 20:32:38 -04:00
parent 6fddcb8eea
commit a553ded043
2 changed files with 15 additions and 11 deletions

View file

@ -15,11 +15,10 @@ trakr:
allow_ip_spoofing: false
storage:
name: memory
config:
shards: 1
gc_interval: 14m
peer_lifetime: 15m
shards: 1
max_numwant: 100
prehooks:
- name: jwt

View file

@ -12,13 +12,12 @@ import (
"github.com/jzelinskie/trakr/storage"
)
// TODO(jzelinskie): separate ipv4 and ipv6 swarms
// Config holds the configuration of a memory PeerStore.
type Config struct {
GarbageCollectionInterval time.Duration `yaml:"gc_interval"`
PeerLifetime time.Duration `yaml:"peer_lifetime"`
ShardCount int `yaml:"shard_count"`
MaxNumWant int `yaml:"max_numwant"`
}
// New creates a new PeerStore backed by memory.
@ -31,6 +30,7 @@ func New(cfg Config) (storage.PeerStore, error) {
ps := &peerStore{
shards: make([]*peerShard, shardCount*2),
closed: make(chan struct{}),
maxNumWant: cfg.MaxNumWant,
}
for i := 0; i < shardCount*2; i++ {
@ -69,6 +69,7 @@ type swarm struct {
type peerStore struct {
shards []*peerShard
closed chan struct{}
maxNumWant int
}
var _ storage.PeerStore = &peerStore{}
@ -246,6 +247,10 @@ func (s *peerStore) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant i
default:
}
if numWant > s.maxNumWant {
numWant = s.maxNumWant
}
shard := s.shards[s.shardIndex(ih, announcer)]
shard.RLock()