-
Notifications
You must be signed in to change notification settings - Fork 4
/
stream_manager.go
92 lines (79 loc) · 2.15 KB
/
stream_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package network
import (
"context"
"sync"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
newStreamTimeout = 5 * time.Second
maxStreamNumPerConn = 16
)
type streamMgr struct {
ctx context.Context
protocolID protocol.ID
host host.Host
logger logrus.FieldLogger
timeout *timeout
pools sync.Map
}
func newStreamMng(ctx context.Context, host host.Host, protocolID protocol.ID, logger logrus.FieldLogger, timeout *timeout) *streamMgr {
return &streamMgr{
ctx: ctx,
protocolID: protocolID,
host: host,
logger: logger,
timeout: timeout,
pools: sync.Map{},
}
}
func (mng *streamMgr) get(peerID string) (*stream, error) {
loadPool, ok := mng.pools.Load(peerID)
if !ok {
pool, err := newPool(mng.newStream, mng.logger, maxStreamNumPerConn)
if err != nil {
return nil, errors.Wrap(err, "failed on create new pool")
}
mng.pools.Store(peerID, pool)
loadPool = pool
}
s, err := loadPool.(*pool).acquire(peerID)
if err != nil {
return nil, errors.Wrap(err, "failed on acquire stream")
}
return s, nil
}
func (mng *streamMgr) release(stream *stream) {
peerID := stream.RemotePeerID()
loadPool, ok := mng.pools.Load(peerID)
if !ok {
mng.logger.WithFields(logrus.Fields{"peer id": peerID, "err": "failed on get pool"}).Warn("failed on release stream")
return
}
loadPool.(*pool).release(stream)
}
func (mng *streamMgr) newStream(peerID string) (*stream, error) {
pid, err := peer.Decode(peerID)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(mng.ctx, newStreamTimeout)
defer cancel()
mng.logger.WithFields(logrus.Fields{"protocol id": mng.protocolID}).Debug("new stream")
s, err := mng.host.NewStream(ctx, pid, mng.protocolID)
if err != nil {
return nil, errors.Wrap(err, "failed on creat new stream")
}
return newStream(s, mng.protocolID, DirOutbound, mng.timeout), nil
}
func (mng *streamMgr) stop() {
mng.pools.Range(func(key, value interface{}) bool {
pool := value.(*pool)
pool.close()
return true
})
}