Repository navigation
Add ability to configure timeout for clearing server resources. #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import ( | |
| "github.com/firebirdsql/fbx/fbproto" | ||
| "github.com/firebirdsql/fbx/fbproto/auth" | ||
| "github.com/firebirdsql/fbx/fbproto/wirecrypt" | ||
| "github.com/firebirdsql/fbx/internal/cleanupctx" | ||
| "github.com/firebirdsql/fbx/internal/operationerror" | ||
| ) | ||
|
|
||
|
|
@@ -1423,7 +1424,7 @@ func (fbConn *FbConn) asyncClose() { | |
| func (fbConn *FbConn) asyncCloseAfterRead(err error) { | ||
| var netErr net.Error | ||
| if errors.As(err, &netErr) && netErr.Timeout() { | ||
| fbConn.asyncCloseWithTimeout(cleanupGracePeriod) | ||
| fbConn.asyncCloseWithTimeout(cleanupTimeout()) | ||
| } else { | ||
| fbConn.asyncClose() | ||
| } | ||
|
|
@@ -2867,14 +2868,25 @@ func (rr *ResultReader) SetCallerErr(err error) { | |
| } | ||
| } | ||
|
|
||
| // cleanupGracePeriod bounds cleanup network calls (e.g. op_free_statement) that | ||
| // must run even after the caller's ctx has been cancelled. | ||
| const cleanupGracePeriod = 5 * time.Second | ||
| // SetCleanupTimeout sets the timeout for the resource cleanup context. | ||
| // The default timeout is 5 seconds. | ||
| // It must be called before using the driver and must not be called | ||
| // concurrently with driver operations. | ||
| // The transmitted timeout must be at least 150 ms. | ||
| func SetCleanupTimeout(timeout time.Duration) error { | ||
| return cleanupctx.SetCleanupTimeout(timeout) | ||
| } | ||
|
|
||
| // cleanupContext detaches from parent cancellation but preserves Values, with a | ||
| // bounded timeout for the network call. | ||
| // cleanupContext returns a context for releasing server-side resources after the | ||
| // user's request has finished. | ||
| func cleanupContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| return context.WithTimeout(context.WithoutCancel(parent), cleanupGracePeriod) | ||
| return cleanupctx.CleanupContext(parent) | ||
| } | ||
|
|
||
| // cleanupTimeout returns a timeout for releasing server-side resources after the | ||
| // user's request has finished. | ||
| func cleanupTimeout() time.Duration { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getCleanupTimeout() |
||
| return cleanupctx.CleanupTimeout() | ||
| } | ||
|
|
||
| // commitInternalTx rolls back a rejected implicit commit so the next request | ||
|
|
@@ -2962,7 +2974,7 @@ func (rr *ResultReader) closeWithoutCommit() (QueryStatus, error) { | |
| rr.fbConn.contextWatcher.Unwatch() | ||
| cleanupWatcher := ctxwatch.NewContextWatcher(&DeadlineContextWatcherHandler{ | ||
| Conn: rr.fbConn.conn, | ||
| DeadlineDelay: cleanupGracePeriod, | ||
| DeadlineDelay: cleanupTimeout(), | ||
| }) | ||
| cleanupWatcher.Watch(rr.ctx) | ||
| defer cleanupWatcher.Unwatch() | ||
|
|
@@ -3602,7 +3614,7 @@ func (h *CancelRequestContextWatcherHandler) HandleUnwatchAfterCancel() { | |
| func (h *CancelRequestContextWatcherHandler) sendCancelOperationWithTimeout(kind int) error { | ||
| timeout := h.CancelResetTimeout | ||
| if timeout <= 0 { | ||
| timeout = 5 * time.Second | ||
| timeout = cleanupTimeout() | ||
|
Comment on lines
-3605
to
+3617
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This timeout is used to enable and disable server cancellation during normal request processing, not just during cleanup. There is already a separate CancelResetTimeout setting. I suggest keeping its five-second default independent from the new cleanup timeout. |
||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| defer cancel() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package cleanupctx | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this file to match the naming style used in the project. We don't use camelCase for filenames in this project. |
||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
| ) | ||
|
|
||
| var cleanupGracePeriod time.Duration = 5 * time.Second | ||
|
|
||
| // SetCleanupTimeout sets the timeout for the resource cleanup context in the event | ||
| // of an error. The default timeout is 5 seconds. | ||
| // It must be called before using the driver and must not be called | ||
| // concurrently with driver operations. | ||
| // The transmitted timeout must be at least 150 ms. | ||
| func SetCleanupTimeout(timeout time.Duration) error { | ||
| if timeout >= 150*time.Millisecond { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the minimum timeout 150 ms? If the driver requires this limit, please explain why. Otherwise, I suggest accepting positive durations, treating zero as the default, and returning an error for negative values. |
||
| cleanupGracePeriod = timeout | ||
| return nil | ||
| } | ||
| return errors.New("timeout must be at least 150ms") | ||
| } | ||
|
|
||
| // Returns the cleanup timeout, which defaults to 5 seconds but can be changed via [SetCleanupTimeout]. | ||
| func CleanupTimeout() time.Duration { | ||
| return cleanupGracePeriod | ||
| } | ||
|
|
||
| // Returns a context for clearing and a cancellation function for it; by default, | ||
| // the context timeout is 5 seconds, but it can be changed via [SetCleanupTimeout]. | ||
| func CleanupContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| return context.WithTimeout(context.WithoutCancel(parent), cleanupGracePeriod) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package cleanupctx_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "testing/synctest" | ||
| "time" | ||
|
|
||
| "github.com/firebirdsql/fbx" | ||
| "github.com/firebirdsql/fbx/fbconn" | ||
| "github.com/firebirdsql/fbx/internal/cleanupctx" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConfiguredCleanupDeadline(t *testing.T) { | ||
| previous := cleanupctx.CleanupTimeout() | ||
| t.Cleanup(func() { require.NoError(t, fbx.SetCleanupTimeout(previous)) }) | ||
| const timeout = 150 * time.Millisecond | ||
| require.NoError(t, fbx.SetCleanupTimeout(timeout)) | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| parent, cancelParent := context.WithCancel(context.Background()) | ||
| cancelParent() | ||
| ctx, cancel := cleanupctx.CleanupContext(parent) | ||
| defer cancel() | ||
|
|
||
| require.NoError(t, ctx.Err()) | ||
| deadline, ok := ctx.Deadline() | ||
| require.True(t, ok) | ||
| require.Equal(t, timeout, time.Until(deadline)) | ||
| }) | ||
| } | ||
|
|
||
| func TestInvalidCleanupTimeoutPreservesSetting(t *testing.T) { | ||
| previous := cleanupctx.CleanupTimeout() | ||
| t.Cleanup(func() { require.NoError(t, fbconn.SetCleanupTimeout(previous)) }) | ||
| const timeout = time.Second | ||
| require.NoError(t, fbconn.SetCleanupTimeout(timeout)) | ||
|
|
||
| require.Error(t, fbconn.SetCleanupTimeout(149*time.Millisecond)) | ||
| require.Equal(t, timeout, cleanupctx.CleanupTimeout()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helper is also used in Batch.Execute to commit a successful batch in autocommit mode. Changing the cleanup timeout now also changes how long that commit can wait, even though no error has occurred. I suggest keeping the existing five-second timeout for this commit separate from the cleanup setting for now.