-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
461 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version, created by Stagehand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:conn_pool/conn_pool.dart'; | ||
|
||
main() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () { | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.