Skip to content

Commit 7c0d0ba

Browse files
authored
Optimize StackTrace capture (#432)
1 parent 9a96878 commit 7c0d0ba

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Upgraded SDK constraints and lints.
66
- Supporting more URL-based connection-string parameters (mostly for pool).
7+
- Optimized `StackTrace` capture [#432](https://github.com/isoos/postgresql-dart/pull/432) by [gmpassos](https://github.com/gmpassos).
78

89
## 3.5.7.
910

lib/src/v3/connection.dart

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ abstract class _PgSessionBase implements Session {
8686

8787
/// Sends a message to the server and waits for a response [T], gracefully
8888
/// handling error messages that might come in instead.
89-
Future<T> _sendAndWaitForQuery<T extends ServerMessage>(ClientMessage send) {
90-
final trace = StackTrace.current;
89+
Future<T> _sendAndWaitForQuery<T extends ServerMessage>(
90+
ClientMessage send, {
91+
StackTrace? stackTrace,
92+
}) {
93+
final trace = stackTrace ?? StackTrace.current;
9194

9295
return _withResource(() {
9396
_connection._channel.sink.add(
@@ -193,7 +196,8 @@ abstract class _PgSessionBase implements Session {
193196
}
194197

195198
Future<_PreparedStatement> _prepare(Object query) async {
196-
final trace = Trace.current();
199+
final stackTrace = StackTrace.current;
200+
final trace = Trace.from(stackTrace);
197201
final conn = _connection;
198202
final name = 's/${conn._statementCounter++}';
199203
final description = InternalQueryDescription.wrap(
@@ -207,6 +211,7 @@ abstract class _PgSessionBase implements Session {
207211
statementName: name,
208212
typeOids: description.parameterTypes?.map((e) => e?.oid).toList(),
209213
),
214+
stackTrace: stackTrace,
210215
);
211216

212217
return _PreparedStatement(description, name, this, trace);
@@ -710,18 +715,23 @@ class _PreparedStatement extends Statement {
710715

711716
@override
712717
Future<Result> run(Object? parameters, {Duration? timeout}) async {
718+
final stackTrace = StackTrace.current;
719+
final trace = Trace.from(stackTrace);
713720
_session._connection._queryCount++;
714721
timeout ??= _session._settings.queryTimeout;
715722
final items = <ResultRow>[];
716-
final subscription = bind(parameters).listen(items.add);
723+
final subscription = (bind(parameters) as _BoundStatement).listen(
724+
items.add,
725+
callerTrace: trace,
726+
);
717727
try {
718728
return await (subscription as _PgResultStreamSubscription)._waitForResult(
719729
items: items,
720730
timeout: timeout,
721731
);
722732
} finally {
723733
await subscription.cancel();
724-
await _closePendingPortals();
734+
await _closePendingPortals(stackTrace: stackTrace);
725735
}
726736
}
727737

@@ -741,12 +751,13 @@ class _PreparedStatement extends Statement {
741751
_portalsToClose!.add(portalName);
742752
}
743753

744-
Future<void> _closePendingPortals() async {
754+
Future<void> _closePendingPortals({StackTrace? stackTrace}) async {
745755
final list = _portalsToClose;
746756
while (list != null && list.isNotEmpty) {
747757
final portalName = list.removeFirst();
748758
await _session._sendAndWaitForQuery<CloseCompleteMessage>(
749759
CloseMessage.portal(portalName),
760+
stackTrace: stackTrace,
750761
);
751762
}
752763
}
@@ -764,6 +775,7 @@ class _BoundStatement extends Stream<ResultRow> implements ResultStream {
764775
Function? onError,
765776
void Function()? onDone,
766777
bool? cancelOnError,
778+
Trace? callerTrace,
767779
}) {
768780
final controller = StreamController<ResultRow>();
769781

@@ -774,7 +786,12 @@ class _BoundStatement extends Stream<ResultRow> implements ResultStream {
774786
onDone: onDone,
775787
cancelOnError: cancelOnError,
776788
);
777-
return _PgResultStreamSubscription(this, controller, subscription);
789+
return _PgResultStreamSubscription(
790+
this,
791+
controller,
792+
subscription,
793+
callerTrace: callerTrace,
794+
);
778795
}
779796
}
780797

@@ -803,12 +820,13 @@ class _PgResultStreamSubscription
803820
_PgResultStreamSubscription(
804821
_BoundStatement statement,
805822
this._controller,
806-
this._source,
807-
) : session = statement.statement._session,
808-
ignoreRows = false,
809-
_boundStatement = statement,
810-
_parentTrace = statement.statement._trace,
811-
_callerTrace = Trace.current() {
823+
this._source, {
824+
Trace? callerTrace,
825+
}) : session = statement.statement._session,
826+
ignoreRows = false,
827+
_boundStatement = statement,
828+
_parentTrace = statement.statement._trace,
829+
_callerTrace = callerTrace ?? Trace.current() {
812830
_scheduleStatement(() async {
813831
connection._pending = this;
814832

@@ -847,9 +865,10 @@ class _PgResultStreamSubscription
847865
this._controller,
848866
this._source,
849867
this.ignoreRows, {
868+
Trace? callerTrace,
850869
void Function()? cleanup,
851870
}) : _parentTrace = null,
852-
_callerTrace = Trace.current() {
871+
_callerTrace = callerTrace ?? Trace.current() {
853872
_scheduleStatement(() async {
854873
connection._pending = this;
855874

0 commit comments

Comments
 (0)