Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement client Write Timeouts

  • Loading branch information...
commit 8290891eec8b91136d09b069288cef969bc51293 1 parent 0e73ba8
@mrb authored
Showing with 38 additions and 6 deletions.
  1. +10 −1 conn.go
  2. +1 −0  err.go
  3. +22 −0 request.go
  4. +5 −5 response.go
View
11 conn.go
@@ -22,7 +22,7 @@ func Dial(addr string) (*Conn, error) {
return nil, err
}
- c.conn, err = net.DialTCP("tcp", nil, tcpaddr) //, time.Duration(500)*time.Millisecond)
+ c.conn, err = net.DialTCP("tcp", nil, tcpaddr)
if err != nil {
return nil, err
@@ -36,7 +36,16 @@ func (c *Conn) Close() {
}
func (c *Conn) Write(formattedRequest []byte) (err error) {
+ timeoutime := time.Now().Add(time.Duration(1e9))
+ c.conn.SetWriteDeadline(timeoutime)
+
_, err = c.conn.Write(formattedRequest)
+
+ if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
+ err = ErrWriteTimeout
+ return err
+ }
+
return err
}
View
1  err.go
@@ -13,4 +13,5 @@ var (
ErrRiakError = errors.New("riak error")
ErrNotDone = errors.New("not done")
ErrReadTimeout = errors.New("read timeout")
+ ErrWriteTimeout = errors.New("write timeout")
)
View
22 request.go
@@ -2,6 +2,7 @@ package riakpbc
import (
"code.google.com/p/goprotobuf/proto"
+ "log"
)
var commandToNum = map[string]byte{
@@ -32,6 +33,10 @@ var commandToNum = map[string]byte{
"RpbMapRedResp": 24,
}
+var (
+ maxWriteRetries = 3
+)
+
func (c *Conn) Request(reqstruct interface{}, structname string) (err error) {
marshaledRequest, err := marshalRequest(reqstruct)
if err != nil {
@@ -43,11 +48,28 @@ func (c *Conn) Request(reqstruct interface{}, structname string) (err error) {
return err
}
+ currentRetries := 0
err = c.Write(formattedRequest)
if err != nil {
return err
}
+ if err != nil {
+ if err == ErrReadTimeout && currentRetries < maxReadRetries {
+ for currentRetries < maxReadRetries {
+ log.Print(currentRetries, maxReadRetries)
+ err = c.Write(formattedRequest)
+ if err != nil {
+ currentRetries = currentRetries + 1
+ } else {
+ currentRetries = maxReadRetries + 1
+ }
+ }
+ }
+ err = ErrWriteTimeout
+ return err
+ }
+
return nil
}
View
10 response.go
@@ -34,7 +34,7 @@ var numToCommand = map[int]string{
}
var (
- maxRetries = 3
+ maxReadRetries = 3
)
func (c *Conn) Response(respstruct interface{}, structname string) (response interface{}, err error) {
@@ -43,14 +43,14 @@ func (c *Conn) Response(respstruct interface{}, structname string) (response int
rawresp, err = c.Read()
if err != nil {
- if err == ErrReadTimeout && currentRetries < maxRetries {
- for currentRetries < maxRetries {
- log.Print(currentRetries, maxRetries, rawresp)
+ if err == ErrReadTimeout && currentRetries < maxReadRetries {
+ for currentRetries < maxReadRetries {
+ log.Print(currentRetries, maxReadRetries, rawresp)
rawresp, err = c.Read()
if err != nil {
currentRetries = currentRetries + 1
} else {
- currentRetries = maxRetries + 1
+ currentRetries = maxReadRetries + 1
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.