You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit dd1e1ff
Browse filesBrowse the repository at this point in the historyBrowse files
feat: plugin call context for cursor execute arguments
Plugins see the arguments of Cursor.execute and Cursor.executemany but
cannot replace what the target driver receives. Add a call context for
synchronous plugins, read through PluginService.current_call_context. A
plugin assigns new arguments to it before calling execute_func: the
plugins after it and the driver get the new arguments, and the plugins
before it and the caller keep their own.
A replacement is undone when the plugin that made it returns. The
driver's arguments are bound when the call reaches DefaultPlugin, so a
driver call that starts late on a socket_timeout worker still sends
them. Each thread has its own context, and the context's arguments are
cleared when the call ends, so a copy of the thread's context taken
during the call keeps none of them.
Also route cursor loops through the plugins once per loop (as
Cursor.__next__), and return the wrapper cursor from execute when the
driver returns its own, so a fetch chained onto psycopg's execute goes
through the plugins too. Document the call context in the developer
guide and add CHANGELOG entries.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
## [Unreleased]
7
7
8
8
### :magic_wand: Added
9
+
* Synchronous plugins can replace the arguments that `Cursor.execute` and `Cursor.executemany` pass to the plugins after them and to the target driver, without modifying the caller's objects, by assigning new `args` and `kwargs` to the plugin service's `current_call_context`. See [The Call Context](./docs/development-guide/PluginService.md#the-call-context).
9
10
* The `failover` and `failover_v2` plugins currently rewraps all errors to an AwsWrapperError even when the errors do not trigger failover. Set the `preserve_driver_exception_type` configuration parameter to `True` to re-raise the target driver errors as-is for the failover plugins. Failover errors are unaffected and are still raised as `FailoverSuccessError`, `FailoverFailedError` or `TransactionResolutionUnknownError`. This is useful for ORMs to properly classify the errors. ([Issue #1275](https://github.com/aws/aws-advanced-python-wrapper/issues/1275))
10
11
12
+
### :bug: Fixed
13
+
* With psycopg, a fetch chained onto `cursor.execute()` (`cursor.execute(query).fetchone()`) skipped every plugin, the failover and host monitoring plugins included, because `execute()` returned the driver's cursor. It now returns the wrapper's cursor, so `isinstance(cursor.execute(query), psycopg.Cursor)` is now `False`.
14
+
* Loops over a cursor (`for row in cursor`) skipped every plugin. A loop now calls the plugins subscribed to `Cursor.__next__`, and those subscribed to every method, such as `execute_time`, once per loop, not once per row. Like `fetchone`, a loop over a cursor whose connection is no longer the current one now raises `AwsWrapperError`.
|[Host list changed notification pipeline](./Pipelines.md#host-list-changed-notification-pipeline)| notify_host_list_changed |
61
61
62
+
A loop over a cursor (`for row in cursor`) calls the plugins subscribed to `Cursor.__next__` once per loop, not once per row. Such a plugin calls `execute_func()` to get the target driver's iterator and returns an iterator over it, for example a generator that changes each row. The driver's iterator fetches the rows after the plugins have returned, so a server-side cursor still fetches a page at a time, and an error raised while fetching a row does not pass through the plugins' `execute` methods. The asynchronous cursor sends each row of a loop through the plugins as `Cursor.fetchone` instead.
63
+
64
+
With psycopg, `cursor.execute()` returns the wrapper's cursor, so a fetch chained onto it, such as `cursor.execute(query).fetchone()`, goes through the plugins as well.
65
+
62
66
### Tips on Creating a Custom Plugin
63
67
64
68
A custom plugin can subscribe to all Python methods being executed by setting the Plugin's `subscribed_methods` attribute to `{"*"}`. In this case, the plugin will be active in every workflow. We recommend that you be aware of the performance impact of subscribing to all Python methods, especially if your plugin regularly performs demanding tasks for common Python method calls.
@@ -122,6 +126,9 @@ When creating custom plugins, it is important to **avoid** the following bad pra
122
126
- the plugin should always call the pipeline lambdas (i.e. `connect_func: Callable` or `force_connect_func: Callable`)
123
127
4. Running long tasks synchronously:
124
128
- the Python method calls are executed by all subscribed plugins synchronously; if one plugin runs a long task during the execution it blocks the execution for the other plugins
129
+
5. Modifying the arguments a method is called with:
130
+
- the objects passed to a plugin's `execute` belong to the caller (its query parameters, for example) and must not be changed
131
+
- to send different arguments to the plugins after it and to the target driver, a plugin assigns new ones to the [call context](./PluginService.md#the-call-context)
0 commit comments