Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 75abc0e

Browse files
committed
[magneticod] add parameters to adjust resource usage
1 parent d55e654 commit 75abc0e

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

cmd/magneticod/dht/mainline/indexingService.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (ir IndexingResult) PeerAddrs() []net.TCPAddr {
4646
return ir.peerAddrs
4747
}
4848

49-
func NewIndexingService(laddr string, interval time.Duration, eventHandlers IndexingServiceEventHandlers) *IndexingService {
49+
func NewIndexingService(laddr string, interval time.Duration, maxNeighbors uint, eventHandlers IndexingServiceEventHandlers) *IndexingService {
5050
service := new(IndexingService)
5151
service.interval = interval
5252
service.protocol = NewProtocol(
@@ -60,7 +60,7 @@ func NewIndexingService(laddr string, interval time.Duration, eventHandlers Inde
6060
service.nodeID = make([]byte, 20)
6161
service.routingTable = make(map[string]*net.UDPAddr)
6262
service.routingTableMutex = new(sync.Mutex)
63-
service.maxNeighbors = 50
63+
service.maxNeighbors = maxNeighbors
6464
service.eventHandlers = eventHandlers
6565

6666
service.getPeersRequests = make(map[[2]byte][20]byte)
@@ -92,6 +92,7 @@ func (is *IndexingService) index() {
9292
} else {
9393
zap.L().Info("Latest status:", zap.Int("n", len(is.routingTable)),
9494
zap.Uint("maxNeighbors", is.maxNeighbors))
95+
//TODO
9596
is.findNeighbors()
9697
is.routingTable = make(map[string]*net.UDPAddr)
9798
}
@@ -134,7 +135,7 @@ func (is *IndexingService) findNeighbors() {
134135
}
135136

136137
is.protocol.SendMessage(
137-
NewFindNodeQuery(is.nodeID, target),
138+
NewSampleInfohashesQuery(is.nodeID, []byte("aa"), target),
138139
addr,
139140
)
140141
}
@@ -144,8 +145,6 @@ func (is *IndexingService) onFindNodeResponse(response *Message, addr *net.UDPAd
144145
is.routingTableMutex.Lock()
145146
defer is.routingTableMutex.Unlock()
146147

147-
//zap.S().Debugf("find node response from %+v -- %+v", addr, response)
148-
149148
for _, node := range response.R.Nodes {
150149
if uint(len(is.routingTable)) >= is.maxNeighbors {
151150
break
@@ -221,6 +220,16 @@ func (is *IndexingService) onSampleInfohashesResponse(msg *Message, addr *net.UD
221220

222221
// iterate
223222
for _, node := range msg.R.Nodes {
223+
if uint(len(is.routingTable)) >= is.maxNeighbors {
224+
break
225+
}
226+
if node.Addr.Port == 0 { // Ignore nodes who "use" port 0.
227+
continue
228+
}
229+
is.routingTable[string(node.ID)] = &node.Addr
230+
231+
// TODO
232+
/*
224233
target := make([]byte, 20)
225234
_, err := rand.Read(target)
226235
if err != nil {
@@ -230,6 +239,7 @@ func (is *IndexingService) onSampleInfohashesResponse(msg *Message, addr *net.UD
230239
NewSampleInfohashesQuery(is.nodeID, []byte("aa"), target),
231240
&node.Addr,
232241
)
242+
*/
233243
}
234244
}
235245

cmd/magneticod/dht/managers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ type Manager struct {
2424
indexingServices []Service
2525
}
2626

27-
func NewTrawlingManager(addrs []string, interval time.Duration) *Manager {
27+
func NewManager(addrs []string, interval time.Duration, maxNeighbors uint) *Manager {
2828
manager := new(Manager)
2929
manager.output = make(chan Result, 20)
3030

3131
for _, addr := range addrs {
32-
service := mainline.NewIndexingService(addr, 2*time.Second, mainline.IndexingServiceEventHandlers{
32+
service := mainline.NewIndexingService(addr, interval, maxNeighbors, mainline.IndexingServiceEventHandlers{
3333
OnResult: manager.onIndexingResult,
3434
})
3535
manager.indexingServices = append(manager.indexingServices, service)

cmd/magneticod/main.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
type opFlags struct {
2727
DatabaseURL string
2828

29-
IndexerAddrs []string
30-
IndexerInterval time.Duration
29+
IndexerAddrs []string
30+
IndexerInterval time.Duration
31+
IndexerMaxNeighbors uint
3132

3233
LeechMaxN int
3334

@@ -96,7 +97,7 @@ func main() {
9697
logger.Sugar().Fatalf("Could not open the database at `%s`", opFlags.DatabaseURL, zap.Error(err))
9798
}
9899

99-
trawlingManager := dht.NewTrawlingManager(opFlags.IndexerAddrs, opFlags.IndexerInterval)
100+
trawlingManager := dht.NewManager(opFlags.IndexerAddrs, opFlags.IndexerInterval, opFlags.IndexerMaxNeighbors)
100101
metadataSink := metadata.NewSink(5*time.Second, opFlags.LeechMaxN)
101102

102103
// The Event Loop
@@ -135,8 +136,9 @@ func parseFlags() (*opFlags, error) {
135136
var cmdF struct {
136137
DatabaseURL string `long:"database" description:"URL of the database."`
137138

138-
IndexerAddrs []string `long:"indexer-addr" description:"Address(es) to be used by indexing DHT nodes." default:"0.0.0.0:0"`
139-
IndexerInterval uint `long:"indexer-interval" description:"Indexing interval in integer seconds."`
139+
IndexerAddrs []string `long:"indexer-addr" description:"Address(es) to be used by indexing DHT nodes." default:"0.0.0.0:0"`
140+
IndexerInterval uint `long:"indexer-interval" description:"Indexing interval in integer seconds." default:"1"`
141+
IndexerMaxNeighbors uint `long:"indexer-max-neighbors" description:"Maximum number of neighbors of an indexer." default:"10000"`
140142

141143
LeechMaxN uint `long:"leech-max-n" description:"Maximum number of leeches." default:"200"`
142144

@@ -170,11 +172,8 @@ func parseFlags() (*opFlags, error) {
170172
opF.IndexerAddrs = cmdF.IndexerAddrs
171173
}
172174

173-
if cmdF.IndexerInterval == 0 {
174-
opF.IndexerInterval = 2 * time.Second
175-
} else {
176-
opF.IndexerInterval = time.Duration(cmdF.IndexerInterval) * time.Second
177-
}
175+
opF.IndexerInterval = time.Duration(cmdF.IndexerInterval) * time.Second
176+
opF.IndexerMaxNeighbors = cmdF.IndexerMaxNeighbors
178177

179178
opF.LeechMaxN = int(cmdF.LeechMaxN)
180179
if opF.LeechMaxN > 1000 {

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ require (
44
github.com/Wessie/appdirs v0.0.0-20141031215813-6573e894f8e2
55
github.com/anacrolix/missinggo v1.1.0
66
github.com/anacrolix/torrent v1.1.4
7+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
78
github.com/dustin/go-humanize v1.0.0
9+
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
810
github.com/gorilla/mux v1.7.2
911
github.com/gorilla/schema v1.1.0
12+
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
1013
github.com/jessevdk/go-flags v1.4.0
1114
github.com/kevinburke/go-bindata v3.13.0+incompatible // indirect
1215
github.com/libp2p/go-sockaddr v0.0.1
1316
github.com/mattn/go-sqlite3 v1.10.0
1417
github.com/pkg/errors v0.8.1
18+
github.com/pkg/profile v1.3.0
1519
github.com/willf/bloom v2.0.3+incompatible
1620
go.uber.org/atomic v1.4.0 // indirect
1721
go.uber.org/multierr v1.1.0 // indirect

0 commit comments

Comments
 (0)