Don't export types/functions
This commit is contained in:
parent
2c229bac00
commit
61885aa965
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var rpclogger = logger.NewLogger("RPC")
|
var rpclogger = logger.NewLogger("RPC")
|
||||||
var rpclistener *StoppableTCPListener
|
var rpclistener *stoppableTCPListener
|
||||||
|
|
||||||
const (
|
const (
|
||||||
jsonrpcver = "2.0"
|
jsonrpcver = "2.0"
|
||||||
@ -29,7 +29,7 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
|
|||||||
return nil // RPC service already running on given host/port
|
return nil // RPC service already running on given host/port
|
||||||
}
|
}
|
||||||
|
|
||||||
l, err := NewStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
|
l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err)
|
rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err)
|
||||||
return err
|
return err
|
||||||
@ -43,9 +43,9 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
|
|||||||
opts.AllowedOrigins = []string{config.CorsDomain}
|
opts.AllowedOrigins = []string{config.CorsDomain}
|
||||||
|
|
||||||
c := cors.New(opts)
|
c := cors.New(opts)
|
||||||
handler = NewStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop)
|
handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop)
|
||||||
} else {
|
} else {
|
||||||
handler = NewStoppableHandler(JSONRPC(pipe), l.stop)
|
handler = newStoppableHandler(JSONRPC(pipe), l.stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
go http.Serve(l, handler)
|
go http.Serve(l, handler)
|
||||||
|
24
rpc/types.go
24
rpc/types.go
@ -265,18 +265,18 @@ type RpcErrorObject struct {
|
|||||||
// Data interface{} `json:"data"`
|
// Data interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListenerStoppedError struct {
|
type listenerHasStoppedError struct {
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self ListenerStoppedError) Error() string {
|
func (self listenerHasStoppedError) Error() string {
|
||||||
return self.msg
|
return self.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
var listenerStoppedError = ListenerStoppedError{"Listener stopped"}
|
var listenerStoppedError = listenerHasStoppedError{"Listener stopped"}
|
||||||
|
|
||||||
// When https://github.com/golang/go/issues/4674 is fixed this could be replaced
|
// When https://github.com/golang/go/issues/4674 is fixed this could be replaced
|
||||||
type StoppableTCPListener struct {
|
type stoppableTCPListener struct {
|
||||||
*net.TCPListener
|
*net.TCPListener
|
||||||
stop chan struct{} // closed when the listener must stop
|
stop chan struct{} // closed when the listener must stop
|
||||||
}
|
}
|
||||||
@ -284,7 +284,7 @@ type StoppableTCPListener struct {
|
|||||||
// Wraps the default handler and checks if the RPC service was stopped. In that case it returns an
|
// Wraps the default handler and checks if the RPC service was stopped. In that case it returns an
|
||||||
// error indicating that the service was stopped. This will only happen for connections which are
|
// error indicating that the service was stopped. This will only happen for connections which are
|
||||||
// kept open (HTTP keep-alive) when the RPC service was shutdown.
|
// kept open (HTTP keep-alive) when the RPC service was shutdown.
|
||||||
func NewStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
|
func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
select {
|
select {
|
||||||
case <-stop:
|
case <-stop:
|
||||||
@ -298,11 +298,11 @@ func NewStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Stop the listener and all accepted and still active connections.
|
// Stop the listener and all accepted and still active connections.
|
||||||
func (self *StoppableTCPListener) Stop() {
|
func (self *stoppableTCPListener) Stop() {
|
||||||
close(self.stop)
|
close(self.stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) {
|
func newStoppableTCPListener(addr string) (*stoppableTCPListener, error) {
|
||||||
wl, err := net.Listen("tcp", addr)
|
wl, err := net.Listen("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -310,14 +310,14 @@ func NewStoppableTCPListener(addr string) (*StoppableTCPListener, error) {
|
|||||||
|
|
||||||
if tcpl, ok := wl.(*net.TCPListener); ok {
|
if tcpl, ok := wl.(*net.TCPListener); ok {
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
l := &StoppableTCPListener{tcpl, stop}
|
l := &stoppableTCPListener{tcpl, stop}
|
||||||
return l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("Unable to create TCP listener for RPC service")
|
return nil, errors.New("Unable to create TCP listener for RPC service")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StoppableTCPListener) Accept() (net.Conn, error) {
|
func (self *stoppableTCPListener) Accept() (net.Conn, error) {
|
||||||
for {
|
for {
|
||||||
self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second)))
|
self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second)))
|
||||||
c, err := self.TCPListener.AcceptTCP()
|
c, err := self.TCPListener.AcceptTCP()
|
||||||
@ -338,16 +338,16 @@ func (self *StoppableTCPListener) Accept() (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ClosableConnection{c, self.stop}, err
|
return &closableConnection{c, self.stop}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClosableConnection struct {
|
type closableConnection struct {
|
||||||
*net.TCPConn
|
*net.TCPConn
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *ClosableConnection) Read(b []byte) (n int, err error) {
|
func (self *closableConnection) Read(b []byte) (n int, err error) {
|
||||||
select {
|
select {
|
||||||
case <-self.closed:
|
case <-self.closed:
|
||||||
self.TCPConn.Close()
|
self.TCPConn.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user