Skip to content

Commit abed63c

Browse files
authored
Merge pull request #16250 from gluk256/317-fatalf
whisper: refactoring go-routines workflow Move the call mailServer.Init() down (to the bottom of the function) because if the function initialize() completes successfully, then it will be followed by mailServer.Close() in shutdown(). The workflow of the corresponding goroutines is clearer now.
2 parents d429a92 + 61a061c commit abed63c

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

cmd/wnode/main.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func main() {
110110
processArgs()
111111
initialize()
112112
run()
113+
shutdown()
113114
}
114115

115116
func processArgs() {
@@ -209,21 +210,6 @@ func initialize() {
209210
MinimumAcceptedPOW: *argPoW,
210211
}
211212

212-
if *mailServerMode {
213-
if len(msPassword) == 0 {
214-
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
215-
if err != nil {
216-
utils.Fatalf("Failed to read Mail Server password: %s", err)
217-
}
218-
}
219-
220-
shh = whisper.New(cfg)
221-
shh.RegisterServer(&mailServer)
222-
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
223-
} else {
224-
shh = whisper.New(cfg)
225-
}
226-
227213
if *argPoW != whisper.DefaultMinimumPoW {
228214
err := shh.SetMinimumPoW(*argPoW)
229215
if err != nil {
@@ -265,6 +251,26 @@ func initialize() {
265251
maxPeers = 800
266252
}
267253

254+
_, err = crand.Read(entropy[:])
255+
if err != nil {
256+
utils.Fatalf("crypto/rand failed: %s", err)
257+
}
258+
259+
if *mailServerMode {
260+
if len(msPassword) == 0 {
261+
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
262+
if err != nil {
263+
utils.Fatalf("Failed to read Mail Server password: %s", err)
264+
}
265+
}
266+
267+
shh = whisper.New(cfg)
268+
shh.RegisterServer(&mailServer)
269+
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
270+
} else {
271+
shh = whisper.New(cfg)
272+
}
273+
268274
server = &p2p.Server{
269275
Config: p2p.Config{
270276
PrivateKey: nodeid,
@@ -278,17 +284,13 @@ func initialize() {
278284
TrustedNodes: peers,
279285
},
280286
}
281-
282-
_, err = crand.Read(entropy[:])
283-
if err != nil {
284-
utils.Fatalf("crypto/rand failed: %s", err)
285-
}
286287
}
287288

288-
func startServer() {
289+
func startServer() error {
289290
err := server.Start()
290291
if err != nil {
291-
utils.Fatalf("Failed to start Whisper peer: %s.", err)
292+
fmt.Printf("Failed to start Whisper peer: %s.", err)
293+
return err
292294
}
293295

294296
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
@@ -307,6 +309,7 @@ func startServer() {
307309
if !*forwarderMode {
308310
fmt.Printf("Please type the message. To quit type: '%s'\n", quitCommand)
309311
}
312+
return nil
310313
}
311314

312315
func isKeyValid(k *ecdsa.PublicKey) bool {
@@ -420,8 +423,10 @@ func waitForConnection(timeout bool) {
420423
}
421424

422425
func run() {
423-
defer mailServer.Close()
424-
startServer()
426+
err := startServer()
427+
if err != nil {
428+
return
429+
}
425430
defer server.Stop()
426431
shh.Start(nil)
427432
defer shh.Stop()
@@ -439,8 +444,11 @@ func run() {
439444
} else {
440445
sendLoop()
441446
}
447+
}
442448

449+
func shutdown() {
443450
close(done)
451+
mailServer.Close()
444452
}
445453

446454
func sendLoop() {

0 commit comments

Comments
 (0)