Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
Minor performance fix: Redstone.dart shouldn't create a new `shelf.Pi…
Browse files Browse the repository at this point in the history
…peline` per request.
  • Loading branch information
luizmineo committed Aug 5, 2014
1 parent 722fbea commit cdd3abb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.5.11
* Minor performance fix: Redstone.dart shouldn't create a new `shelf.Pipeline` per request.

## v0.5.10
* Upgraded to di 2.0.1

Expand Down
8 changes: 4 additions & 4 deletions lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ void addPlugin(RedstonePlugin plugin) {
* Middlewares are invoked before any interceptor or route.
*/
void addShelfMiddleware(shelf.Middleware middleware) {
if (_initHandler == null) {
_initHandler = new shelf.Pipeline().addMiddleware(_redstoneMiddleware);
if (_shelfPipeline == null) {
_shelfPipeline = _buildShelfPipeline();
}
_initHandler = _initHandler.addMiddleware(middleware);
_shelfPipeline = _shelfPipeline.addMiddleware(middleware);
}

/**
Expand All @@ -374,7 +374,7 @@ void addShelfMiddleware(shelf.Middleware middleware) {
* completed, and no route is found for the requested URL.
*/
void setShelfHandler(shelf.Handler handler) {
_finalHandler = handler;
_defaultHandler = handler;
}

/**
Expand Down
39 changes: 21 additions & 18 deletions lib/src/server_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class _RequestImpl extends HttpRequestParser implements UnparsedRequest {

}

final shelf.Handler _redstoneHandler = (shelf.Request req) {
return Zone.current[#chain]._handleShelfRequest(req);
};

final shelf.Middleware _redstoneMiddleware = (shelf.Handler handler) {
return (shelf.Request shelfRequest) {
var completer = new Completer();
Expand Down Expand Up @@ -87,6 +91,18 @@ final shelf.Middleware _redstoneMiddleware = (shelf.Handler handler) {
};
};

shelf.Pipeline _buildShelfPipeline() =>
new shelf.Pipeline().addMiddleware(_redstoneMiddleware);

void _buildMainHandler() {
if (_shelfPipeline != null) {
_mainHandler = _shelfPipeline.addHandler(_redstoneHandler);
} else {
_mainHandler = _buildShelfPipeline()
.addHandler(_redstoneHandler);
}
}

void _commitResponse(shelf.Response resp, Completer completer) {
if (!resp.headers.containsKey(HttpHeaders.SERVER)) {
resp = resp.change(headers: const {HttpHeaders.SERVER: "dart:io with Redstone.dart/Shelf"});
Expand Down Expand Up @@ -127,7 +143,7 @@ Future<HttpResponse> _dispatchRequest(UnparsedRequest req) {
List<_Interceptor> interceptors = _getInterceptors(req.httpRequest.uri);
_Target target = _getTarget(req.httpRequest.uri, state);

chain = new _ChainImpl(interceptors, target, req, _initHandler, _finalHandler);
chain = new _ChainImpl(interceptors, target, req);
} catch(e, s) {
_handleError("Failed to handle request.", e, stack: s).then((_) =>
completer.completeError(e, s));
Expand All @@ -143,7 +159,7 @@ void _process(UnparsedRequest req, _RequestState state,
_ChainImpl chain, Completer completer) {
runZoned(() {

shelf_io.handleRequest(req.httpRequest, chain._initHandler).then((_) {
shelf_io.handleRequest(req.httpRequest, _mainHandler).then((_) {
_logger.finer("Closed request for: ${request.url}");
completer.complete(req.httpRequest.response);
});
Expand Down Expand Up @@ -259,9 +275,6 @@ Future _runTarget(_Target target, UnparsedRequest req, shelf.Handler handler) {

class _ChainImpl implements Chain {

shelf.Handler _initHandler;
shelf.Handler _finalHandler;

List<_Interceptor> _interceptors;
_Target _target;

Expand All @@ -276,19 +289,9 @@ class _ChainImpl implements Chain {

List _callbacks = [];

_ChainImpl(this._interceptors, this._target, this._request,
shelf.Pipeline initHandler, this._finalHandler) {

if (initHandler != null) {
_initHandler = initHandler.addHandler(_handler);
} else {
_initHandler = new shelf.Pipeline()
.addMiddleware(_redstoneMiddleware)
.addHandler(_handler);
}
}
_ChainImpl(this._interceptors, this._target, this._request);

Future _handler(shelf.Request req) {
Future _handleShelfRequest(shelf.Request req) {
_request.shelfRequest = req;
_request.attributes.addAll(req.context);
Zone.current[#state].chainInitialized = true;
Expand Down Expand Up @@ -369,7 +372,7 @@ class _ChainImpl implements Chain {
_currentInterceptor = null;
_targetInvoked = true;
new Future(() {
_runTarget(_target, request, _finalHandler).then((_) {
_runTarget(_target, request, _defaultHandler).then((_) {
if (!_interrupted && !Zone.current[#state].requestAborted) {
return _invokeCallbacks();
}
Expand Down
16 changes: 11 additions & 5 deletions lib/src/setup_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final Map<Type, ResponseProcessor> _responseProcessors = {};
final Map<Type, RouteWrapper> _routeWrappers = {};
final Set<Type> _groupAnnotations = new Set();

shelf.Pipeline _initHandler = null;
shelf.Handler _finalHandler = null;
shelf.Pipeline _shelfPipeline = null;
shelf.Handler _defaultHandler = null;

shelf.Handler _mainHandler = null;

final Set<Symbol> _blacklistSet = _buildBlacklistSet();

Expand Down Expand Up @@ -393,6 +395,9 @@ void _scanHandlers([List<Symbol> libraries]) {

//install plugins
manager._installPlugins();

//install shelf handler
_buildMainHandler();

_targets.addAll(_targetsCache.values.map((t) {
if (t is _TargetWrapper) {
Expand Down Expand Up @@ -449,9 +454,10 @@ void _clearHandlers() {
_routeWrappers.clear();
_groupAnnotations.clear();

_initHandler = null;
_finalHandler = null;

_shelfPipeline = null;
_defaultHandler = null;
_mainHandler = null;

}

void _configureGroup(_ServerMetadataImpl serverMetadata,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: redstone
version: 0.5.10
version: 0.5.11
author: Luiz Mineo <[email protected]>
description: A metadata driven microframework for Dart
homepage: http://redstonedart.org
Expand Down

0 comments on commit cdd3abb

Please sign in to comment.