Skip to content

Implement query deadline - #329

Open
byroot wants to merge 1 commit into
trilogy-libraries:mainfrom
byroot:deadline
Open

byroot wants to merge 1 commit into
trilogy-libraries:mainfrom
byroot:deadline

Conversation

@byroot

@byroot byroot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Timeouts are useful to avoid blocking too long on an unresponsive server, but they're impractical to ensure that a unit of work (e.g. a web request) complete within a a reasonable timeline, since they only cap how long a given query can take, not how many queries you can issue as part of that unit of work.

In addition, in Trilogy the timeouts only consider individual reads and write, so a query can actually take much longer than the specified timeout if it involve multiple reads and writes.

Hence the concept of deadline is useful, it allow to keep track of the absolute time at which we'd rather give up than continue.

Example:

class Deadline
  def initialize(timeout)
    @ends_at = Time.now.to_f + timeout
  end

  def remaining
    @ends_at - Time.now.to_f
  end
end

deadline = Deadline.new(10)

client.query('...', deadline: deadline.remaining)
client.query('...', deadline: deadline.remaining)
client.query('...', deadline: deadline.remaining)

Ref: rails/rails#59822

@byroot

byroot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author
  1) Failure:
ClientTest#test_trilogy_query_values_vs_query_allocations [test/client_test.rb:134]:
Expected 23 to be <= 20.

Not sure why those started failing, they pass locally 🤔

@byroot
byroot force-pushed the deadline branch 2 times, most recently from 44de62d to 2d4ebc0 Compare November 20, 2026 11:35
Timeouts are useful to avoid blocking too long on an unresponsive
server, but they're impractical to ensure that a unit of work
(e.g. a web request) complete within a a reasonable timeline,
since they only cap how long a given query can take, not how many
queries you can issue as part of that unit of work.

In addition, in Trilogy the timeouts only consider individual reads
and write, so a query can actually take much longer than the specified
timeout if it involve multiple reads and writes.

Hence the concept of deadline is useful, it allow to keep track
of the absolute time at which we'd rather give up than continue.

Example:

```ruby
class Deadline
  def initialize(timeout)
    @ends_at = Time.now.to_f + timeout
  end

  def remaining
    @ends_at - Time.now.to_f
  end
end

deadline = Deadline.new(10)

client.query('...', deadline: deadline.remaining)
client.query('...', deadline: deadline.remaining)
client.query('...', deadline: deadline.remaining)
```
@byroot

byroot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

I can't reproduce these SEGV locally, even using script/cibuild, but also it crashes in the C client tests even though I haven't touched them, so I suppose it's a pre-existing bitrot issue....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant