go-peerstream update (accept concurrency)
https://github.com/jbenet/go-peerstream/commit/8d52ed2801410a2af995b4e87660272d11c8a9a4
Juan Batiz-Benet committed
May 13, 2015 at 02:59 UTC
d25147d04282ecc978fad6336380a6687f75f97b
2 files changed
+23
-8
Godeps/Godeps.json
+1
-1
@@ -191,7 +191,7 @@
191
},
192
{
193
"ImportPath": "github.com/jbenet/go-peerstream",
194
- "Rev": "bbe2a6461aa80ee25fd87eccf35bd54bac7f788d"
194
+ "Rev": "8d52ed2801410a2af995b4e87660272d11c8a9a4"
195
},
196
{
197
"ImportPath": "github.com/jbenet/go-random",
Godeps/_workspace/src/github.com/jbenet/go-peerstream/listener.go
+22
-7
@@ -8,6 +8,13 @@ import (
8
tec "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-temp-err-catcher"
9
)
10
11
+// AcceptConcurrency is how many connections can simultaneously be
12
+// in process of being accepted. Handshakes can sometimes occurr as
13
+// part of this process, so it may take some time. It is imporant to
14
+// rate limit lest a malicious influx of connections would cause our
15
+// node to consume all its resources accepting new connections.
16
+var AcceptConcurrency = 200
17
+
18
type Listener struct {
19
netList net.Listener
20
groups groupSet
@@ -73,6 +80,9 @@ func (l *Listener) accept() {
80
// Using the lib: https://godoc.org/github.com/jbenet/go-temp-err-catcher
81
var catcher tec.TempErrCatcher
82
83
+ // rate limit concurrency
84
+ limit := make(chan struct{}, AcceptConcurrency)
85
+
86
// loop forever accepting connections
87
for {
88
conn, err := l.netList.Accept()
@@ -85,13 +95,18 @@ func (l *Listener) accept() {
95
}
96
97
// add conn to swarm and listen for incoming streams
88
- // log.Printf("accepted conn %s\n", conn.RemoteAddr())
89
- conn2, err := l.swarm.addConn(conn, true)
90
- if err != nil {
91
- l.acceptErr <- err
92
- continue
93
- }
94
- conn2.groups.AddSet(&l.groups) // add out groups
98
+ // do this in a goroutine to avoid blocking the Accept loop.
99
+ // note that this does not rate limit accepts.
100
+ limit <- struct{}{} // sema down
101
+ go func(conn net.Conn) {
102
+ defer func() { <-limit }() // sema up
103
+
104
+ conn2, err := l.swarm.addConn(conn, true)
105
+ if err != nil {
106
+ l.acceptErr <- err
107
+ }
108
+ conn2.groups.AddSet(&l.groups) // add out groups
109
+ }(conn)
110
}
111
}
112