Skip to content

Commit 570c26e

Browse files
committed
Endless query execution fix
1 parent c75ff5f commit 570c26e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

policies.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func (host *selectedHost) Info() *HostInfo {
323323
func (host *selectedHost) Mark(err error) {}
324324

325325
// NextHost is an iteration function over picked hosts
326+
// Should return nil if SelectedHost is not Up to prevent endless query execution.
326327
type NextHost func() SelectedHost
327328

328329
// RoundRobinHostPolicy is a round-robin load balancing policy, where each host

query_executor.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package gocql
2727
import (
2828
"context"
2929
"sync"
30+
"sync/atomic"
3031
"time"
3132
)
3233

@@ -89,14 +90,17 @@ func (q *queryExecutor) executeQuery(qry ExecutableQuery) (*Iter, error) {
8990
// check if the host id is specified for the query,
9091
// if it is, the query should be executed at the corresponding host.
9192
if hostID := qry.GetHostID(); hostID != "" {
93+
pool, ok := q.pool.getPoolByHostID(hostID)
94+
if !ok {
95+
return nil, ErrNoConnections
96+
}
97+
host := pool.host
98+
var returnedHostOnce int32 = 0
9299
hostIter = func() SelectedHost {
93-
pool, ok := q.pool.getPoolByHostID(hostID)
94-
// if the specified host is down
95-
// we return nil to avoid endless query execution in queryExecutor.do()
96-
if !ok || !pool.host.IsUp() {
97-
return nil
100+
if atomic.CompareAndSwapInt32(&returnedHostOnce, 0, 1) {
101+
return (*selectedHost)(host)
98102
}
99-
return (*selectedHost)(pool.host)
103+
return nil
100104
}
101105
}
102106

0 commit comments

Comments
 (0)