Skip to content

Commit 0e0cea3

Browse files
authored
Query count + proper pool check. (#217)
* Query count + proper pool check. * remove todos
1 parent 1cf35f1 commit 0e0cea3

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/src/pool/pool_impl.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ class _PoolConnection implements Connection {
212212
Duration _elapsedInUse = Duration.zero;
213213
DateTime _lastReturned = DateTime.now();
214214
bool _isInUse = false;
215-
int _queryCount = 0;
216215

217216
_PoolConnection(
218217
this._pool, this._endpoint, this._connectionSettings, this._connection);
@@ -235,7 +234,7 @@ class _PoolConnection implements Connection {
235234
if (_elapsedInUse >= _pool._settings.maxSessionUse) {
236235
return true;
237236
}
238-
if (_queryCount >= _pool._settings.maxQueryCount) {
237+
if (_connection.queryCount >= _pool._settings.maxQueryCount) {
239238
return true;
240239
}
241240
return false;
@@ -268,7 +267,6 @@ class _PoolConnection implements Connection {
268267
QueryMode? queryMode,
269268
Duration? timeout,
270269
}) {
271-
_queryCount++;
272270
return _connection.execute(
273271
query,
274272
parameters: parameters,
@@ -280,7 +278,6 @@ class _PoolConnection implements Connection {
280278

281279
@override
282280
Future<Statement> prepare(Object query) {
283-
// TODO: increment query count on statement runs
284281
return _connection.prepare(query);
285282
}
286283

@@ -289,7 +286,6 @@ class _PoolConnection implements Connection {
289286
Future<R> Function(Session session) fn, {
290287
SessionSettings? settings,
291288
}) {
292-
// TODO: increment query count on session callbacks
293289
return _connection.run(fn, settings: settings);
294290
}
295291

@@ -298,7 +294,6 @@ class _PoolConnection implements Connection {
298294
Future<R> Function(Session session) fn, {
299295
TransactionSettings? settings,
300296
}) {
301-
// TODO: increment query count on session callbacks
302297
return _connection.runTx(
303298
fn,
304299
settings: settings,

lib/src/v3/connection.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import 'dart:typed_data';
55

66
import 'package:async/async.dart' as async;
77
import 'package:charcode/ascii.dart';
8+
import 'package:meta/meta.dart';
89
import 'package:pool/pool.dart' as pool;
9-
import 'package:postgres/src/v3/resolved_settings.dart';
1010
import 'package:stream_channel/stream_channel.dart';
1111

1212
import '../../postgres.dart';
@@ -15,10 +15,11 @@ import '../binary_codec.dart';
1515
import '../text_codec.dart';
1616
import 'protocol.dart';
1717
import 'query_description.dart';
18+
import 'resolved_settings.dart';
1819

1920
const _debugLog = false;
2021

21-
String identifier(String source) {
22+
String _identifier(String source) {
2223
// To avoid complex ambiguity rules, we always wrap identifier in double
2324
// quotes. That means the only character we need to escape are double quotes
2425
// in the source.
@@ -110,6 +111,7 @@ abstract class _PgSessionBase implements Session {
110111
}
111112

112113
if (isSimple || (ignoreRows && variables.isEmpty)) {
114+
_connection._queryCount++;
113115
// Great, we can just run a simple query.
114116
final controller = StreamController<ResultRow>();
115117
final items = <ResultRow>[];
@@ -294,9 +296,13 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
294296

295297
var _statementCounter = 0;
296298
var _portalCounter = 0;
299+
var _queryCount = 0;
297300

298301
late final _channels = _Channels(this);
299302

303+
@internal
304+
int get queryCount => _queryCount;
305+
300306
@override
301307
Channels get channels => _channels;
302308

@@ -472,6 +478,7 @@ class _PreparedStatement extends Statement {
472478
Object? parameters, {
473479
Duration? timeout,
474480
}) async {
481+
_session._connection._queryCount++;
475482
timeout ??= _session._settings.queryTimeout;
476483
final items = <ResultRow>[];
477484
final subscription = bind(parameters).listen(items.add);
@@ -763,7 +770,7 @@ class _Channels implements Channels {
763770

764771
void _subscribe(String channel, MultiStreamController firstListener) {
765772
Future(() async {
766-
await _connection.execute(Sql('LISTEN ${identifier(channel)}'),
773+
await _connection.execute(Sql('LISTEN ${_identifier(channel)}'),
767774
ignoreRows: true);
768775
}).onError<Object>((error, stackTrace) {
769776
_activeListeners[channel]?.remove(firstListener);
@@ -782,7 +789,7 @@ class _Channels implements Channels {
782789
_activeListeners.remove(channel);
783790

784791
// Send unlisten command
785-
await _connection.execute(Sql('UNLISTEN ${identifier(channel)}'),
792+
await _connection.execute(Sql('UNLISTEN ${_identifier(channel)}'),
786793
ignoreRows: true);
787794
}
788795
}

0 commit comments

Comments
 (0)