added IPC timeout support
This commit is contained in:
parent
04910c902a
commit
5757a0edb5
@ -10,61 +10,69 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
READ_TIMEOUT = 15 // read timeout in seconds
|
||||||
MAX_REQUEST_SIZE = 1024 * 1024
|
MAX_REQUEST_SIZE = 1024 * 1024
|
||||||
MAX_RESPONSE_SIZE = 1024 * 1024
|
MAX_RESPONSE_SIZE = 1024 * 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
// Json serialization support
|
// Json serialization support
|
||||||
type JsonCodec struct {
|
type JsonCodec struct {
|
||||||
c net.Conn
|
c net.Conn
|
||||||
buffer []byte
|
|
||||||
bytesInBuffer int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new JSON coder instance
|
// Create new JSON coder instance
|
||||||
func NewJsonCoder(conn net.Conn) ApiCoder {
|
func NewJsonCoder(conn net.Conn) ApiCoder {
|
||||||
return &JsonCodec{
|
return &JsonCodec{
|
||||||
c: conn,
|
c: conn,
|
||||||
buffer: make([]byte, MAX_REQUEST_SIZE),
|
|
||||||
bytesInBuffer: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize obj to JSON and write it to conn
|
// Serialize obj to JSON and write it to conn
|
||||||
func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) {
|
func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) {
|
||||||
n, err := self.c.Read(self.buffer[self.bytesInBuffer:])
|
bytesInBuffer := 0
|
||||||
if err != nil {
|
buf := make([]byte, MAX_REQUEST_SIZE)
|
||||||
self.bytesInBuffer = 0
|
|
||||||
|
deadline := time.Now().Add(READ_TIMEOUT * time.Second)
|
||||||
|
if err := self.c.SetDeadline(deadline); err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
self.bytesInBuffer += n
|
for {
|
||||||
|
n, err := self.c.Read(buf[bytesInBuffer:])
|
||||||
|
if err != nil {
|
||||||
|
self.c.Close()
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
singleRequest := shared.Request{}
|
bytesInBuffer += n
|
||||||
err = json.Unmarshal(self.buffer[:self.bytesInBuffer], &singleRequest)
|
|
||||||
if err == nil {
|
singleRequest := shared.Request{}
|
||||||
self.bytesInBuffer = 0
|
err = json.Unmarshal(buf[:bytesInBuffer], &singleRequest)
|
||||||
requests := make([]*shared.Request, 1)
|
if err == nil {
|
||||||
requests[0] = &singleRequest
|
requests := make([]*shared.Request, 1)
|
||||||
return requests, false, nil
|
requests[0] = &singleRequest
|
||||||
|
return requests, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
requests = make([]*shared.Request, 0)
|
||||||
|
err = json.Unmarshal(buf[:bytesInBuffer], &requests)
|
||||||
|
if err == nil {
|
||||||
|
return requests, true, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requests = make([]*shared.Request, 0)
|
self.c.Close() // timeout
|
||||||
err = json.Unmarshal(self.buffer[:self.bytesInBuffer], &requests)
|
return nil, false, fmt.Errorf("Unable to read response")
|
||||||
if err == nil {
|
|
||||||
self.bytesInBuffer = 0
|
|
||||||
return requests, true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *JsonCodec) ReadResponse() (interface{}, error) {
|
func (self *JsonCodec) ReadResponse() (interface{}, error) {
|
||||||
bytesInBuffer := 0
|
bytesInBuffer := 0
|
||||||
buf := make([]byte, MAX_RESPONSE_SIZE)
|
buf := make([]byte, MAX_RESPONSE_SIZE)
|
||||||
|
|
||||||
deadline := time.Now().Add(15 * time.Second)
|
deadline := time.Now().Add(READ_TIMEOUT * time.Second)
|
||||||
self.c.SetDeadline(deadline)
|
if err := self.c.SetDeadline(deadline); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := self.c.Read(buf[bytesInBuffer:])
|
n, err := self.c.Read(buf[bytesInBuffer:])
|
||||||
|
Loading…
Reference in New Issue
Block a user