Skip to content

Commit

Permalink
rpc: clean up IPC handler (ethereum#16524)
Browse files Browse the repository at this point in the history
This avoids logging accept errors on shutdown and removes
a bit of duplication. It also fixes some goimports lint warnings.
  • Loading branch information
fjl authored and mariameda committed Aug 19, 2018
1 parent 6548492 commit e53df0a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 60 deletions.
6 changes: 3 additions & 3 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strings"

"encoding/hex"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -44,7 +45,6 @@ import (
"github.com/ethereum/go-ethereum/signer/rules"
"github.com/ethereum/go-ethereum/signer/storage"
"gopkg.in/urfave/cli.v1"
"os/signal"
)

// ExternalApiVersion -- see extapi_changelog.md
Expand Down Expand Up @@ -435,7 +435,7 @@ func signer(c *cli.Context) error {
ipcApiUrl = filepath.Join(configDir, "clef.ipc")
}

listener, _, err := rpc.StartIPCEndpoint(func() bool { return true }, ipcApiUrl, rpcApi)
listener, _, err := rpc.StartIPCEndpoint(ipcApiUrl, rpcApi)
if err != nil {
utils.Fatalf("Could not start IPC api: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ package common

import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"math/rand"
"reflect"
"strings"

"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto/sha3"
"strings"
)

const (
Expand Down
14 changes: 2 additions & 12 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,23 +303,13 @@ func (n *Node) stopInProc() {

// startIPC initializes and starts the IPC RPC endpoint.
func (n *Node) startIPC(apis []rpc.API) error {
// Short circuit if the IPC endpoint isn't being exposed
if n.ipcEndpoint == "" {
return nil

}
isClosed := func() bool {
n.lock.RLock()
defer n.lock.RUnlock()
return n.ipcListener == nil
return nil // IPC disabled.
}

listener, handler, err := rpc.StartIPCEndpoint(isClosed, n.ipcEndpoint, apis)
listener, handler, err := rpc.StartIPCEndpoint(n.ipcEndpoint, apis)
if err != nil {
return err
}

// All listeners booted successfully
n.ipcListener = listener
n.ipcHandler = handler
n.log.Info("IPC endpoint opened", "url", n.ipcEndpoint)
Expand Down
2 changes: 1 addition & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"net"
"net/url"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -33,7 +34,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/log"
"os"
)

var (
Expand Down
36 changes: 9 additions & 27 deletions rpc/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package rpc

import (
"github.com/ethereum/go-ethereum/log"
"net"

"github.com/ethereum/go-ethereum/log"
)

// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
Expand Down Expand Up @@ -81,40 +82,21 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []

}

// StartIPCEndpoint starts an IPC endpoint
func StartIPCEndpoint(isClosedFn func() bool, ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
// Register all the APIs exposed by the services
// StartIPCEndpoint starts an IPC endpoint.
func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
// Register all the APIs exposed by the services.
handler := NewServer()
for _, api := range apis {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return nil, nil, err
}
log.Debug("IPC registered", "namespace", api.Namespace)
}
// All APIs registered, start the IPC listener
var (
listener net.Listener
err error
)
if listener, err = CreateIPCListener(ipcEndpoint); err != nil {
// All APIs registered, start the IPC listener.
listener, err := ipcListen(ipcEndpoint)
if err != nil {
return nil, nil, err
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
// Terminate if the listener was closed
if isClosedFn() {
log.Info("IPC closed", "err", err)
} else {
// Not closed, just some error; report and continue
log.Error("IPC accept failed", "err", err)
}
continue
}
go handler.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
}
}()

go handler.ServeListener(listener)
return listener, handler, nil
}
27 changes: 12 additions & 15 deletions rpc/ipc.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
// Copyright 2015 The go-nilu Authors
// This file is part of the go-nilu library.
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-nilu library is free software: you can redistribute it and/or modify
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-nilu library is distributed in the hope that it will be useful,
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-nilu library. If not, see <http://www.gnu.org/licenses/>.
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package rpc

import (
"context"
"fmt"
"net"

"github.com/NiluPlatform/go-nilu/log"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/netutil"
)

// CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on
// Windows this is a named pipe
func CreateIPCListener(endpoint string) (net.Listener, error) {
return ipcListen(endpoint)
}

// ServeListener accepts connections on l, serving JSON-RPC on them.
func (srv *Server) ServeListener(l net.Listener) error {
for {
conn, err := l.Accept()
if err != nil {
if netutil.IsTemporaryError(err) {
log.Warn("RPC accept error", "err", err)
continue
} else if err != nil {
return err
}
log.Trace(fmt.Sprint("accepted conn", conn.RemoteAddr()))
log.Trace("Accepted connection", "addr", conn.RemoteAddr())
go srv.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
}
}
Expand Down

0 comments on commit e53df0a

Please sign in to comment.