Skip to content

Commit

Permalink
Connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
tejainece committed Jul 7, 2018
1 parent d878148 commit 4690513
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 137 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Files and directories created by pub
.packages
.dart_tool
.pub/
packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
.idea
*.iml
10 changes: 10 additions & 0 deletions conn_pool/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Files and directories created by pub
.dart_tool/
.packages
.pub/
build/
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

# Directory created by dartdoc
doc/api/
3 changes: 3 additions & 0 deletions conn_pool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version, created by Stagehand
22 changes: 22 additions & 0 deletions conn_pool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# conn_pool

A library for Dart developers.

Created from templates made available by Stagehand under a BSD-style
[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE).

## Usage

A simple usage example:

import 'package:conn_pool/conn_pool.dart';

main() {
var awesome = new Awesome();
}

## Features and bugs

Please file feature requests and bugs at the [issue tracker][tracker].

[tracker]: http://example.com/issues/replaceme
15 changes: 15 additions & 0 deletions conn_pool/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
analyzer:
strong-mode: true
# exclude:
# - path/to/excluded/files/**

# Lint rules and documentation, see http://dart-lang.github.io/linter/lints
linter:
rules:
- cancel_subscriptions
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- test_types_in_equals
- unrelated_type_equality_checks
- valid_regexps
5 changes: 5 additions & 0 deletions conn_pool/example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:conn_pool/conn_pool.dart';

main() {

}
8 changes: 8 additions & 0 deletions conn_pool/lib/conn_pool.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library conn_pool;

export 'src/pool.dart';

// TODO: Export any libraries intended for clients of this package.
97 changes: 97 additions & 0 deletions conn_pool/lib/src/counted_set.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:collection';

class CountedSet<T> {
final _set = SplayTreeMap<int, Set<T>>();

final _values = Map<T, int>();

CountedSet();

Iterable<T> get values => _values.keys;

int get length => _values.length;

void add(int count, T value) {
if (_values.containsKey(value)) throw Exception('Already present!');
Set<T> set = _set[count] ??= Set<T>();
_values[value] = count;
set.add(value);
}

void remove(T value) {
if (!_values.containsKey(value)) throw Exception('Not present!');
int count = _values[value];

Set<T> set = _set[count];
if (set == null) {
_values.remove(value);
throw Exception('$value is not located at $count!');
}

if (!set.remove(value)) {
_values.remove(value);
throw Exception('$value is not located at $count!');
}
if (set.isEmpty) _set.remove(count);

_values.remove(value);
}

void inc(T value) {
int count = _values[value];
if (count == null) throw Exception('$value is not present!');

Set<T> set = _set[count];
if (set == null) throw Exception('$value is not located at $count!');
if (!set.remove(value))
throw Exception('$value is not located at $count!');
if(set.isEmpty) _set.remove(count);

count++;
set = _set[count] ??= Set<T>();
_values[value] = count;
set.add(value);
}

void dec(T value) {
int count = _values[value];
if (count == null) throw Exception('$value is not present!');

Set<T> set = _set[count];
if (set == null) throw Exception('$value is not located at $count!');
if (!set.remove(value))
throw Exception('$value is not located at $count!');
if(set.isEmpty) _set.remove(count);

count--;
set = _set[count] ??= Set<T>();
_values[value] = count;
set.add(value);
}

T get leastUsed {
if (_values.length == 0) return null;
int firstKey = _set.firstKey();
return _set[firstKey].first;
}

int numAt(int count) {
Set<T> set = _set[count];
if(set == null) return 0;
return set.length;
}

List<T> removeAllAt(int count) {
Set<T> set = _set[count];
if(set == null) return [];

for(T t in set) _values.remove(t);
_set.remove(count);

return set.toList();
}

bool contains(T value) => _values.containsKey(value);

int countOf(T value) => _values[value];
}
85 changes: 85 additions & 0 deletions conn_pool/lib/src/pool.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'dart:async';
import 'counted_set.dart';

class SharedPool<T> implements Pool<T> {
final ConnectionManager<T> manager;

final int minSize;
final int maxSize;
final _pool = CountedSet<Connection<T>>();

SharedPool(this.manager, {int minSize: 10, int maxSize: 10})
: minSize = minSize,
maxSize = maxSize,
_d = maxSize - minSize;

Future<Connection<T>> _createNew() async {
var conn = Connection._(this);
_pool.add(1, conn);
T n = await manager.open();
conn._connection = n;
return conn;
}

Future<Connection<T>> get() async {
if (_pool.numAt(0) > 0 || _pool.length >= maxSize) {
var conn = _pool.leastUsed;
_pool.inc(conn);
return conn;
}
return _createNew();
}

final int _d;

void release(Connection<T> conn) {
int count = _pool.countOf(conn);
if (count == null || count == 0) return;
_pool.dec(conn);
if (_pool.length != maxSize) return;
if (_pool.numAt(0) < _d) return;
var removes = _pool.removeAllAt(0);
for (var r in removes) {
try {
if (r.isReleased) continue;
r.isReleased = true;
manager.close(r.connection);
} catch (_) {}
}
}
}

class Connection<T> {
/// The connection pool this connection belongs to.
final Pool<T> pool;

/// The underlying connection
T _connection;

bool isReleased = false;

Connection._(this.pool);

T get connection => _connection;

Future<void> release() => pool.release(this);
}

/// Interface to open and close the connection [C]
abstract class ConnectionManager<C> {
/// Establishes and returns a new connection
Future<C> open();

/// Closes provided[connection]
void close(C connection);
}

abstract class Pool<T> {
ConnectionManager<T> get manager;

Future<Connection<T>> get();

FutureOr<void> release(Connection<T> connection);
}

// TODO class ExclusivePool<T> implements Pool<T> {}
11 changes: 11 additions & 0 deletions conn_pool/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: conn_pool
description: A starting point for Dart libraries or applications.
version: 2.1.1
homepage: https://github.com/Jaguar-dart/db
author: Ravi Teja Gudapati <[email protected]>

environment:
sdk: '>=2.0.0-dev.55.0 <2.0.0'

dev_dependencies:
test: ^1.2.0
12 changes: 12 additions & 0 deletions conn_pool/test/conn_pool_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:conn_pool/conn_pool.dart';
import 'package:test/test.dart';

void main() {
group('A group of tests', () {
setUp(() {
});

test('First Test', () {
});
});
}
6 changes: 3 additions & 3 deletions jaguar_mongo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Files and directories created by pub
.packages
.dart_tool
.pub/
packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

.dart_tool
.idea
.idea
*.iml
15 changes: 7 additions & 8 deletions jaguar_mongo/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: jaguar_mongo
version: 1.3.1
version: 2.1.1
description: Mongo interceptor for jaguar
authors:
- Ravi Teja Gudapati <[email protected]>
Expand All @@ -11,12 +11,11 @@ environment:
sdk: ">=1.20.0 <2.0.0"

dependencies:
mongo_dart: ^0.3.0
connection_pool: ">=0.1.2 <0.2.0"
jaguar: ^1.3.4
mongo_dart: ^0.3.2
conn_pool: ^2.1.1
jaguar: ^2.1.1

dev_dependencies:
# test: 0.12.15+10
jaguar_client: ^0.2.0
jaguar_reflect:
http:
test: 1.2.0
jaguar_client: ^2.1.4
jaguar_reflect: ^2.1.1
3 changes: 2 additions & 1 deletion jaguar_postgres/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Files and directories created by pub
.packages
.dart_tool
.pub/
packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
.idea
.dart_tool
*.iml
Loading

0 comments on commit 4690513

Please sign in to comment.