diff options
author | Bas van Kervel <basvankervel@gmail.com> | 2015-06-25 19:18:10 +0800 |
---|---|---|
committer | Bas van Kervel <basvankervel@gmail.com> | 2015-06-25 19:18:10 +0800 |
commit | ffbe5656ff2cba43c813f46f743fde4d1ab2dd58 (patch) | |
tree | f6bf37bd5e76287cfc8f8fe3037cb03d9e224a99 /rpc/codec/json.go | |
parent | 6d92fdc0df12337c2ffb8e6d19c83653f1a00ff2 (diff) | |
download | go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar.gz go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar.bz2 go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar.lz go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar.xz go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.tar.zst go-tangerine-ffbe5656ff2cba43c813f46f743fde4d1ab2dd58.zip |
support for large requests/responses
Diffstat (limited to 'rpc/codec/json.go')
-rw-r--r-- | rpc/codec/json.go | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/rpc/codec/json.go b/rpc/codec/json.go index 380b4cba7..1de649c21 100644 --- a/rpc/codec/json.go +++ b/rpc/codec/json.go @@ -2,28 +2,30 @@ package codec import ( "encoding/json" + "fmt" "net" + "time" "github.com/ethereum/go-ethereum/rpc/shared" ) const ( - MAX_REQUEST_SIZE = 1024 * 1024 + MAX_REQUEST_SIZE = 1024 * 1024 MAX_RESPONSE_SIZE = 1024 * 1024 ) // Json serialization support type JsonCodec struct { - c net.Conn - buffer []byte + c net.Conn + buffer []byte bytesInBuffer int } // Create new JSON coder instance func NewJsonCoder(conn net.Conn) ApiCoder { return &JsonCodec{ - c: conn, - buffer: make([]byte, MAX_REQUEST_SIZE), + c: conn, + buffer: make([]byte, MAX_REQUEST_SIZE), bytesInBuffer: 0, } } @@ -58,28 +60,40 @@ func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, } func (self *JsonCodec) ReadResponse() (interface{}, error) { - var err error + bytesInBuffer := 0 buf := make([]byte, MAX_RESPONSE_SIZE) - n, _ := self.c.Read(buf) - var failure shared.ErrorResponse - if err = json.Unmarshal(buf[:n], &failure); err == nil && failure.Error != nil { - return failure, nil - } + deadline := time.Now().Add(15 * time.Second) + self.c.SetDeadline(deadline) + + for { + n, err := self.c.Read(buf[bytesInBuffer:]) + if err != nil { + return nil, err + } + bytesInBuffer += n - var success shared.SuccessResponse - if err = json.Unmarshal(buf[:n], &success); err == nil { - return success, nil + var success shared.SuccessResponse + if err = json.Unmarshal(buf[:bytesInBuffer], &success); err == nil { + return success, nil + } + + var failure shared.ErrorResponse + if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil { + return failure, nil + } } - return nil, err + self.c.Close() + return nil, fmt.Errorf("Unable to read response") } -// Encode response to encoded form in underlying stream +// Decode data func (self *JsonCodec) Decode(data []byte, msg interface{}) error { return json.Unmarshal(data, msg) } +// Encode message func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) { return json.Marshal(msg) } |