fix memory optimization
This commit is contained in:
parent
40d0236596
commit
1530a37138
1 changed files with 22 additions and 20 deletions
|
@ -33,7 +33,7 @@ func (d *peerStoreDriver) New(storecfg *store.DriverConfig) (store.PeerStore, er
|
||||||
shards := make([]*peerShard, cfg.Shards)
|
shards := make([]*peerShard, cfg.Shards)
|
||||||
for i := 0; i < cfg.Shards; i++ {
|
for i := 0; i < cfg.Shards; i++ {
|
||||||
shards[i] = &peerShard{}
|
shards[i] = &peerShard{}
|
||||||
shards[i].swarms = make(map[chihaya.InfoHash]*swarm)
|
shards[i].swarms = make(map[chihaya.InfoHash]swarm)
|
||||||
}
|
}
|
||||||
return &peerStore{
|
return &peerStore{
|
||||||
shards: shards,
|
shards: shards,
|
||||||
|
@ -66,7 +66,7 @@ func newPeerStoreConfig(storecfg *store.DriverConfig) (*peerStoreConfig, error)
|
||||||
type serializedPeer string
|
type serializedPeer string
|
||||||
|
|
||||||
type peerShard struct {
|
type peerShard struct {
|
||||||
swarms map[chihaya.InfoHash]*swarm
|
swarms map[chihaya.InfoHash]swarm
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,8 +115,8 @@ func (s *peerStore) PutSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
||||||
shard.Lock()
|
shard.Lock()
|
||||||
defer shard.Unlock()
|
defer shard.Unlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
shard.swarms[infoHash] = &swarm{
|
shard.swarms[infoHash] = swarm{
|
||||||
seeders: make(map[serializedPeer]int64),
|
seeders: make(map[serializedPeer]int64),
|
||||||
leechers: make(map[serializedPeer]int64),
|
leechers: make(map[serializedPeer]int64),
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ func (s *peerStore) DeleteSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) erro
|
||||||
shard.Lock()
|
shard.Lock()
|
||||||
defer shard.Unlock()
|
defer shard.Unlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return store.ErrResourceDoesNotExist
|
return store.ErrResourceDoesNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,8 +167,8 @@ func (s *peerStore) PutLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error
|
||||||
shard.Lock()
|
shard.Lock()
|
||||||
defer shard.Unlock()
|
defer shard.Unlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
shard.swarms[infoHash] = &swarm{
|
shard.swarms[infoHash] = swarm{
|
||||||
seeders: make(map[serializedPeer]int64),
|
seeders: make(map[serializedPeer]int64),
|
||||||
leechers: make(map[serializedPeer]int64),
|
leechers: make(map[serializedPeer]int64),
|
||||||
}
|
}
|
||||||
|
@ -191,7 +191,7 @@ func (s *peerStore) DeleteLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) err
|
||||||
shard.Lock()
|
shard.Lock()
|
||||||
defer shard.Unlock()
|
defer shard.Unlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return store.ErrResourceDoesNotExist
|
return store.ErrResourceDoesNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,16 +220,14 @@ func (s *peerStore) GraduateLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) e
|
||||||
shard.Lock()
|
shard.Lock()
|
||||||
defer shard.Unlock()
|
defer shard.Unlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
shard.swarms[infoHash] = &swarm{
|
shard.swarms[infoHash] = swarm{
|
||||||
seeders: make(map[serializedPeer]int64),
|
seeders: make(map[serializedPeer]int64),
|
||||||
leechers: make(map[serializedPeer]int64),
|
leechers: make(map[serializedPeer]int64),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := shard.swarms[infoHash].leechers[key]; ok {
|
|
||||||
delete(shard.swarms[infoHash].leechers, key)
|
delete(shard.swarms[infoHash].leechers, key)
|
||||||
}
|
|
||||||
|
|
||||||
shard.swarms[infoHash].seeders[key] = time.Now().UnixNano()
|
shard.swarms[infoHash].seeders[key] = time.Now().UnixNano()
|
||||||
|
|
||||||
|
@ -294,6 +292,10 @@ func (s *peerStore) AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWan
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
defer shard.RUnlock()
|
defer shard.RUnlock()
|
||||||
|
|
||||||
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
|
return nil, nil, store.ErrResourceDoesNotExist
|
||||||
|
}
|
||||||
|
|
||||||
if seeder {
|
if seeder {
|
||||||
// Append leechers as possible.
|
// Append leechers as possible.
|
||||||
leechers := shard.swarms[infoHash].leechers
|
leechers := shard.swarms[infoHash].leechers
|
||||||
|
@ -366,7 +368,7 @@ func (s *peerStore) GetSeeders(infoHash chihaya.InfoHash) (peers, peers6 []chiha
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
defer shard.RUnlock()
|
defer shard.RUnlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return nil, nil, store.ErrResourceDoesNotExist
|
return nil, nil, store.ErrResourceDoesNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,12 +395,12 @@ func (s *peerStore) GetLeechers(infoHash chihaya.InfoHash) (peers, peers6 []chih
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
defer shard.RUnlock()
|
defer shard.RUnlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return nil, nil, store.ErrResourceDoesNotExist
|
return nil, nil, store.ErrResourceDoesNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
seeders := shard.swarms[infoHash].leechers
|
leechers := shard.swarms[infoHash].leechers
|
||||||
for p := range seeders {
|
for p := range leechers {
|
||||||
decodedPeer := decodePeerKey(p)
|
decodedPeer := decodePeerKey(p)
|
||||||
if decodedPeer.IP.To4() == nil {
|
if decodedPeer.IP.To4() == nil {
|
||||||
peers6 = append(peers6, decodedPeer)
|
peers6 = append(peers6, decodedPeer)
|
||||||
|
@ -420,7 +422,7 @@ func (s *peerStore) NumSeeders(infoHash chihaya.InfoHash) int {
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
defer shard.RUnlock()
|
defer shard.RUnlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,7 +440,7 @@ func (s *peerStore) NumLeechers(infoHash chihaya.InfoHash) int {
|
||||||
shard.RLock()
|
shard.RLock()
|
||||||
defer shard.RUnlock()
|
defer shard.RUnlock()
|
||||||
|
|
||||||
if shard.swarms[infoHash] == nil {
|
if _, ok := shard.swarms[infoHash]; !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,7 +453,7 @@ func (s *peerStore) Stop() <-chan error {
|
||||||
shards := make([]*peerShard, len(s.shards))
|
shards := make([]*peerShard, len(s.shards))
|
||||||
for i := 0; i < len(s.shards); i++ {
|
for i := 0; i < len(s.shards); i++ {
|
||||||
shards[i] = &peerShard{}
|
shards[i] = &peerShard{}
|
||||||
shards[i].swarms = make(map[chihaya.InfoHash]*swarm)
|
shards[i].swarms = make(map[chihaya.InfoHash]swarm)
|
||||||
}
|
}
|
||||||
s.shards = shards
|
s.shards = shards
|
||||||
close(s.closed)
|
close(s.closed)
|
||||||
|
|
Loading…
Reference in a new issue