From 1777b8f42ea3a498fbfe84c31fc33a63955eb30b Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 23 Mar 2018 09:30:41 +0545 Subject: [PATCH] ansible: use DeduplicatingService for ContextService; closes #162. --- ansible_mitogen/services.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index e34914a2..0a576351 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -30,7 +30,7 @@ from __future__ import absolute_import import mitogen.service -class ContextService(mitogen.service.Service): +class ContextService(mitogen.service.DeduplicatingService): """ Used by worker processes connecting back into the top-level process to fetch the single Context instance corresponding to the supplied connection @@ -60,20 +60,11 @@ class ContextService(mitogen.service.Service): """ handle = 500 max_message_size = 1000 + required_args = { + 'method': str + } - def __init__(self, router): - super(ContextService, self).__init__(router) - self._context_by_key = {} - - def validate_args(self, args): - return isinstance(args, dict) - - def dispatch(self, dct, msg): - key = repr(sorted(dct.items())) - dct.pop('discriminator', None) - - if key not in self._context_by_key: - method = getattr(self.router, dct.pop('method')) - self._context_by_key[key] = method(**dct) - return self._context_by_key[key] - + def get_response(self, args): + args.pop('discriminator', None) + method = getattr(self.router, args.pop('method')) + return method(**args)