@@ -3,6 +3,7 @@ package protocol
3
3
import (
4
4
"context"
5
5
"crypto/rand"
6
+ "encoding/json"
6
7
mrand "math/rand"
7
8
"ocf/internal/common"
8
9
"strconv"
@@ -21,6 +22,7 @@ import (
21
22
"github.com/libp2p/go-libp2p/core/peer"
22
23
"github.com/libp2p/go-libp2p/core/routing"
23
24
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
25
+ connmgr "github.com/libp2p/go-libp2p/p2p/net/connmgr"
24
26
"github.com/libp2p/go-libp2p/p2p/security/noise"
25
27
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
26
28
"github.com/spf13/viper"
@@ -52,15 +54,17 @@ func GetP2PNode(ds datastore.Batching) (host.Host, dualdht.DHT) {
52
54
}
53
55
54
56
func newHost (ctx context.Context , seed int64 , ds datastore.Batching ) (host.Host , error ) {
55
- // connmgr, err := connmgr.NewConnManager(
56
- // 50,
57
- // 500,
58
- // connmgr.WithGracePeriod(time.Minute),
59
- // )
60
- // if err != nil {
61
- // common.Logger.Error("Error while creating connection manager: %v", err)
62
- // }
63
57
var err error
58
+ // Connection manager: maintain a larger pool of connections so we can exceed
59
+ // the pubsub mesh degree and keep more peers around.
60
+ cm , err := connmgr .NewConnManager (
61
+ 100 , // Low watermark
62
+ 800 , // High watermark
63
+ connmgr .WithGracePeriod (5 * time .Minute ),
64
+ )
65
+ if err != nil {
66
+ common .Logger .Error ("Error while creating connection manager: " , err )
67
+ }
64
68
var priv crypto.PrivKey
65
69
// try to load the private key from file
66
70
if seed == 0 {
@@ -103,9 +107,9 @@ func newHost(ctx context.Context, seed int64, ds datastore.Batching) (host.Host,
103
107
System : systemLimits ,
104
108
// Keep default peer limits but increase them slightly
105
109
PeerDefault : rcmgr.ResourceLimits {
106
- ConnsInbound : 16 , // Allow more connections per peer
107
- ConnsOutbound : 16 ,
108
- Conns : 32 ,
110
+ ConnsInbound : 512 , // Allow more connections per peer
111
+ ConnsOutbound : 512 ,
112
+ Conns : 1024 ,
109
113
},
110
114
}.Build (limits )
111
115
@@ -119,7 +123,7 @@ func newHost(ctx context.Context, seed int64, ds datastore.Batching) (host.Host,
119
123
libp2p .DefaultTransports ,
120
124
libp2p .Identity (priv ),
121
125
libp2p .ResourceManager (mgr ), // Use our custom resource manager
122
- // libp2p.ConnectionManager(connmgr ),
126
+ libp2p .ConnectionManager (cm ),
123
127
libp2p .NATPortMap (),
124
128
libp2p .ListenAddrStrings (
125
129
"/ip4/0.0.0.0/tcp/" + viper .GetString ("tcpport" ),
@@ -149,9 +153,45 @@ func newHost(ctx context.Context, seed int64, ds datastore.Batching) (host.Host,
149
153
common .Logger .Info ("Connected to peer: " , c .RemotePeer (), " Total connections: " , len (n .Conns ()))
150
154
// On (re)connections, re-announce local services
151
155
go ReannounceLocalServices ()
156
+
157
+ // Mark peer as connected in node table immediately
158
+ go func (pid peer.ID ) {
159
+ // Avoid updating self
160
+ if pid == host .ID () {
161
+ return
162
+ }
163
+ p , err := GetPeerFromTable (pid .String ())
164
+ if err != nil {
165
+ p = Peer {ID : pid .String ()}
166
+ }
167
+ p .Connected = true
168
+ p .LastSeen = time .Now ().Unix ()
169
+ if b , e := json .Marshal (p ); e == nil {
170
+ UpdateNodeTableHook (datastore .NewKey (pid .String ()), b )
171
+ } else {
172
+ common .Logger .Error ("Failed to marshal peer on connect: " , e )
173
+ }
174
+ }(c .RemotePeer ())
152
175
},
153
176
DisconnectedF : func (n network.Network , c network.Conn ) {
154
177
common .Logger .Info ("Disconnected from peer: " , c .RemotePeer (), " Total connections: " , len (n .Conns ()))
178
+ // Mark peer as disconnected in node table immediately
179
+ go func (pid peer.ID ) {
180
+ if pid == host .ID () {
181
+ return
182
+ }
183
+ p , err := GetPeerFromTable (pid .String ())
184
+ if err != nil {
185
+ p = Peer {ID : pid .String ()}
186
+ }
187
+ p .Connected = false
188
+ // keep LastSeen as last known good; do not bump here
189
+ if b , e := json .Marshal (p ); e == nil {
190
+ UpdateNodeTableHook (datastore .NewKey (pid .String ()), b )
191
+ } else {
192
+ common .Logger .Error ("Failed to marshal peer on disconnect: " , e )
193
+ }
194
+ }(c .RemotePeer ())
155
195
},
156
196
})
157
197
@@ -247,6 +287,11 @@ func ConnectedBootstraps() []string {
247
287
}
248
288
}
249
289
}
290
+ // add myself as bootstrap
291
+ myaddr := host .Addrs ()[0 ].String () + "/p2p/" + host .ID ().String ()
292
+ bootstraps = append (bootstraps , myaddr )
293
+ // deduplicate
294
+ bootstraps = common .DeduplicateStrings (bootstraps )
250
295
return bootstraps
251
296
}
252
297
0 commit comments