Skip to content

Commit

Permalink
Fix check in maybeReplaceWithLessBusyConnection
Browse files Browse the repository at this point in the history
The inequality in maybeReplaceWithLessBusyConnection was supposed
to check if the least busy connection has at least 20% less inflight
requests compared to heavy loaded connection.

However, this check was incorrect:

if alternative == nil || alternative.AvailableStreams() * 120 > c.AvailableStreams() * 100 {
	return c
}

Since "alternative" is the least busy connection, by definition it has
the largest number of available streams. Therefore,
alternative.AvailableStreams() > c.AvailableStreams() is always true.

This commit rewrites the condition by using a number of in use streams
(inflight requests). The inequality now correctly checks is the least
busy connection has at least 20% less in use streams compared to
the heavy loaded connection.
  • Loading branch information
avelanarius authored and dkropachev committed Jul 5, 2024
1 parent ed9f13a commit 10646e3
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,10 @@ func (c *Conn) AvailableStreams() int {
return c.streams.Available()
}

func (c *Conn) InUseStreams() int {
return c.streams.InUse()
}

func (c *Conn) UseKeyspace(keyspace string) error {
q := &writeQueryFrame{statement: `USE "` + keyspace + `"`}
q.params.consistency = c.session.cons
Expand Down

0 comments on commit 10646e3

Please sign in to comment.