mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-10 22:29:08 -07:00
Use atomics to track peermap size
This commit is contained in:
@@ -7,6 +7,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
"github.com/chihaya/chihaya/stats"
|
"github.com/chihaya/chihaya/stats"
|
||||||
@@ -18,6 +19,7 @@ type PeerMap struct {
|
|||||||
Peers map[string]map[PeerKey]Peer `json:"peers"`
|
Peers map[string]map[PeerKey]Peer `json:"peers"`
|
||||||
Seeders bool `json:"seeders"`
|
Seeders bool `json:"seeders"`
|
||||||
Config config.SubnetConfig `json:"config"`
|
Config config.SubnetConfig `json:"config"`
|
||||||
|
Size int32
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +98,10 @@ func (pm *PeerMap) Put(p Peer) {
|
|||||||
if !exists {
|
if !exists {
|
||||||
pm.Peers[maskedIP] = make(map[PeerKey]Peer)
|
pm.Peers[maskedIP] = make(map[PeerKey]Peer)
|
||||||
}
|
}
|
||||||
|
_, exists = pm.Peers[maskedIP][p.Key()]
|
||||||
|
if !exists {
|
||||||
|
atomic.AddInt32(&(pm.Size), 1)
|
||||||
|
}
|
||||||
pm.Peers[maskedIP][p.Key()] = p
|
pm.Peers[maskedIP][p.Key()] = p
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,19 +111,16 @@ func (pm *PeerMap) Delete(pk PeerKey) {
|
|||||||
defer pm.Unlock()
|
defer pm.Unlock()
|
||||||
|
|
||||||
maskedIP := pm.mask(pk.IP())
|
maskedIP := pm.mask(pk.IP())
|
||||||
delete(pm.Peers[maskedIP], pk)
|
_, exists := pm.Peers[maskedIP][pk]
|
||||||
|
if exists {
|
||||||
|
atomic.AddInt32(&(pm.Size), -1)
|
||||||
|
delete(pm.Peers[maskedIP], pk)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the number of peers within a PeerMap.
|
// Len returns the number of peers within a PeerMap.
|
||||||
func (pm *PeerMap) Len() int {
|
func (pm *PeerMap) Len() int {
|
||||||
pm.RLock()
|
return int(atomic.LoadInt32(&pm.Size))
|
||||||
defer pm.RUnlock()
|
|
||||||
|
|
||||||
var count int
|
|
||||||
for _, subnetmap := range pm.Peers {
|
|
||||||
count += len(subnetmap)
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Purge iterates over all of the peers within a PeerMap and deletes them if
|
// Purge iterates over all of the peers within a PeerMap and deletes them if
|
||||||
@@ -129,6 +132,7 @@ func (pm *PeerMap) Purge(unixtime int64) {
|
|||||||
for _, subnetmap := range pm.Peers {
|
for _, subnetmap := range pm.Peers {
|
||||||
for key, peer := range subnetmap {
|
for key, peer := range subnetmap {
|
||||||
if peer.LastAnnounce <= unixtime {
|
if peer.LastAnnounce <= unixtime {
|
||||||
|
atomic.AddInt32(&(pm.Size), -1)
|
||||||
delete(subnetmap, key)
|
delete(subnetmap, key)
|
||||||
if pm.Seeders {
|
if pm.Seeders {
|
||||||
stats.RecordPeerEvent(stats.ReapedSeed, peer.HasIPv6())
|
stats.RecordPeerEvent(stats.ReapedSeed, peer.HasIPv6())
|
||||||
|
|||||||
Reference in New Issue
Block a user