fix(pool): enforce stratum line limits
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
417b967d48
commit
0d7c60726c
2 changed files with 43 additions and 2 deletions
|
|
@ -205,13 +205,17 @@ func (c *StratumClient) writeJSON(value interface{}) error {
|
|||
}
|
||||
|
||||
func (c *StratumClient) readLoop() {
|
||||
reader := bufio.NewReader(c.conn)
|
||||
reader := bufio.NewReaderSize(c.conn, 16384)
|
||||
for {
|
||||
line, errorValue := reader.ReadBytes('\n')
|
||||
line, isPrefix, errorValue := reader.ReadLine()
|
||||
if errorValue != nil {
|
||||
c.notifyDisconnect()
|
||||
return
|
||||
}
|
||||
if isPrefix {
|
||||
c.notifyDisconnect()
|
||||
return
|
||||
}
|
||||
|
||||
response := jsonRPCResponse{}
|
||||
if errorValue = json.Unmarshal(line, &response); errorValue != nil {
|
||||
|
|
|
|||
|
|
@ -31,6 +31,43 @@ func (l *disconnectCountingListener) Count() int {
|
|||
return l.count
|
||||
}
|
||||
|
||||
func TestStratumClient_ReadLoop_Ugly(t *testing.T) {
|
||||
serverConn, clientConn := net.Pipe()
|
||||
defer clientConn.Close()
|
||||
|
||||
listener := &disconnectCountingListener{}
|
||||
client := &StratumClient{
|
||||
listener: listener,
|
||||
conn: serverConn,
|
||||
}
|
||||
|
||||
go client.readLoop()
|
||||
|
||||
payload := make([]byte, 16385)
|
||||
for index := range payload {
|
||||
payload[index] = 'a'
|
||||
}
|
||||
payload = append(payload, '\n')
|
||||
writeErr := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := clientConn.Write(payload)
|
||||
writeErr <- err
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if got := listener.Count(); got != 1 {
|
||||
t.Fatalf("expected oversized line to close the connection, got %d disconnect callbacks", got)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-writeErr:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStratumClient_Disconnect_Good(t *testing.T) {
|
||||
serverConn, clientConn := net.Pipe()
|
||||
defer clientConn.Close()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue