2016-01-25 06:41:39 +01:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash/fnv"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya"
|
|
|
|
"github.com/chihaya/chihaya/server/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
store.RegisterPeerStoreDriver("memory", &peerStoreDriver{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStoreDriver struct{}
|
|
|
|
|
2016-03-11 21:09:49 +01:00
|
|
|
func (d *peerStoreDriver) New(storecfg *store.DriverConfig) (store.PeerStore, error) {
|
2016-01-25 06:41:39 +01:00
|
|
|
cfg, err := newPeerStoreConfig(storecfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:45:28 +02:00
|
|
|
shards := make([]*peerShard, cfg.Shards)
|
|
|
|
for i := 0; i < cfg.Shards; i++ {
|
|
|
|
shards[i] = &peerShard{}
|
|
|
|
shards[i].peers = make(map[string]map[string]peer)
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
return &peerStore{
|
2016-03-31 02:45:28 +02:00
|
|
|
shards: shards,
|
2016-01-25 06:41:39 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStoreConfig struct {
|
|
|
|
Shards int `yaml:"shards"`
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:09:49 +01:00
|
|
|
func newPeerStoreConfig(storecfg *store.DriverConfig) (*peerStoreConfig, error) {
|
|
|
|
bytes, err := yaml.Marshal(storecfg.Config)
|
2016-01-25 06:41:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg peerStoreConfig
|
|
|
|
err = yaml.Unmarshal(bytes, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:45:28 +02:00
|
|
|
if cfg.Shards < 1 {
|
|
|
|
cfg.Shards = 1
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
return &cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type peer struct {
|
|
|
|
chihaya.Peer
|
|
|
|
LastAction time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerShard struct {
|
|
|
|
peers map[string]map[string]peer
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStore struct {
|
|
|
|
shards []*peerShard
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.PeerStore = &peerStore{}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func (s *peerStore) shardIndex(infoHash chihaya.InfoHash) uint32 {
|
2016-01-25 06:41:39 +01:00
|
|
|
idx := fnv.New32()
|
2016-02-16 01:49:25 +01:00
|
|
|
idx.Write([]byte(infoHash))
|
2016-01-25 06:41:39 +01:00
|
|
|
return idx.Sum32() % uint32(len(s.shards))
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func peerKey(p chihaya.Peer) string {
|
|
|
|
return string(p.IP) + string(p.ID)
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func seedersKey(infoHash chihaya.InfoHash) string {
|
|
|
|
return string(infoHash) + "-s"
|
|
|
|
}
|
|
|
|
|
|
|
|
func leechersKey(infoHash chihaya.InfoHash) string {
|
|
|
|
return string(infoHash) + "-l"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *peerStore) PutSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
|
|
|
key := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Lock()
|
|
|
|
defer shard.Unlock()
|
|
|
|
|
|
|
|
if shard.peers[key] == nil {
|
|
|
|
shard.peers[key] = make(map[string]peer)
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
shard.peers[key][peerKey(p)] = peer{
|
2016-01-25 06:41:39 +01:00
|
|
|
Peer: p,
|
|
|
|
LastAction: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func (s *peerStore) DeleteSeeder(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
|
|
|
key := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Lock()
|
|
|
|
defer shard.Unlock()
|
|
|
|
|
|
|
|
if shard.peers[key] == nil {
|
2016-05-01 23:56:07 +02:00
|
|
|
return store.ErrResourceDoesNotExist
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
pk := peerKey(p)
|
|
|
|
|
|
|
|
if _, ok := shard.peers[key][pk]; !ok {
|
|
|
|
return store.ErrResourceDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(shard.peers[key], pk)
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
if len(shard.peers[key]) == 0 {
|
2016-02-19 14:15:40 +01:00
|
|
|
delete(shard.peers, key)
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func (s *peerStore) PutLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
|
|
|
key := leechersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Lock()
|
|
|
|
defer shard.Unlock()
|
|
|
|
|
|
|
|
if shard.peers[key] == nil {
|
|
|
|
shard.peers[key] = make(map[string]peer)
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
shard.peers[key][peerKey(p)] = peer{
|
2016-01-25 06:41:39 +01:00
|
|
|
Peer: p,
|
|
|
|
LastAction: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func (s *peerStore) DeleteLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
|
|
|
key := leechersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Lock()
|
|
|
|
defer shard.Unlock()
|
|
|
|
|
|
|
|
if shard.peers[key] == nil {
|
2016-05-01 23:56:07 +02:00
|
|
|
return store.ErrResourceDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := peerKey(p)
|
|
|
|
|
|
|
|
if _, ok := shard.peers[key][pk]; !ok {
|
|
|
|
return store.ErrResourceDoesNotExist
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:56:07 +02:00
|
|
|
delete(shard.peers[key], pk)
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
if len(shard.peers[key]) == 0 {
|
2016-02-19 14:15:40 +01:00
|
|
|
delete(shard.peers, key)
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
func (s *peerStore) GraduateLeecher(infoHash chihaya.InfoHash, p chihaya.Peer) error {
|
|
|
|
lkey := leechersKey(infoHash)
|
|
|
|
skey := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Lock()
|
|
|
|
defer shard.Unlock()
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
if shard.peers[lkey] != nil {
|
|
|
|
delete(shard.peers[lkey], peerKey(p))
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
if shard.peers[skey] == nil {
|
|
|
|
shard.peers[skey] = make(map[string]peer)
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-16 01:49:25 +01:00
|
|
|
shard.peers[skey][peerKey(p)] = peer{
|
2016-01-25 06:41:39 +01:00
|
|
|
Peer: p,
|
|
|
|
LastAction: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *peerStore) CollectGarbage(cutoff time.Time) error {
|
|
|
|
for _, shard := range s.shards {
|
|
|
|
shard.RLock()
|
|
|
|
var keys []string
|
|
|
|
for key := range shard.peers {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
shard.RUnlock()
|
|
|
|
runtime.Gosched()
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
shard.Lock()
|
2016-02-16 01:49:25 +01:00
|
|
|
|
|
|
|
for peerKey, p := range shard.peers[key] {
|
2016-01-25 06:41:39 +01:00
|
|
|
if p.LastAction.Before(cutoff) {
|
2016-02-16 01:49:25 +01:00
|
|
|
delete(shard.peers[key], peerKey)
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:22:21 +01:00
|
|
|
if len(shard.peers[key]) == 0 {
|
2016-02-19 14:15:40 +01:00
|
|
|
delete(shard.peers, key)
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.Unlock()
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:06:32 +02:00
|
|
|
func (s *peerStore) AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int, peer4, peer6 chihaya.Peer) (peers, peers6 []chihaya.Peer, err error) {
|
2016-02-16 01:49:25 +01:00
|
|
|
lkey := leechersKey(infoHash)
|
|
|
|
skey := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
2016-01-25 06:41:39 +01:00
|
|
|
shard.RLock()
|
|
|
|
defer shard.RUnlock()
|
|
|
|
|
|
|
|
if seeder {
|
|
|
|
// Append leechers as possible.
|
2016-02-16 01:49:25 +01:00
|
|
|
leechers := shard.peers[lkey]
|
2016-01-25 06:41:39 +01:00
|
|
|
for _, p := range leechers {
|
|
|
|
if numWant == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.IP.To4() == nil {
|
|
|
|
peers6 = append(peers6, p.Peer)
|
|
|
|
} else {
|
|
|
|
peers = append(peers, p.Peer)
|
|
|
|
}
|
|
|
|
numWant--
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Append as many seeders as possible.
|
2016-02-16 01:49:25 +01:00
|
|
|
seeders := shard.peers[skey]
|
2016-01-25 06:41:39 +01:00
|
|
|
for _, p := range seeders {
|
|
|
|
if numWant == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.IP.To4() == nil {
|
|
|
|
peers6 = append(peers6, p.Peer)
|
|
|
|
} else {
|
|
|
|
peers = append(peers, p.Peer)
|
|
|
|
}
|
|
|
|
numWant--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append leechers until we reach numWant.
|
2016-02-16 01:49:25 +01:00
|
|
|
leechers := shard.peers[lkey]
|
2016-01-25 06:41:39 +01:00
|
|
|
if numWant > 0 {
|
|
|
|
for _, p := range leechers {
|
|
|
|
if numWant == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.IP.To4() == nil {
|
2016-04-16 22:01:18 +02:00
|
|
|
if p.Equal(peer6) {
|
2016-04-12 18:06:32 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
peers6 = append(peers6, p.Peer)
|
|
|
|
} else {
|
2016-04-16 22:01:18 +02:00
|
|
|
if p.Equal(peer4) {
|
2016-04-12 18:06:32 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
peers = append(peers, p.Peer)
|
|
|
|
}
|
|
|
|
numWant--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-03-31 02:45:28 +02:00
|
|
|
|
|
|
|
func (s *peerStore) GetSeeders(infoHash chihaya.InfoHash) (peers, peers6 []chihaya.Peer, err error) {
|
|
|
|
key := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
|
|
|
shard.RLock()
|
|
|
|
defer shard.RUnlock()
|
|
|
|
|
|
|
|
seeders := shard.peers[key]
|
|
|
|
for _, p := range seeders {
|
|
|
|
if p.IP.To4() == nil {
|
|
|
|
peers6 = append(peers6, p.Peer)
|
|
|
|
} else {
|
|
|
|
peers = append(peers, p.Peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *peerStore) GetLeechers(infoHash chihaya.InfoHash) (peers, peers6 []chihaya.Peer, err error) {
|
|
|
|
key := leechersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
|
|
|
shard.RLock()
|
|
|
|
defer shard.RUnlock()
|
|
|
|
|
|
|
|
leechers := shard.peers[key]
|
|
|
|
for _, p := range leechers {
|
|
|
|
if p.IP.To4() == nil {
|
|
|
|
peers6 = append(peers6, p.Peer)
|
|
|
|
} else {
|
|
|
|
peers = append(peers, p.Peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *peerStore) NumSeeders(infoHash chihaya.InfoHash) int {
|
|
|
|
key := seedersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
|
|
|
shard.RLock()
|
|
|
|
defer shard.RUnlock()
|
|
|
|
|
|
|
|
return len(shard.peers[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *peerStore) NumLeechers(infoHash chihaya.InfoHash) int {
|
|
|
|
key := leechersKey(infoHash)
|
|
|
|
shard := s.shards[s.shardIndex(infoHash)]
|
|
|
|
shard.RLock()
|
|
|
|
defer shard.RUnlock()
|
|
|
|
|
|
|
|
return len(shard.peers[key])
|
|
|
|
}
|