Skip to content

Commit 1d3d896

Browse files
committed
connection timeout override with custom was fixed
1 parent 63b6d78 commit 1d3d896

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

cassandra_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
"time"
4545
"unicode"
4646

47-
inf "gopkg.in/inf.v0"
47+
"gopkg.in/inf.v0"
4848

4949
"github.com/stretchr/testify/require"
5050
)
@@ -3955,3 +3955,46 @@ func TestRoutingKeyCacheUsesOverriddenKeyspace(t *testing.T) {
39553955

39563956
session.Query("DROP KEYSPACE IF EXISTS gocql_test_routing_key_cache").Exec()
39573957
}
3958+
3959+
func TestTimeoutOverride(t *testing.T) {
3960+
session := createSession(t)
3961+
defer session.Close()
3962+
3963+
if session.cfg.ProtoVersion < 3 {
3964+
t.Skip("named Values are not supported in protocol < 3")
3965+
}
3966+
3967+
if err := createTable(session, "CREATE TABLE gocql_test.named_query(id int, value text, PRIMARY KEY (id))"); err != nil {
3968+
t.Fatal(err)
3969+
}
3970+
3971+
// normal case
3972+
err := session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(1, 'value')").Exec()
3973+
if err != nil {
3974+
t.Fatal(err)
3975+
}
3976+
3977+
//decrease Conn.timeout
3978+
session.executor.pool.mu.Lock()
3979+
for _, conPool := range session.executor.pool.hostConnPools {
3980+
conPool.mu.Lock()
3981+
for _, conn := range conPool.conns {
3982+
conn.r.SetTimeout(50)
3983+
}
3984+
conPool.mu.Unlock()
3985+
}
3986+
session.executor.pool.mu.Unlock()
3987+
err = session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(2, 'value')").Exec()
3988+
if err != ErrTimeoutNoResponse {
3989+
t.Fatalf("expected: ErrTimeoutNoResponse, got: %v", err)
3990+
}
3991+
3992+
// override timeout with context
3993+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
3994+
defer cancel()
3995+
err = session.Query("TRUNCATE TABLE gocql_test.named_query").WithContext(ctx).Exec()
3996+
if err != nil {
3997+
t.Fatal(err)
3998+
}
3999+
4000+
}

conn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,8 @@ func (c *Conn) execInternal(ctx context.Context, req frameBuilder, tracer Tracer
12731273
}
12741274

12751275
var timeoutCh <-chan time.Time
1276-
if timeout := c.r.GetTimeout(); timeout > 0 {
1276+
_, isDeadline := ctx.Deadline()
1277+
if timeout := c.r.GetTimeout(); timeout > 0 && !isDeadline {
12771278
if call.timer == nil {
12781279
call.timer = time.NewTimer(0)
12791280
<-call.timer.C

session.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,13 @@ func (q *Query) withContext(ctx context.Context) ExecutableQuery {
11321132
// The provided context controls the entire lifetime of executing a
11331133
// query, queries will be canceled and return once the context is
11341134
// canceled.
1135+
//
1136+
// Can be used to override default timeout for query:
1137+
// Example:
1138+
//
1139+
// ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
1140+
// defer cancel()
1141+
// err = session.Query("SELECT * FROM my_table").WithContext(ctx).Exec()
11351142
func (q *Query) WithContext(ctx context.Context) *Query {
11361143
q2 := *q
11371144
q2.context = ctx

0 commit comments

Comments
 (0)