Skip to content

Commit

Permalink
fix: httpServer could not be destroyed normally
Browse files Browse the repository at this point in the history
  • Loading branch information
zengchen221 committed Sep 23, 2019
1 parent 921961f commit b1e6221
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 23 additions & 12 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package rpc

import (
"net"
"net/http"
"net/url"
)

// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, *http.Server, error) {
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
Expand All @@ -33,7 +34,7 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
for _, api := range apis {
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return nil, nil, err
return nil, nil, nil, err
}
logger.Debug("HTTP registered ", "namespace ", api.Namespace)
}
Expand All @@ -45,18 +46,23 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
)
network, address, err := scheme(endpoint)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
if listener, err = net.Listen(network, address); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

go NewHTTPServer(cors, vhosts, timeouts, handler).Serve(listener)
return listener, handler, err
hServer := new(http.Server)
go func(hServer *http.Server) {
hServer = NewHTTPServer(cors, vhosts, timeouts, handler)
hServer.Serve(listener)
}(hServer)

return listener, handler, hServer, err
}

// StartWSEndpoint starts a websocket endpoint
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, *http.Server, error) {

// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
Expand All @@ -68,7 +74,7 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
for _, api := range apis {
if exposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return nil, nil, err
return nil, nil, nil, err
}
logger.Debug("WebSocket registered ", " service ", api.Service, " namespace ", api.Namespace)
}
Expand All @@ -80,15 +86,20 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
)
network, address, err := scheme(endpoint)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

if listener, err = net.Listen(network, address); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

go NewWSServer(wsOrigins, handler).Serve(listener)
return listener, handler, err
hServer := new(http.Server)
go func(hServer *http.Server) {
hServer = NewWSServer(wsOrigins, handler)
hServer.Serve(listener)
}(hServer)

return listener, handler, hServer, err

}

Expand Down
2 changes: 2 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rpc

import (
"context"
"fmt"
"io"
"sync/atomic"

Expand Down Expand Up @@ -93,6 +94,7 @@ func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
// Don't serve if server is stopped.
if atomic.LoadInt32(&s.run) == 0 {
fmt.Println("server is stopped")
return
}

Expand Down

0 comments on commit b1e6221

Please sign in to comment.