aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-14 18:47:31 +0800
committerobscuren <geffobscura@gmail.com>2015-04-14 18:47:31 +0800
commit474aa924ca525da2fb43f888c6d527f39d07c261 (patch)
tree70187e77b68f8f016a1dcf0e74755b55631d9623 /p2p
parentb7e1b686aa1fc2c44fd6827b08c546629607d1a2 (diff)
downloaddexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar.gz
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar.bz2
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar.lz
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar.xz
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.tar.zst
dexon-474aa924ca525da2fb43f888c6d527f39d07c261.zip
p2p: added limiter function to limit package broadcasting
Diffstat (limited to 'p2p')
-rw-r--r--p2p/server.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/p2p/server.go b/p2p/server.go
index 61e0d71e9..eaffc9d13 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -136,6 +136,12 @@ func (srv *Server) SuggestPeer(n *discover.Node) {
// Broadcast sends an RLP-encoded message to all connected peers.
// This method is deprecated and will be removed later.
func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) error {
+ return srv.BroadcastLimited(protocol, code, func(i float64) float64 { return i }, data)
+}
+
+// BroadcastsRange an RLP-encoded message to a random set of peers using the limit function to limit the amount
+// of peers.
+func (srv *Server) BroadcastLimited(protocol string, code uint64, limit func(float64) float64, data interface{}) error {
var payload []byte
if data != nil {
var err error
@@ -146,7 +152,13 @@ func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) err
}
srv.lock.RLock()
defer srv.lock.RUnlock()
+
+ i, max := 0, int(limit(float64(len(srv.peers))))
for _, peer := range srv.peers {
+ if i >= max {
+ break
+ }
+
if peer != nil {
var msg = Msg{Code: code}
if data != nil {
@@ -154,6 +166,7 @@ func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) err
msg.Size = uint32(len(payload))
}
peer.writeProtoMsg(protocol, msg)
+ i++
}
}
return nil