@@ -28,13 +28,12 @@ import (
2828
2929const meshThreadInterval = time .Minute
3030
31- type meshStrategy interface {
32- meshThread ()
31+ type mesher interface {
3332 start ()
3433 stop ()
3534}
3635
37- type baseMeshStrategy struct {
36+ type baseMesher struct {
3837 wg sync.WaitGroup
3938 meshConfig
4039}
@@ -44,18 +43,18 @@ type meshConfig struct {
4443 meshUpdateRequests chan meshRequest
4544 meshThreadInterval time.Duration
4645 backoff backoff.BackoffStrategy
47- netMesh func () bool
48- peerStatReport func ()
46+ netMeshFn func () bool
47+ peerStatReporter func ()
4948 closer func ()
5049
5150 // wsnet and p2pnet are used in hybrid relay mode
5251 wsnet * WebsocketNetwork
5352 p2pnet * P2PNetwork
5453}
5554
56- type meshStrategyOption func (* meshConfig )
55+ type meshOption func (* meshConfig )
5756
58- func withMeshExpJitterBackoff () meshStrategyOption {
57+ func withMeshExpJitterBackoff () meshOption {
5958 return func (cfg * meshConfig ) {
6059 // Add exponential backoff with jitter to the mesh thread to handle new networks startup
6160 // when no DNS or DHT peers are available.
@@ -66,61 +65,61 @@ func withMeshExpJitterBackoff() meshStrategyOption {
6665 cfg .backoff = eb
6766 }
6867}
69- func withMeshNetMesh ( netMesh func () bool ) meshStrategyOption {
68+ func withMeshNetMeshFn ( netMeshFn func () bool ) meshOption {
7069 return func (cfg * meshConfig ) {
71- cfg .netMesh = netMesh
70+ cfg .netMeshFn = netMeshFn
7271 }
7372}
74- func withMeshPeerStatReport ( peerStatReport func ()) meshStrategyOption {
73+ func withMeshPeerStatReporter ( peerStatReporter func ()) meshOption {
7574 return func (cfg * meshConfig ) {
76- cfg .peerStatReport = peerStatReport
75+ cfg .peerStatReporter = peerStatReporter
7776 }
7877}
79- func withMeshCloser (closer func ()) meshStrategyOption {
78+ func withMeshCloser (closer func ()) meshOption {
8079 return func (cfg * meshConfig ) {
8180 cfg .closer = closer
8281 }
8382}
8483
85- func withMeshUpdateRequest (ch chan meshRequest ) meshStrategyOption {
84+ func withMeshUpdateRequest (ch chan meshRequest ) meshOption {
8685 return func (cfg * meshConfig ) {
8786 cfg .meshUpdateRequests = ch
8887 }
8988}
9089
91- func withMeshUpdateInterval (d time.Duration ) meshStrategyOption {
90+ func withMeshUpdateInterval (d time.Duration ) meshOption {
9291 return func (cfg * meshConfig ) {
9392 cfg .meshThreadInterval = d
9493 }
9594}
9695
97- func withContext (ctx context.Context ) meshStrategyOption {
96+ func withContext (ctx context.Context ) meshOption {
9897 return func (cfg * meshConfig ) {
9998 cfg .ctx = ctx
10099 }
101100}
102101
103- func withWebsocketNetwork (wsnet * WebsocketNetwork ) meshStrategyOption {
102+ func withWebsocketNetwork (wsnet * WebsocketNetwork ) meshOption {
104103 return func (cfg * meshConfig ) {
105104 cfg .wsnet = wsnet
106105 }
107106}
108107
109- func withP2PNetwork (p2pnet * P2PNetwork ) meshStrategyOption {
108+ func withP2PNetwork (p2pnet * P2PNetwork ) meshOption {
110109 return func (cfg * meshConfig ) {
111110 cfg .p2pnet = p2pnet
112111 }
113112}
114113
115- func newBaseMeshStrategy (opts ... meshStrategyOption ) (* baseMeshStrategy , error ) {
114+ func newBaseMesher (opts ... meshOption ) (* baseMesher , error ) {
116115 var cfg meshConfig
117116 for _ , opt := range opts {
118117 opt (& cfg )
119118 }
120119 if cfg .ctx == nil {
121120 return nil , errors .New ("context is not set" )
122121 }
123- if cfg .netMesh == nil {
122+ if cfg .netMeshFn == nil {
124123 return nil , errors .New ("mesh function is not set" )
125124 }
126125 if cfg .meshUpdateRequests == nil {
@@ -130,12 +129,12 @@ func newBaseMeshStrategy(opts ...meshStrategyOption) (*baseMeshStrategy, error)
130129 cfg .meshThreadInterval = meshThreadInterval
131130 }
132131
133- return & baseMeshStrategy {
132+ return & baseMesher {
134133 meshConfig : cfg ,
135134 }, nil
136135}
137136
138- func (m * baseMeshStrategy ) meshThread () {
137+ func (m * baseMesher ) meshThread () {
139138 defer m .wg .Done ()
140139
141140 timer := time .NewTicker (m .meshThreadInterval )
@@ -150,7 +149,7 @@ func (m *baseMeshStrategy) meshThread() {
150149 return
151150 }
152151
153- hasPeers := m .netMesh ()
152+ hasPeers := m .netMeshFn ()
154153 if m .backoff != nil {
155154 if hasPeers {
156155 // found something, reset timer to the configured value
@@ -168,41 +167,41 @@ func (m *baseMeshStrategy) meshThread() {
168167 // send the currently connected peers information to the
169168 // telemetry server; that would allow the telemetry server
170169 // to construct a cross-node map of all the nodes interconnections.
171- m .peerStatReport ()
170+ m .peerStatReporter ()
172171 }
173172}
174173
175- func (m * baseMeshStrategy ) start () {
174+ func (m * baseMesher ) start () {
176175 m .wg .Add (1 )
177176 go m .meshThread ()
178177}
179178
180- func (m * baseMeshStrategy ) stop () {
179+ func (m * baseMesher ) stop () {
181180 m .wg .Wait ()
182181 if m .closer != nil {
183182 m .closer ()
184183 }
185184}
186185
187- // MeshStrategyCreator is an interface for creating mesh strategies.
188- type MeshStrategyCreator interface {
189- create (opts ... meshStrategyOption ) (meshStrategy , error )
186+ // MeshCreator is an interface for creating mesh strategies.
187+ type MeshCreator interface {
188+ create (opts ... meshOption ) (mesher , error )
190189}
191190
192- // BaseMeshStrategyCreator is a creator for the base mesh strategy used in our standard WS or P2P implementations:
191+ // BaseMeshCreator is a creator for the base mesh strategy used in our standard WS or P2P implementations:
193192// run a mesh thread that periodically checks for new peers.
194- type BaseMeshStrategyCreator struct {
193+ type BaseMeshCreator struct {
195194}
196195
197- func (c * BaseMeshStrategyCreator ) create (opts ... meshStrategyOption ) (meshStrategy , error ) {
198- return newBaseMeshStrategy (opts ... )
196+ func (c * BaseMeshCreator ) create (opts ... meshOption ) (mesher , error ) {
197+ return newBaseMesher (opts ... )
199198}
200199
201- // HybridRelayMeshStrategyCreator is a creator for the hybrid relay mesh strategy used in hybrid relays:
200+ // HybridRelayMeshCreator is a creator for the hybrid relay mesh strategy used in hybrid relays:
202201// always use wsnet nodes
203- type HybridRelayMeshStrategyCreator struct {}
202+ type HybridRelayMeshCreator struct {}
204203
205- func (c * HybridRelayMeshStrategyCreator ) create (opts ... meshStrategyOption ) (meshStrategy , error ) {
204+ func (c * HybridRelayMeshCreator ) create (opts ... meshOption ) (mesher , error ) {
206205 var cfg meshConfig
207206 for _ , opt := range opts {
208207 opt (& cfg )
@@ -215,12 +214,12 @@ func (c *HybridRelayMeshStrategyCreator) create(opts ...meshStrategyOption) (mes
215214 out := make (chan meshRequest , 5 )
216215 var wg sync.WaitGroup
217216
218- creator := BaseMeshStrategyCreator {}
217+ creator := BaseMeshCreator {}
219218 ctx := cfg .wsnet .ctx
220- strategy , err := creator .create (
219+ mesh , err := creator .create (
221220 withContext (ctx ),
222- withMeshNetMesh (cfg .wsnet .meshThreadInner ),
223- withMeshPeerStatReport (func () {
221+ withMeshNetMeshFn (cfg .wsnet .meshThreadInner ),
222+ withMeshPeerStatReporter (func () {
224223 cfg .p2pnet .peerStater .sendPeerConnectionsTelemetryStatus (cfg .wsnet )
225224 cfg .p2pnet .peerStater .sendPeerConnectionsTelemetryStatus (cfg .p2pnet )
226225 }),
@@ -256,20 +255,18 @@ func (c *HybridRelayMeshStrategyCreator) create(opts ...meshStrategyOption) (mes
256255 }
257256 }()
258257
259- return strategy , nil
258+ return mesh , nil
260259}
261260
262- type noopMeshStrategyCreator struct {}
261+ type noopMeshCreator struct {}
263262
264- func (c * noopMeshStrategyCreator ) create (opts ... meshStrategyOption ) (meshStrategy , error ) {
265- return & noopMeshStrategy {}, nil
263+ func (c * noopMeshCreator ) create (opts ... meshOption ) (mesher , error ) {
264+ return & noopMesh {}, nil
266265}
267266
268- type noopMeshStrategy struct {}
267+ type noopMesh struct {}
269268
270- func (m * noopMeshStrategy ) meshThread () {
269+ func (m * noopMesh ) start () {
271270}
272- func (m * noopMeshStrategy ) start () {
273- }
274- func (m * noopMeshStrategy ) stop () {
271+ func (m * noopMesh ) stop () {
275272}
0 commit comments