Commit 75958881 authored by Zhu Yong's avatar Zhu Yong
Browse files

for client option, add connection timeout and request timeout settings

parent 2185740d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
bin/
vendor/
/Godeps/
/.idea/
+7 −6
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ type ClientOptions struct {
	Hmac           []byte
	UseSSL         bool  // Use SSL connection, or plain connection
	Timeout        int64 // Network timeout in millisecond
	RequestTimeout int64 // Operation request timeout in millisecond
}

// MessageType defines the top level kinetic command message type.
+16 −6
Original line number Diff line number Diff line
@@ -31,8 +31,14 @@ import (
	proto "github.com/golang/protobuf/proto"
)

const (
	defaultConnectionTimeout = 20 * time.Second
	defaultRequestTimeout    = 60 * time.Second
)

var (
	networkTimeout = 20 * time.Second
	connectionTimeout = defaultConnectionTimeout
	requestTimeout    = defaultRequestTimeout
)

func newMessage(t kproto.Message_AuthType) *kproto.Message {
@@ -72,18 +78,22 @@ type networkService struct {
func newNetworkService(op ClientOptions) (*networkService, error) {
	var conn net.Conn
	var err error

	if op.Timeout > 0 {
		networkTimeout = time.Duration(op.Timeout) * time.Millisecond
		connectionTimeout = time.Duration(op.Timeout) * time.Millisecond
	}
	if op.RequestTimeout > 0 {
		requestTimeout = time.Duration(op.RequestTimeout) * time.Millisecond
	}

	target := fmt.Sprintf("%s:%d", op.Host, op.Port)
	if op.UseSSL {
		// TODO: Need to enable verify certification later
		config := tls.Config{InsecureSkipVerify: true}
		d := &net.Dialer{Timeout: networkTimeout}
		d := &net.Dialer{Timeout: connectionTimeout}
		conn, err = tls.DialWithDialer(d, "tcp", target, &config)
	} else {
		conn, err = net.DialTimeout("tcp", target, networkTimeout)
		conn, err = net.DialTimeout("tcp", target, connectionTimeout)
	}

	if err != nil {
@@ -257,7 +267,7 @@ func (ns *networkService) send(msg *kproto.Message, value []byte) error {
	}

	// Set timeout for send packet
	ns.conn.SetWriteDeadline(time.Now().Add(networkTimeout))
	ns.conn.SetWriteDeadline(time.Now().Add(requestTimeout))

	// Construct message header 9 bytes
	header := make([]byte, 9)
@@ -285,7 +295,7 @@ func (ns *networkService) send(msg *kproto.Message, value []byte) error {

func (ns *networkService) receive() (*kproto.Message, *kproto.Command, []byte, error) {
	// Set timeout for receive packet
	ns.conn.SetReadDeadline(time.Now().Add(networkTimeout))
	ns.conn.SetReadDeadline(time.Now().Add(requestTimeout))

	header := make([]byte, 9)