Skip to content

Commit 49ba4a0

Browse files
authored
Merge pull request #113 from cloudstruct/feature/functional-options
feat: use functional options pattern for config
2 parents 046d70d + a644b5b commit 49ba4a0

File tree

6 files changed

+83
-47
lines changed

6 files changed

+83
-47
lines changed

cmd/go-ouroboros-network/chainsync.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,20 @@ func testChainSync(f *globalFlags) {
111111

112112
conn := createClientConnection(f)
113113
errorChan := make(chan error)
114-
oOpts := &ouroboros.OuroborosOptions{
115-
Conn: conn,
116-
NetworkMagic: uint32(f.networkMagic),
117-
ErrorChan: errorChan,
118-
UseNodeToNodeProtocol: f.ntnProto,
119-
SendKeepAlives: true,
120-
}
121114
go func() {
122115
for {
123116
err := <-errorChan
124117
fmt.Printf("ERROR: %s\n", err)
125118
os.Exit(1)
126119
}
127120
}()
128-
o, err := ouroboros.New(oOpts)
121+
o, err := ouroboros.New(
122+
ouroboros.WithConnection(conn),
123+
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
124+
ouroboros.WithErrorChan(errorChan),
125+
ouroboros.WithNodeToNode(f.ntnProto),
126+
ouroboros.WithKeepAlive(true),
127+
)
129128
if err != nil {
130129
fmt.Printf("ERROR: %s\n", err)
131130
os.Exit(1)

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,20 @@ func testLocalTxSubmission(f *globalFlags) {
5050

5151
conn := createClientConnection(f)
5252
errorChan := make(chan error)
53-
oOpts := &ouroboros.OuroborosOptions{
54-
Conn: conn,
55-
NetworkMagic: uint32(f.networkMagic),
56-
ErrorChan: errorChan,
57-
UseNodeToNodeProtocol: f.ntnProto,
58-
SendKeepAlives: true,
59-
}
6053
go func() {
6154
for {
6255
err := <-errorChan
6356
fmt.Printf("ERROR: %s\n", err)
6457
os.Exit(1)
6558
}
6659
}()
67-
o, err := ouroboros.New(oOpts)
60+
o, err := ouroboros.New(
61+
ouroboros.WithConnection(conn),
62+
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
63+
ouroboros.WithErrorChan(errorChan),
64+
ouroboros.WithNodeToNode(f.ntnProto),
65+
ouroboros.WithKeepAlive(true),
66+
)
6867
if err != nil {
6968
fmt.Printf("ERROR: %s\n", err)
7069
os.Exit(1)

cmd/go-ouroboros-network/server.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,19 @@ func testServer(f *globalFlags) {
6262
continue
6363
}
6464
errorChan := make(chan error)
65-
oOpts := &ouroboros.OuroborosOptions{
66-
Conn: conn,
67-
NetworkMagic: uint32(f.networkMagic),
68-
ErrorChan: errorChan,
69-
UseNodeToNodeProtocol: f.ntnProto,
70-
Server: true,
71-
}
7265
go func() {
7366
for {
7467
err := <-errorChan
7568
fmt.Printf("ERROR: %s\n", err)
7669
}
7770
}()
78-
_, err = ouroboros.New(oOpts)
71+
_, err = ouroboros.New(
72+
ouroboros.WithConnection(conn),
73+
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
74+
ouroboros.WithErrorChan(errorChan),
75+
ouroboros.WithNodeToNode(f.ntnProto),
76+
ouroboros.WithServer(true),
77+
)
7978
if err != nil {
8079
fmt.Printf("ERROR: %s\n", err)
8180
}

options.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ouroboros
2+
3+
import (
4+
"net"
5+
)
6+
7+
type OuroborosOptionFunc func(*Ouroboros)
8+
9+
func WithConnection(conn net.Conn) OuroborosOptionFunc {
10+
return func(o *Ouroboros) {
11+
o.conn = conn
12+
}
13+
}
14+
15+
func WithNetworkMagic(networkMagic uint32) OuroborosOptionFunc {
16+
return func(o *Ouroboros) {
17+
o.networkMagic = networkMagic
18+
}
19+
}
20+
21+
func WithErrorChan(errorChan chan error) OuroborosOptionFunc {
22+
return func(o *Ouroboros) {
23+
o.ErrorChan = errorChan
24+
}
25+
}
26+
27+
func WithServer(server bool) OuroborosOptionFunc {
28+
return func(o *Ouroboros) {
29+
o.server = server
30+
}
31+
}
32+
33+
func WithNodeToNode(nodeToNode bool) OuroborosOptionFunc {
34+
return func(o *Ouroboros) {
35+
o.useNodeToNodeProto = nodeToNode
36+
}
37+
}
38+
39+
func WithKeepAlive(keepAlive bool) OuroborosOptionFunc {
40+
return func(o *Ouroboros) {
41+
o.sendKeepAlives = keepAlive
42+
}
43+
}
44+
45+
func WithDelayMuxerStart(delayMuxerStart bool) OuroborosOptionFunc {
46+
return func(o *Ouroboros) {
47+
o.delayMuxerStart = delayMuxerStart
48+
}
49+
}
50+
51+
func WithFullDuplex(fullDuplex bool) OuroborosOptionFunc {
52+
return func(o *Ouroboros) {
53+
o.fullDuplex = fullDuplex
54+
}
55+
}

ouroboros.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,13 @@ type Ouroboros struct {
3636
TxSubmission *txsubmission.TxSubmission
3737
}
3838

39-
type OuroborosOptions struct {
40-
Conn net.Conn
41-
NetworkMagic uint32
42-
ErrorChan chan error
43-
Server bool
44-
UseNodeToNodeProtocol bool
45-
SendKeepAlives bool
46-
DelayMuxerStart bool
47-
FullDuplex bool
48-
}
49-
50-
func New(options *OuroborosOptions) (*Ouroboros, error) {
39+
func New(options ...OuroborosOptionFunc) (*Ouroboros, error) {
5140
o := &Ouroboros{
52-
conn: options.Conn,
53-
networkMagic: options.NetworkMagic,
54-
server: options.Server,
55-
useNodeToNodeProto: options.UseNodeToNodeProtocol,
56-
ErrorChan: options.ErrorChan,
57-
sendKeepAlives: options.SendKeepAlives,
58-
delayMuxerStart: options.DelayMuxerStart,
59-
fullDuplex: options.FullDuplex,
60-
protoErrorChan: make(chan error, 10),
41+
protoErrorChan: make(chan error, 10),
42+
}
43+
// Apply provided options functions
44+
for _, option := range options {
45+
option(o)
6146
}
6247
if o.ErrorChan == nil {
6348
o.ErrorChan = make(chan error, 10)

ouroboros_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77

88
// Ensure that we don't panic when closing the Ouroboros object after a failed Dial() call
99
func TestDialFailClose(t *testing.T) {
10-
oOpts := &ouroboros.OuroborosOptions{}
11-
oConn, err := ouroboros.New(oOpts)
10+
oConn, err := ouroboros.New()
1211
if err != nil {
1312
t.Fatalf("unexpected error when creating Ouroboros object: %s", err)
1413
}

0 commit comments

Comments
 (0)