2018-07-04 10:51:47 +00:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-09 07:24:20 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2018-07-04 10:51:47 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/transport"
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dialResult struct {
|
2019-06-09 07:24:20 +00:00
|
|
|
Conn transport.CapableConn
|
2018-07-04 10:51:47 +00:00
|
|
|
Addr ma.Multiaddr
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type dialJob struct {
|
2022-04-01 16:16:46 +00:00
|
|
|
addr ma.Multiaddr
|
|
|
|
peer peer.ID
|
|
|
|
ctx context.Context
|
|
|
|
resp chan dialResult
|
|
|
|
timeout time.Duration
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dj *dialJob) cancelled() bool {
|
2019-06-09 07:24:20 +00:00
|
|
|
return dj.ctx.Err() != nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dialLimiter struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
fdConsuming int
|
|
|
|
fdLimit int
|
|
|
|
waitingOnFd []*dialJob
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
dialFunc dialfunc
|
|
|
|
|
|
|
|
activePerPeer map[peer.ID]int
|
|
|
|
perPeerLimit int
|
|
|
|
waitingOnPeerLimit map[peer.ID][]*dialJob
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
type dialfunc func(context.Context, peer.ID, ma.Multiaddr) (transport.CapableConn, error)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func newDialLimiter(df dialfunc) *dialLimiter {
|
2019-06-09 07:24:20 +00:00
|
|
|
fd := ConcurrentFdDials
|
|
|
|
if env := os.Getenv("LIBP2P_SWARM_FD_LIMIT"); env != "" {
|
|
|
|
if n, err := strconv.ParseInt(env, 10, 32); err == nil {
|
|
|
|
fd = int(n)
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
return newDialLimiterWithParams(df, fd, DefaultPerPeerRateLimit)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimiter {
|
2018-07-04 10:51:47 +00:00
|
|
|
return &dialLimiter{
|
|
|
|
fdLimit: fdLimit,
|
|
|
|
perPeerLimit: perPeerLimit,
|
|
|
|
waitingOnPeerLimit: make(map[peer.ID][]*dialJob),
|
|
|
|
activePerPeer: make(map[peer.ID]int),
|
|
|
|
dialFunc: df,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// freeFDToken frees FD token and if there are any schedules another waiting dialJob
|
|
|
|
// in it's place
|
|
|
|
func (dl *dialLimiter) freeFDToken() {
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] freeing FD token; waiting: %d; consuming: %d", len(dl.waitingOnFd), dl.fdConsuming)
|
2018-07-04 10:51:47 +00:00
|
|
|
dl.fdConsuming--
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
for len(dl.waitingOnFd) > 0 {
|
2018-07-04 10:51:47 +00:00
|
|
|
next := dl.waitingOnFd[0]
|
|
|
|
dl.waitingOnFd[0] = nil // clear out memory
|
|
|
|
dl.waitingOnFd = dl.waitingOnFd[1:]
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
if len(dl.waitingOnFd) == 0 {
|
2019-06-09 07:24:20 +00:00
|
|
|
// clear out memory.
|
|
|
|
dl.waitingOnFd = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over canceled dials instead of queuing up a goroutine.
|
|
|
|
if next.cancelled() {
|
|
|
|
dl.freePeerToken(next)
|
|
|
|
continue
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
dl.fdConsuming++
|
|
|
|
|
|
|
|
// we already have activePerPeer token at this point so we can just dial
|
|
|
|
go dl.executeDial(next)
|
2019-06-09 07:24:20 +00:00
|
|
|
return
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *dialLimiter) freePeerToken(dj *dialJob) {
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] freeing peer token; peer %s; addr: %s; active for peer: %d; waiting on peer limit: %d",
|
|
|
|
dj.peer, dj.addr, dl.activePerPeer[dj.peer], len(dl.waitingOnPeerLimit[dj.peer]))
|
2018-07-04 10:51:47 +00:00
|
|
|
// release tokens in reverse order than we take them
|
|
|
|
dl.activePerPeer[dj.peer]--
|
|
|
|
if dl.activePerPeer[dj.peer] == 0 {
|
|
|
|
delete(dl.activePerPeer, dj.peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
waitlist := dl.waitingOnPeerLimit[dj.peer]
|
2019-06-09 07:24:20 +00:00
|
|
|
for len(waitlist) > 0 {
|
2018-07-04 10:51:47 +00:00
|
|
|
next := waitlist[0]
|
2019-06-09 07:24:20 +00:00
|
|
|
waitlist[0] = nil // clear out memory
|
|
|
|
waitlist = waitlist[1:]
|
|
|
|
|
|
|
|
if len(waitlist) == 0 {
|
2018-07-04 10:51:47 +00:00
|
|
|
delete(dl.waitingOnPeerLimit, next.peer)
|
|
|
|
} else {
|
2019-06-09 07:24:20 +00:00
|
|
|
dl.waitingOnPeerLimit[next.peer] = waitlist
|
|
|
|
}
|
|
|
|
|
|
|
|
if next.cancelled() {
|
|
|
|
continue
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dl.activePerPeer[next.peer]++ // just kidding, we still want this token
|
|
|
|
|
|
|
|
dl.addCheckFdLimit(next)
|
2019-06-09 07:24:20 +00:00
|
|
|
return
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *dialLimiter) finishedDial(dj *dialJob) {
|
|
|
|
dl.lk.Lock()
|
|
|
|
defer dl.lk.Unlock()
|
2021-06-16 20:19:45 +00:00
|
|
|
if dl.shouldConsumeFd(dj.addr) {
|
2018-07-04 10:51:47 +00:00
|
|
|
dl.freeFDToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
dl.freePeerToken(dj)
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func (dl *dialLimiter) shouldConsumeFd(addr ma.Multiaddr) bool {
|
|
|
|
// we don't consume FD's for relay addresses for now as they will be consumed when the Relay Transport
|
|
|
|
// actually dials the Relay server. That dial call will also pass through this limiter with
|
|
|
|
// the address of the relay server i.e. non-relay address.
|
|
|
|
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
|
|
|
|
|
|
|
|
isRelay := err == nil
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
return !isRelay && isFdConsumingAddr(addr)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
func (dl *dialLimiter) addCheckFdLimit(dj *dialJob) {
|
2021-06-16 20:19:45 +00:00
|
|
|
if dl.shouldConsumeFd(dj.addr) {
|
2018-07-04 10:51:47 +00:00
|
|
|
if dl.fdConsuming >= dl.fdLimit {
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] blocked dial waiting on FD token; peer: %s; addr: %s; consuming: %d; "+
|
|
|
|
"limit: %d; waiting: %d", dj.peer, dj.addr, dl.fdConsuming, dl.fdLimit, len(dl.waitingOnFd))
|
2018-07-04 10:51:47 +00:00
|
|
|
dl.waitingOnFd = append(dl.waitingOnFd, dj)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] taking FD token: peer: %s; addr: %s; prev consuming: %d",
|
|
|
|
dj.peer, dj.addr, dl.fdConsuming)
|
2018-07-04 10:51:47 +00:00
|
|
|
// take token
|
|
|
|
dl.fdConsuming++
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] executing dial; peer: %s; addr: %s; FD consuming: %d; waiting: %d",
|
|
|
|
dj.peer, dj.addr, dl.fdConsuming, len(dl.waitingOnFd))
|
2018-07-04 10:51:47 +00:00
|
|
|
go dl.executeDial(dj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *dialLimiter) addCheckPeerLimit(dj *dialJob) {
|
|
|
|
if dl.activePerPeer[dj.peer] >= dl.perPeerLimit {
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] blocked dial waiting on peer limit; peer: %s; addr: %s; active: %d; "+
|
|
|
|
"peer limit: %d; waiting: %d", dj.peer, dj.addr, dl.activePerPeer[dj.peer], dl.perPeerLimit,
|
|
|
|
len(dl.waitingOnPeerLimit[dj.peer]))
|
2018-07-04 10:51:47 +00:00
|
|
|
wlist := dl.waitingOnPeerLimit[dj.peer]
|
|
|
|
dl.waitingOnPeerLimit[dj.peer] = append(wlist, dj)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dl.activePerPeer[dj.peer]++
|
|
|
|
|
|
|
|
dl.addCheckFdLimit(dj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddDialJob tries to take the needed tokens for starting the given dial job.
|
|
|
|
// If it acquires all needed tokens, it immediately starts the dial, otherwise
|
|
|
|
// it will put it on the waitlist for the requested token.
|
|
|
|
func (dl *dialLimiter) AddDialJob(dj *dialJob) {
|
|
|
|
dl.lk.Lock()
|
|
|
|
defer dl.lk.Unlock()
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] adding a dial job through limiter: %v", dj.addr)
|
2018-07-04 10:51:47 +00:00
|
|
|
dl.addCheckPeerLimit(dj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *dialLimiter) clearAllPeerDials(p peer.ID) {
|
|
|
|
dl.lk.Lock()
|
|
|
|
defer dl.lk.Unlock()
|
|
|
|
delete(dl.waitingOnPeerLimit, p)
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Debugf("[limiter] clearing all peer dials: %v", p)
|
2018-07-04 10:51:47 +00:00
|
|
|
// NB: the waitingOnFd list doesn't need to be cleaned out here, we will
|
|
|
|
// remove them as we encounter them because they are 'cancelled' at this
|
|
|
|
// point
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeDial calls the dialFunc, and reports the result through the response
|
|
|
|
// channel when finished. Once the response is sent it also releases all tokens
|
|
|
|
// it held during the dial.
|
|
|
|
func (dl *dialLimiter) executeDial(j *dialJob) {
|
|
|
|
defer dl.finishedDial(j)
|
|
|
|
if j.cancelled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
dctx, cancel := context.WithTimeout(j.ctx, j.timeout)
|
2018-07-04 10:51:47 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
con, err := dl.dialFunc(dctx, j.peer, j.addr)
|
|
|
|
select {
|
|
|
|
case j.resp <- dialResult{Conn: con, Addr: j.addr, Err: err}:
|
|
|
|
case <-j.ctx.Done():
|
2022-04-01 16:16:46 +00:00
|
|
|
if con != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
con.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|