2016-08-15 09:00:18 +02:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 23:50:50 +02:00
|
|
|
package cmd
|
2016-08-15 09:00:18 +02:00
|
|
|
|
|
|
|
import (
|
2016-09-30 08:42:37 +02:00
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
2016-10-18 20:46:33 +02:00
|
|
|
"crypto/x509"
|
2016-08-27 12:16:52 +02:00
|
|
|
"errors"
|
2016-11-20 02:37:57 +01:00
|
|
|
"fmt"
|
2016-09-30 08:42:37 +02:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2016-08-15 09:00:18 +02:00
|
|
|
"net/rpc"
|
|
|
|
"sync"
|
2016-10-17 23:31:33 +02:00
|
|
|
"time"
|
2016-08-15 09:00:18 +02:00
|
|
|
)
|
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
// defaultDialTimeout is used for non-secure connection.
|
|
|
|
const defaultDialTimeout = 3 * time.Second
|
|
|
|
|
2016-08-15 09:00:18 +02:00
|
|
|
// RPCClient is a wrapper type for rpc.Client which provides reconnect on first failure.
|
|
|
|
type RPCClient struct {
|
2016-12-18 03:17:40 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
netRPCClient *rpc.Client
|
|
|
|
node string
|
|
|
|
rpcPath string
|
|
|
|
secureConn bool
|
2016-08-15 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newClient constructs a RPCClient object with node and rpcPath initialized.
|
2016-12-18 03:17:40 +01:00
|
|
|
// It does lazy connect to the remote endpoint on Call().
|
|
|
|
func newRPCClient(node, rpcPath string, secureConn bool) *RPCClient {
|
2016-08-15 09:00:18 +02:00
|
|
|
return &RPCClient{
|
2016-09-30 08:42:37 +02:00
|
|
|
node: node,
|
|
|
|
rpcPath: rpcPath,
|
|
|
|
secureConn: secureConn,
|
2016-08-15 09:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
// dial tries to establish a connection to the server in a safe manner.
|
|
|
|
// If there is a valid rpc.Cliemt, it returns that else creates a new one.
|
|
|
|
func (rpcClient *RPCClient) dial() (*rpc.Client, error) {
|
2016-08-27 12:16:52 +02:00
|
|
|
rpcClient.mu.Lock()
|
2016-12-18 03:17:40 +01:00
|
|
|
defer rpcClient.mu.Unlock()
|
2016-08-27 12:16:52 +02:00
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
// Nothing to do as we already have valid connection.
|
|
|
|
if rpcClient.netRPCClient != nil {
|
|
|
|
return rpcClient.netRPCClient, nil
|
2016-08-15 23:33:48 +02:00
|
|
|
}
|
2016-09-30 08:42:37 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var conn net.Conn
|
|
|
|
if rpcClient.secureConn {
|
2016-10-20 16:43:31 +02:00
|
|
|
hostname, _, splitErr := net.SplitHostPort(rpcClient.node)
|
|
|
|
if splitErr != nil {
|
2016-11-20 02:37:57 +01:00
|
|
|
err = errors.New("Unable to parse RPC address <" + rpcClient.node + "> : " + splitErr.Error())
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "dial-http",
|
|
|
|
Net: rpcClient.node + " " + rpcClient.rpcPath,
|
|
|
|
Addr: nil,
|
|
|
|
Err: err,
|
|
|
|
}
|
2016-10-20 16:43:31 +02:00
|
|
|
}
|
|
|
|
// ServerName in tls.Config needs to be specified to support SNI certificates
|
2016-11-11 16:18:44 +01:00
|
|
|
conn, err = tls.Dial("tcp", rpcClient.node, &tls.Config{ServerName: hostname, RootCAs: globalRootCAs})
|
2016-09-30 08:42:37 +02:00
|
|
|
} else {
|
2016-12-18 03:17:40 +01:00
|
|
|
// Dial with 3 seconds timeout.
|
|
|
|
conn, err = net.DialTimeout("tcp", rpcClient.node, defaultDialTimeout)
|
2016-09-30 08:42:37 +02:00
|
|
|
}
|
2016-08-27 12:16:52 +02:00
|
|
|
if err != nil {
|
2016-10-18 20:46:33 +02:00
|
|
|
// Print RPC connection errors that are worthy to display in log
|
|
|
|
switch err.(type) {
|
|
|
|
case x509.HostnameError:
|
2016-12-18 03:17:40 +01:00
|
|
|
errorIf(err, "Unable to establish secure connection to %s", rpcClient.node)
|
2016-10-18 20:46:33 +02:00
|
|
|
}
|
2016-11-20 02:37:57 +01:00
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "dial-http",
|
|
|
|
Net: rpcClient.node + " " + rpcClient.rpcPath,
|
|
|
|
Addr: nil,
|
|
|
|
Err: err,
|
|
|
|
}
|
2016-08-27 12:16:52 +02:00
|
|
|
}
|
2016-12-18 03:17:40 +01:00
|
|
|
|
2016-09-30 08:42:37 +02:00
|
|
|
io.WriteString(conn, "CONNECT "+rpcClient.rpcPath+" HTTP/1.0\n\n")
|
|
|
|
|
2016-10-06 11:30:54 +02:00
|
|
|
// Require successful HTTP response before switching to RPC protocol.
|
2016-09-30 08:42:37 +02:00
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
|
|
|
|
if err == nil && resp.Status == "200 Connected to Go RPC" {
|
2016-12-18 03:17:40 +01:00
|
|
|
netRPCClient := rpc.NewClient(conn)
|
|
|
|
if netRPCClient == nil {
|
2016-11-20 02:37:57 +01:00
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "dial-http",
|
|
|
|
Net: rpcClient.node + " " + rpcClient.rpcPath,
|
|
|
|
Addr: nil,
|
2016-12-18 03:17:40 +01:00
|
|
|
Err: fmt.Errorf("Unable to initialize new rpc.Client, %s", errUnexpected),
|
2016-11-20 02:37:57 +01:00
|
|
|
}
|
2016-09-30 08:42:37 +02:00
|
|
|
}
|
2016-12-18 03:17:40 +01:00
|
|
|
|
|
|
|
rpcClient.netRPCClient = netRPCClient
|
|
|
|
|
|
|
|
return netRPCClient, nil
|
2016-09-30 08:42:37 +02:00
|
|
|
}
|
2016-12-18 03:17:40 +01:00
|
|
|
|
2016-09-30 08:42:37 +02:00
|
|
|
if err == nil {
|
|
|
|
err = errors.New("unexpected HTTP response: " + resp.Status)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "dial-http",
|
|
|
|
Net: rpcClient.node + " " + rpcClient.rpcPath,
|
|
|
|
Addr: nil,
|
|
|
|
Err: err,
|
|
|
|
}
|
2016-08-15 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call makes a RPC call to the remote endpoint using the default codec, namely encoding/gob.
|
|
|
|
func (rpcClient *RPCClient) Call(serviceMethod string, args interface{}, reply interface{}) error {
|
2016-12-18 03:17:40 +01:00
|
|
|
// Get a new or existing rpc.Client.
|
|
|
|
netRPCClient, err := rpcClient.dial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-15 09:00:18 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
return netRPCClient.Call(serviceMethod, args, reply)
|
2016-08-27 12:16:52 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
// Close closes underlying rpc.Client.
|
2016-08-27 12:16:52 +02:00
|
|
|
func (rpcClient *RPCClient) Close() error {
|
2016-12-18 03:17:40 +01:00
|
|
|
rpcClient.mu.Lock()
|
2016-08-15 09:00:18 +02:00
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
if rpcClient.netRPCClient != nil {
|
|
|
|
// We make a copy of rpc.Client and unlock it immediately so that another
|
|
|
|
// goroutine could try to dial or close in parallel.
|
|
|
|
netRPCClient := rpcClient.netRPCClient
|
|
|
|
rpcClient.netRPCClient = nil
|
|
|
|
rpcClient.mu.Unlock()
|
|
|
|
|
|
|
|
return netRPCClient.Close()
|
2016-08-27 12:16:52 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 03:17:40 +01:00
|
|
|
rpcClient.mu.Unlock()
|
|
|
|
return nil
|
2016-08-15 09:00:18 +02:00
|
|
|
}
|
2016-09-16 09:30:55 +02:00
|
|
|
|
|
|
|
// Node returns the node (network address) of the connection
|
|
|
|
func (rpcClient *RPCClient) Node() string {
|
|
|
|
return rpcClient.node
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCPath returns the RPC path of the connection
|
|
|
|
func (rpcClient *RPCClient) RPCPath() string {
|
|
|
|
return rpcClient.rpcPath
|
|
|
|
}
|