rpc: exit from Start after Listener is running
If port is dynamically allocated, `(*Server).Addr` will contain 0 port. This commit executes listener before exiting from `Start()` and sets Addr to the actual address.
This commit is contained in:
parent
911be78cc7
commit
758a88a813
3 changed files with 20 additions and 6 deletions
|
@ -355,7 +355,7 @@ func startServer(ctx *cli.Context) error {
|
|||
errChan := make(chan error)
|
||||
|
||||
go serv.Start(errChan)
|
||||
go rpcServer.Start(errChan)
|
||||
rpcServer.Start(errChan)
|
||||
|
||||
fmt.Fprintln(ctx.App.Writer, logo())
|
||||
fmt.Fprintln(ctx.App.Writer, serv.UserAgent)
|
||||
|
|
|
@ -167,18 +167,32 @@ func (s *Server) Start(errChan chan error) {
|
|||
s.https.Handler = http.HandlerFunc(s.handleHTTPRequest)
|
||||
s.log.Info("starting rpc-server (https)", zap.String("endpoint", s.https.Addr))
|
||||
go func() {
|
||||
err := s.https.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile)
|
||||
ln, err := net.Listen("tcp", s.https.Addr)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
s.https.Addr = ln.Addr().String()
|
||||
err = s.https.ServeTLS(ln, cfg.CertFile, cfg.KeyFile)
|
||||
if err != http.ErrServerClosed {
|
||||
s.log.Error("failed to start TLS RPC server", zap.Error(err))
|
||||
errChan <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
err := s.ListenAndServe()
|
||||
if err != http.ErrServerClosed {
|
||||
s.log.Error("failed to start RPC server", zap.Error(err))
|
||||
ln, err := net.Listen("tcp", s.Addr)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
s.Addr = ln.Addr().String() // set Addr to the actual address
|
||||
go func() {
|
||||
err = s.Serve(ln)
|
||||
if err != http.ErrServerClosed {
|
||||
s.log.Error("failed to start RPC server", zap.Error(err))
|
||||
errChan <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Shutdown overrides the http.Server Shutdown
|
||||
|
|
|
@ -68,7 +68,7 @@ func initClearServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *Server,
|
|||
require.NoError(t, err)
|
||||
rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
||||
errCh := make(chan error, 2)
|
||||
go rpcServer.Start(errCh)
|
||||
rpcServer.Start(errCh)
|
||||
|
||||
handler := http.HandlerFunc(rpcServer.handleHTTPRequest)
|
||||
srv := httptest.NewServer(handler)
|
||||
|
|
Loading…
Add table
Reference in a new issue