Skip to content

Commit a878eeb

Browse files
authored
Merge pull request #281 from blinklabs-io/feat/protocol-auto-start
feat: auto-start protocols with option to disable
2 parents 9c83d3a + 2c01208 commit a878eeb

File tree

9 files changed

+59
-15
lines changed

9 files changed

+59
-15
lines changed

cmd/block-fetch/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121

22-
"github.com/blinklabs-io/gouroboros"
22+
ouroboros "github.com/blinklabs-io/gouroboros"
2323
"github.com/blinklabs-io/gouroboros/cmd/common"
2424
"github.com/blinklabs-io/gouroboros/ledger"
2525
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
@@ -60,7 +60,6 @@ func main() {
6060
fmt.Printf("ERROR: %s\n", err)
6161
os.Exit(1)
6262
}
63-
o.BlockFetch().Client.Start()
6463

6564
blockHash, err := hex.DecodeString(f.hash)
6665
if err != nil {

cmd/gouroboros/chainsync.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ func testChainSync(f *globalFlags) {
151151
fmt.Printf("ERROR: %s\n", err)
152152
os.Exit(1)
153153
}
154-
oConn.ChainSync().Client.Start()
155-
if f.ntnProto {
156-
oConn.BlockFetch().Client.Start()
157-
}
158154

159155
var point common.Point
160156
if chainSyncFlags.tip {

cmd/gouroboros/localtxsubmission.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919
"encoding/json"
2020
"flag"
2121
"fmt"
22+
"io/ioutil"
23+
"os"
24+
2225
ouroboros "github.com/blinklabs-io/gouroboros"
2326
"github.com/blinklabs-io/gouroboros/ledger"
2427
"github.com/blinklabs-io/gouroboros/protocol/localtxsubmission"
25-
"io/ioutil"
26-
"os"
2728
)
2829

2930
type localTxSubmissionFlags struct {
@@ -78,7 +79,6 @@ func testLocalTxSubmission(f *globalFlags) {
7879
fmt.Printf("ERROR: %s\n", err)
7980
os.Exit(1)
8081
}
81-
o.LocalTxSubmission().Client.Start()
8282

8383
var txBytes []byte
8484
if localTxSubmissionFlags.txFile != "" {

cmd/gouroboros/mem_usage.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ func testMemUsage(f *globalFlags) {
8585
fmt.Printf("ERROR: %s\n", err)
8686
os.Exit(1)
8787
}
88-
o.ChainSync().Client.Start()
8988

9089
tip, err := o.ChainSync().Client.GetCurrentTip()
9190
if err != nil {

cmd/gouroboros/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ package main
1717
import (
1818
"flag"
1919
"fmt"
20+
"os"
21+
2022
ouroboros "github.com/blinklabs-io/gouroboros"
2123
"github.com/blinklabs-io/gouroboros/protocol/localstatequery"
22-
"os"
2324
)
2425

2526
type queryFlags struct {
@@ -70,7 +71,6 @@ func testQuery(f *globalFlags) {
7071
fmt.Printf("ERROR: %s\n", err)
7172
os.Exit(1)
7273
}
73-
o.LocalStateQuery().Client.Start()
7474

7575
switch queryFlags.flagset.Args()[0] {
7676
case "current-era":

cmd/peer-sharing/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func main() {
4747
fmt.Printf("ERROR: %s\n", err)
4848
os.Exit(1)
4949
}
50-
o.PeerSharing().Client.Start()
5150

5251
peers, err := o.PeerSharing().Client.GetPeers(10)
5352
if err != nil {

cmd/tx-monitor/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121

22-
"github.com/blinklabs-io/gouroboros"
22+
ouroboros "github.com/blinklabs-io/gouroboros"
2323
"github.com/blinklabs-io/gouroboros/cbor"
2424
"github.com/blinklabs-io/gouroboros/cmd/common"
2525

@@ -51,7 +51,6 @@ func main() {
5151
fmt.Printf("ERROR: %s\n", err)
5252
os.Exit(1)
5353
}
54-
o.LocalTxMonitor().Client.Start()
5554

5655
capacity, size, numberOfTxs, err := o.LocalTxMonitor().Client.GetSizes()
5756
if err != nil {

connection.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Connection struct {
5858
onceClose sync.Once
5959
sendKeepAlives bool
6060
delayMuxerStart bool
61+
delayProtocolStart bool
6162
fullDuplex bool
6263
// Mini-protocols
6364
blockFetch *blockfetch.BlockFetch
@@ -331,6 +332,25 @@ func (c *Connection) setupConnection() error {
331332
if versionNtN.EnablePeerSharingProtocol {
332333
c.peerSharing = peersharing.New(protoOptions, c.peerSharingConfig)
333334
}
335+
// Start protocols
336+
if !c.delayProtocolStart {
337+
if handshakeFullDuplex || !c.server {
338+
c.blockFetch.Client.Start()
339+
c.chainSync.Client.Start()
340+
c.txSubmission.Client.Start()
341+
if c.peerSharing != nil {
342+
c.peerSharing.Client.Start()
343+
}
344+
}
345+
if handshakeFullDuplex || c.server {
346+
c.blockFetch.Server.Start()
347+
c.chainSync.Server.Start()
348+
c.txSubmission.Server.Start()
349+
if c.peerSharing != nil {
350+
c.peerSharing.Server.Start()
351+
}
352+
}
353+
}
334354
} else {
335355
versionNtC := GetProtocolVersionNtC(handshakeVersion)
336356
protoOptions.Mode = protocol.ProtocolModeNodeToClient
@@ -342,6 +362,29 @@ func (c *Connection) setupConnection() error {
342362
if versionNtC.EnableLocalTxMonitorProtocol {
343363
c.localTxMonitor = localtxmonitor.New(protoOptions, c.localTxMonitorConfig)
344364
}
365+
// Start protocols
366+
if !c.delayProtocolStart {
367+
if handshakeFullDuplex || !c.server {
368+
c.chainSync.Client.Start()
369+
c.localTxSubmission.Client.Start()
370+
if c.localStateQuery != nil {
371+
c.localStateQuery.Client.Start()
372+
}
373+
if c.localTxMonitor != nil {
374+
c.localTxMonitor.Client.Start()
375+
}
376+
}
377+
if handshakeFullDuplex || c.server {
378+
c.chainSync.Server.Start()
379+
c.localTxSubmission.Server.Start()
380+
if c.localStateQuery != nil {
381+
c.localStateQuery.Server.Start()
382+
}
383+
if c.localTxMonitor != nil {
384+
c.localTxMonitor.Server.Start()
385+
}
386+
}
387+
}
345388
}
346389
// Start muxer
347390
diffusionMode := muxer.DiffusionModeInitiator

connection_options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ func WithDelayMuxerStart(delayMuxerStart bool) ConnectionOptionFunc {
8787
}
8888
}
8989

90+
// WithDelayProtocolStart specifies whether to delay the start of the relevant mini-protocols. This is useful
91+
// if you are maintaining lots of connections and want to reduce resource overhead by only starting particular
92+
// protocols
93+
func WithDelayProtocolStart(delayProtocolStart bool) ConnectionOptionFunc {
94+
return func(c *Connection) {
95+
c.delayProtocolStart = delayProtocolStart
96+
}
97+
}
98+
9099
// WithFullDuplex specifies whether to enable full-duplex mode when acting as a client
91100
func WithFullDuplex(fullDuplex bool) ConnectionOptionFunc {
92101
return func(c *Connection) {

0 commit comments

Comments
 (0)