Skip to content

Commit ffbe565

Browse files
committed
support for large requests/responses
1 parent 6d92fdc commit ffbe565

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

rpc/codec/json.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@ package codec
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net"
7+
"time"
68

79
"github.com/ethereum/go-ethereum/rpc/shared"
810
)
911

1012
const (
11-
MAX_REQUEST_SIZE = 1024 * 1024
13+
MAX_REQUEST_SIZE = 1024 * 1024
1214
MAX_RESPONSE_SIZE = 1024 * 1024
1315
)
1416

1517
// Json serialization support
1618
type JsonCodec struct {
17-
c net.Conn
18-
buffer []byte
19+
c net.Conn
20+
buffer []byte
1921
bytesInBuffer int
2022
}
2123

2224
// Create new JSON coder instance
2325
func NewJsonCoder(conn net.Conn) ApiCoder {
2426
return &JsonCodec{
25-
c: conn,
26-
buffer: make([]byte, MAX_REQUEST_SIZE),
27+
c: conn,
28+
buffer: make([]byte, MAX_REQUEST_SIZE),
2729
bytesInBuffer: 0,
2830
}
2931
}
@@ -58,28 +60,40 @@ func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool,
5860
}
5961

6062
func (self *JsonCodec) ReadResponse() (interface{}, error) {
61-
var err error
63+
bytesInBuffer := 0
6264
buf := make([]byte, MAX_RESPONSE_SIZE)
63-
n, _ := self.c.Read(buf)
6465

65-
var failure shared.ErrorResponse
66-
if err = json.Unmarshal(buf[:n], &failure); err == nil && failure.Error != nil {
67-
return failure, nil
68-
}
66+
deadline := time.Now().Add(15 * time.Second)
67+
self.c.SetDeadline(deadline)
68+
69+
for {
70+
n, err := self.c.Read(buf[bytesInBuffer:])
71+
if err != nil {
72+
return nil, err
73+
}
74+
bytesInBuffer += n
6975

70-
var success shared.SuccessResponse
71-
if err = json.Unmarshal(buf[:n], &success); err == nil {
72-
return success, nil
76+
var success shared.SuccessResponse
77+
if err = json.Unmarshal(buf[:bytesInBuffer], &success); err == nil {
78+
return success, nil
79+
}
80+
81+
var failure shared.ErrorResponse
82+
if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil {
83+
return failure, nil
84+
}
7385
}
7486

75-
return nil, err
87+
self.c.Close()
88+
return nil, fmt.Errorf("Unable to read response")
7689
}
7790

78-
// Encode response to encoded form in underlying stream
91+
// Decode data
7992
func (self *JsonCodec) Decode(data []byte, msg interface{}) error {
8093
return json.Unmarshal(data, msg)
8194
}
8295

96+
// Encode message
8397
func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) {
8498
return json.Marshal(msg)
8599
}

rpc/comms/ipc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type IpcConfig struct {
1616

1717
type ipcClient struct {
1818
endpoint string
19+
c net.Conn
1920
codec codec.Codec
2021
coder codec.ApiCoder
2122
}

rpc/comms/ipc_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
1818
return nil, err
1919
}
2020

21-
return &ipcClient{cfg.Endpoint, codec, codec.New(c)}, nil
21+
return &ipcClient{cfg.Endpoint, c, codec, codec.New(c)}, nil
2222
}
2323

2424
func (self *ipcClient) reconnect() error {

0 commit comments

Comments
 (0)