Merge lp:~rvb/maas/no-threadpool into lp:~maas-committers/maas/trunk

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: no longer in the source branch.
Merged at revision: 3819
Proposed branch: lp:~rvb/maas/no-threadpool
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 168 lines (+10/-50)
2 files modified
src/maasserver/websockets/protocol.py (+7/-22)
src/maasserver/websockets/tests/test_protocol.py (+3/-28)
To merge this branch: bzr merge lp:~rvb/maas/no-threadpool
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve
Gavin Panella (community) Approve
Review via email: mp+256209@code.launchpad.net

Commit message

Now that the default threadpool is much bigger, there is no reason to use a dedicated threadpool for the websockets.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve
Revision history for this message
Mike Pontillo (mpontillo) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/websockets/protocol.py'
2--- src/maasserver/websockets/protocol.py 2015-03-30 17:22:25 +0000
3+++ src/maasserver/websockets/protocol.py 2015-04-14 20:38:41 +0000
4@@ -37,15 +37,13 @@
5 from maasserver.websockets.listener import PostgresListener
6 from maasserver.websockets.websockets import STATUSES
7 from provisioningserver.utils.twisted import synchronous
8-from twisted.internet import reactor
9 from twisted.internet.defer import inlineCallbacks
10 from twisted.internet.protocol import (
11 Factory,
12 Protocol,
13 )
14-from twisted.internet.threads import deferToThreadPool
15+from twisted.internet.threads import deferToThread
16 from twisted.python import log
17-from twisted.python.threadpool import ThreadPool
18 from twisted.web.server import NOT_DONE_YET
19
20
21@@ -177,9 +175,7 @@
22 "Error authenticating user: %s" % failure.getErrorMessage())
23 return None
24
25- d = deferToThreadPool(
26- reactor, self.factory.threadpool,
27- self.getUserFromSessionId, session_id)
28+ d = deferToThread(self.getUserFromSessionId, session_id)
29 d.addCallbacks(got_user, got_user_error)
30
31 return d
32@@ -258,8 +254,7 @@
33 # performed. The execution of this method is defered to a thread
34 # because it interacts with the database which is blocking.
35 transactional_execute = transactional(handler.execute)
36- d = deferToThreadPool(
37- reactor, self.factory.threadpool,
38+ d = deferToThread(
39 transactional_execute, method, message.get("params", {}))
40 d.addCallbacks(
41 partial(self.sendResult, request_id),
42@@ -305,31 +300,22 @@
43
44
45 class WebSocketFactory(Factory):
46- """Factory for WebSocketProtocol.
47-
48- :ivar threadpool: The thread-pool used for servicing websocket
49- requests.
50- """
51
52 handlers = {}
53 clients = []
54
55 def __init__(self):
56- self.threadpool = ThreadPool(name=self.__class__.__name__)
57 self.listener = PostgresListener()
58 self.cacheHandlers()
59 self.registerNotifiers()
60
61 def startFactory(self):
62- """Start the thread pool and the listener."""
63- self.threadpool.start()
64+ """Start the listener."""
65 return self.listener.start()
66
67 def stopFactory(self):
68- """Stop the thread pool and the listener."""
69- stopped = self.listener.stop()
70- self.threadpool.stop()
71- return stopped
72+ """Stop the listener."""
73+ return self.listener.stop()
74
75 def getSessionEngine(self):
76 """Returns the session engine being used by Django.
77@@ -373,8 +359,7 @@
78 def onNotify(self, handler_class, channel, action, obj_id):
79 for client in self.clients:
80 handler = handler_class(client.user, client.cache)
81- data = yield deferToThreadPool(
82- reactor, self.threadpool,
83+ data = yield deferToThread(
84 self.processNotify, handler, channel, action, obj_id)
85 if data is not None:
86 (name, data) = data
87
88=== modified file 'src/maasserver/websockets/tests/test_protocol.py'
89--- src/maasserver/websockets/tests/test_protocol.py 2015-03-30 22:05:13 +0000
90+++ src/maasserver/websockets/tests/test_protocol.py 2015-04-14 20:38:41 +0000
91@@ -47,14 +47,12 @@
92 from testtools.matchers import (
93 Equals,
94 Is,
95- IsInstance,
96 )
97 from twisted.internet.defer import (
98 fail,
99 inlineCallbacks,
100 )
101 from twisted.internet.threads import deferToThread
102-from twisted.python.threadpool import ThreadPool
103 from twisted.web.server import NOT_DONE_YET
104
105
106@@ -63,8 +61,6 @@
107 def make_protocol(self, patch_authenticate=True, transport_uri=''):
108 self.patch(protocol_module, "PostgresListener")
109 factory = WebSocketFactory()
110- factory.startFactory()
111- self.addCleanup(factory.stopFactory)
112 protocol = factory.buildProtocol(None)
113 protocol.transport = MagicMock()
114 protocol.transport.uri = transport_uri
115@@ -525,9 +521,9 @@
116 self.addCleanup(self.clean_node, node)
117 protocol, factory = self.make_protocol()
118 protocol.user = MagicMock()
119- mock_deferToThreadPool = self.patch_autospec(
120- protocol_module, "deferToThreadPool")
121- mock_deferToThreadPool.return_value = fail(
122+ mock_deferToThread = self.patch_autospec(
123+ protocol_module, "deferToThread")
124+ mock_deferToThread.return_value = fail(
125 maas_factory.make_exception("error"))
126 message = {
127 "type": MSG_TYPE.REQUEST,
128@@ -565,8 +561,6 @@
129
130 def make_protocol_with_factory(self, user=None):
131 factory = WebSocketFactory()
132- factory.startFactory()
133- self.addCleanup(factory.stopFactory)
134 protocol = factory.buildProtocol(None)
135 protocol.transport = MagicMock()
136 if user is None:
137@@ -624,31 +618,12 @@
138
139 @wait_for_reactor
140 @inlineCallbacks
141- def test_startFactory_starts_threadpool(self):
142- factory = WebSocketFactory()
143- yield factory.startFactory()
144- try:
145- self.assertThat(factory.threadpool, IsInstance(ThreadPool))
146- self.expectThat(factory.threadpool.started, Equals(True))
147- finally:
148- yield factory.stopFactory()
149-
150- @wait_for_reactor
151- @inlineCallbacks
152 def test_stopFactory_stops_listener(self):
153 factory = WebSocketFactory()
154 yield factory.startFactory()
155 yield factory.stopFactory()
156 self.expectThat(factory.listener.connected(), Equals(False))
157
158- @wait_for_reactor
159- @inlineCallbacks
160- def test_stopFactory_stops_threadpool(self):
161- factory = WebSocketFactory()
162- yield factory.startFactory()
163- yield factory.stopFactory()
164- self.assertEqual([], factory.threadpool.threads)
165-
166 def test_registerNotifiers_registers_all_notifiers(self):
167 factory = WebSocketFactory()
168 self.assertItemsEqual(