Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions conn_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,15 @@ func (c *connect) handle(ctx context.Context, packet byte, on *onProcess) error
if err != nil {
return err
}
if block.Rows() != 0 && on.data != nil {
on.data(block)
if on.data != nil {
if block.Rows() != 0 {
on.data(block)
}
// time spent blocked on a slow consumer must not count against ReadTimeout,
// and empty blocks reset it too
if err := c.startReadWriteTimeout(ctx); err != nil {
return fmt.Errorf("query processing: failed to reset read deadline: %w", err)
}
}
case proto.ServerException:
return c.exception()
Expand Down
61 changes: 61 additions & 0 deletions tests/issues/issue_1672_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package issues

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
)

func TestIssue1672_ReadTimeoutSlowConsumer(t *testing.T) {
const (
readTimeout = time.Second
blockSize = 1000
blocks = 40
blockDelay = 50 * time.Millisecond
)

conn, err := clickhouse_tests.GetConnectionTCPWithOptions("issues", nil, nil, nil, func(o *clickhouse.Options) {
o.ReadTimeout = readTimeout
})
require.NoError(t, err)
t.Cleanup(func() { conn.Close() })

t.Run("slow consumer", func(t *testing.T) {
ctx := clickhouse.Context(context.Background(),
clickhouse.WithBlockBufferSize(1),
clickhouse.WithSettings(clickhouse.Settings{
"max_block_size": blockSize,
}),
)

rows, err := conn.Query(ctx, "SELECT number FROM numbers(?)", blockSize*blocks)
require.NoError(t, err)
defer rows.Close()

start := time.Now()
var count int
for rows.Next() {
var n uint64
require.NoError(t, rows.Scan(&n))
if n%blockSize == 0 {
time.Sleep(blockDelay)
}
count++
}
require.NoError(t, rows.Err())
require.Equal(t, blockSize*blocks, count)
require.Greater(t, time.Since(start), readTimeout)
})

t.Run("slow server", func(t *testing.T) {
var v uint8
err := conn.QueryRow(context.Background(), "SELECT sleep(3)").Scan(&v)
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
})
}
Loading