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 allow_ip_spoofing: false
storage: storage:
name: memory
config:
shards: 1
gc_interval: 14m gc_interval: 14m
peer_lifetime: 15m peer_lifetime: 15m
shards: 1
max_numwant: 100
prehooks: prehooks:
- name: jwt - name: jwt

View file

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