@@ -140,7 +140,7 @@ func (n *Node) Register(constructor ServiceConstructor) error {
140140 return nil
141141}
142142
143- // Start create a live P2P node and starts running it.
143+ // Start creates a live P2P node and starts running it.
144144func (n * Node ) Start () error {
145145 n .lock .Lock ()
146146 defer n .lock .Unlock ()
@@ -203,7 +203,7 @@ func (n *Node) Start() error {
203203 return convertFileLockError (err )
204204 }
205205 // Start each of the services
206- started := []reflect.Type {}
206+ var started []reflect.Type
207207 for kind , service := range services {
208208 // Start the next service, stopping all previous upon failure
209209 if err := service .Start (running ); err != nil {
@@ -217,7 +217,7 @@ func (n *Node) Start() error {
217217 // Mark the service started for potential cleanup
218218 started = append (started , kind )
219219 }
220- // Lastly start the configured RPC interfaces
220+ // Lastly, start the configured RPC interfaces
221221 if err := n .startRPC (services ); err != nil {
222222 for _ , service := range services {
223223 service .Stop ()
@@ -252,7 +252,7 @@ func (n *Node) openDataDir() error {
252252 return nil
253253}
254254
255- // startRPC is a helper method to start all the various RPC endpoint during node
255+ // startRPC is a helper method to start all the various RPC endpoints during node
256256// startup. It's not meant to be called at any time afterwards as it makes certain
257257// assumptions about the state of the node.
258258func (n * Node ) startRPC (services map [reflect.Type ]Service ) error {
@@ -269,7 +269,7 @@ func (n *Node) startRPC(services map[reflect.Type]Service) error {
269269 n .stopInProc ()
270270 return err
271271 }
272- if err := n .startHTTP (n .httpEndpoint , apis , n .config .HTTPModules , n .config .HTTPCors , n .config .HTTPVirtualHosts ); err != nil {
272+ if err := n .startHTTP (n .httpEndpoint , apis , n .config .HTTPModules , n .config .HTTPCors , n .config .HTTPVirtualHosts , n . config . HTTPTimeouts ); err != nil {
273273 n .stopIPC ()
274274 n .stopInProc ()
275275 return err
@@ -293,7 +293,7 @@ func (n *Node) startInProc(apis []rpc.API) error {
293293 if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
294294 return err
295295 }
296- n .log .Debug ("InProc registered" , "service" , api . Service , " namespace" , api .Namespace )
296+ n .log .Debug ("InProc registered" , "namespace" , api .Namespace )
297297 }
298298 n .inprocHandler = handler
299299 return nil
@@ -320,51 +320,16 @@ func (n *Node) RegisterAPIs(apis []rpc.API) {
320320
321321// startIPC initializes and starts the IPC RPC endpoint.
322322func (n * Node ) startIPC (apis []rpc.API ) error {
323- // Short circuit if the IPC endpoint isn't being exposed
324323 if n .ipcEndpoint == "" {
325- return nil
324+ return nil // IPC disabled.
326325 }
327- // Register all the APIs exposed by the services
328- handler := rpc .NewServer ()
329- for _ , api := range apis {
330- if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
331- return err
332- }
333- n .log .Debug ("IPC registered" , "service" , api .Service , "namespace" , api .Namespace )
334- }
335- // All APIs registered, start the IPC listener
336- var (
337- listener net.Listener
338- err error
339- )
340- if listener , err = rpc .CreateIPCListener (n .ipcEndpoint ); err != nil {
326+ listener , handler , err := rpc .StartIPCEndpoint (n .ipcEndpoint , apis )
327+ if err != nil {
341328 return err
342329 }
343- go func () {
344- n .log .Info ("IPC endpoint opened" , "url" , n .ipcEndpoint )
345-
346- for {
347- conn , err := listener .Accept ()
348- if err != nil {
349- // Terminate if the listener was closed
350- n .lock .RLock ()
351- closed := n .ipcListener == nil
352- n .lock .RUnlock ()
353- if closed {
354- return
355- }
356- // Not closed, just some error; report and continue
357- n .log .Error ("IPC accept failed" , "err" , err )
358- continue
359- }
360- log .Trace ("Accepted RPC connection" , "conn" , conn .RemoteAddr ())
361- go handler .ServeCodec (rpc .NewCodec (conn ), 0 )
362- }
363- }()
364- // All listeners booted successfully
365330 n .ipcListener = listener
366331 n .ipcHandler = handler
367-
332+ log . Info ( "IPC endpoint opened" , "url" , n . ipcEndpoint )
368333 return nil
369334}
370335
@@ -374,7 +339,7 @@ func (n *Node) stopIPC() {
374339 n .ipcListener .Close ()
375340 n .ipcListener = nil
376341
377- n .log .Info ("IPC endpoint closed" , "endpoint " , n .ipcEndpoint )
342+ n .log .Info ("IPC endpoint closed" , "url " , n .ipcEndpoint )
378343 }
379344 if n .ipcHandler != nil {
380345 n .ipcHandler .Stop ()
@@ -383,36 +348,18 @@ func (n *Node) stopIPC() {
383348}
384349
385350// startHTTP initializes and starts the HTTP RPC endpoint.
386- func (n * Node ) startHTTP (endpoint string , apis []rpc.API , modules []string , cors []string , vhosts []string ) error {
351+ func (n * Node ) startHTTP (endpoint string , apis []rpc.API , modules []string , cors []string , vhosts []string , timeouts rpc. HTTPTimeouts ) error {
387352 // Short circuit if the HTTP endpoint isn't being exposed
388353 if endpoint == "" {
389354 return nil
390355 }
391- // Generate the whitelist based on the allowed modules
392- whitelist := make (map [string ]bool )
393- for _ , module := range modules {
394- whitelist [module ] = true
395- }
396- // Register all the APIs exposed by the services
397- handler := rpc .NewServer ()
398- for _ , api := range apis {
399- if whitelist [api .Namespace ] || (len (whitelist ) == 0 && api .Public ) {
400- if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
401- return err
402- }
403- n .log .Debug ("HTTP registered" , "service" , api .Service , "namespace" , api .Namespace )
404- }
405- }
406- // All APIs registered, start the HTTP listener
407- var (
408- listener net.Listener
409- err error
410- )
411- if listener , err = net .Listen ("tcp" , endpoint ); err != nil {
356+ listener , handler , err := rpc .StartHTTPEndpoint (endpoint , apis , modules , cors , vhosts , timeouts )
357+ if err != nil {
412358 return err
413359 }
414- go rpc .NewHTTPServer (cors , vhosts , handler , n .config .HTTPWriteTimeout ).Serve (listener )
415- n .log .Info ("HTTP endpoint opened" , "url" , fmt .Sprintf ("http://%s" , endpoint ), "cors" , strings .Join (cors , "," ), "vhosts" , strings .Join (vhosts , "," ))
360+ n .log .Info ("HTTP endpoint opened" , "url" , fmt .Sprintf ("http://%v/" , listener .Addr ()),
361+ "cors" , strings .Join (cors , "," ),
362+ "vhosts" , strings .Join (vhosts , "," ))
416363 // All listeners booted successfully
417364 n .httpEndpoint = endpoint
418365 n .httpListener = listener
@@ -424,10 +371,10 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
424371// stopHTTP terminates the HTTP RPC endpoint.
425372func (n * Node ) stopHTTP () {
426373 if n .httpListener != nil {
374+ url := fmt .Sprintf ("http://%v/" , n .httpListener .Addr ())
427375 n .httpListener .Close ()
428376 n .httpListener = nil
429-
430- n .log .Info ("HTTP endpoint closed" , "url" , fmt .Sprintf ("http://%s" , n .httpEndpoint ))
377+ n .log .Info ("HTTP endpoint closed" , "url" , url )
431378 }
432379 if n .httpHandler != nil {
433380 n .httpHandler .Stop ()
@@ -441,32 +388,11 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
441388 if endpoint == "" {
442389 return nil
443390 }
444- // Generate the whitelist based on the allowed modules
445- whitelist := make (map [string ]bool )
446- for _ , module := range modules {
447- whitelist [module ] = true
448- }
449- // Register all the APIs exposed by the services
450- handler := rpc .NewServer ()
451- for _ , api := range apis {
452- if exposeAll || whitelist [api .Namespace ] || (len (whitelist ) == 0 && api .Public ) {
453- if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
454- return err
455- }
456- n .log .Debug ("WebSocket registered" , "service" , api .Service , "namespace" , api .Namespace )
457- }
458- }
459- // All APIs registered, start the HTTP listener
460- var (
461- listener net.Listener
462- err error
463- )
464- if listener , err = net .Listen ("tcp" , endpoint ); err != nil {
391+ listener , handler , err := rpc .StartWSEndpoint (endpoint , apis , modules , wsOrigins , exposeAll )
392+ if err != nil {
465393 return err
466394 }
467- go rpc .NewWSServer (wsOrigins , handler ).Serve (listener )
468395 n .log .Info ("WebSocket endpoint opened" , "url" , fmt .Sprintf ("ws://%s" , listener .Addr ()))
469-
470396 // All listeners booted successfully
471397 n .wsEndpoint = endpoint
472398 n .wsListener = listener
@@ -645,11 +571,23 @@ func (n *Node) IPCEndpoint() string {
645571
646572// HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.
647573func (n * Node ) HTTPEndpoint () string {
574+ n .lock .Lock ()
575+ defer n .lock .Unlock ()
576+
577+ if n .httpListener != nil {
578+ return n .httpListener .Addr ().String ()
579+ }
648580 return n .httpEndpoint
649581}
650582
651583// WSEndpoint retrieves the current WS endpoint used by the protocol stack.
652584func (n * Node ) WSEndpoint () string {
585+ n .lock .Lock ()
586+ defer n .lock .Unlock ()
587+
588+ if n .wsListener != nil {
589+ return n .wsListener .Addr ().String ()
590+ }
653591 return n .wsEndpoint
654592}
655593
0 commit comments