issue #155: allocate child IDs in batches of 1000.

Avoids a roundtrip for every fork.
pull/167/head
David Wilson 7 years ago
parent 0c77107041
commit f4ba66e3ee

@ -724,16 +724,25 @@ class IdAllocator(object):
finally: finally:
self.lock.release() self.lock.release()
def allocate_block(self):
self.lock.acquire()
try:
id_ = self.next_id
self.next_id += 1000
return id_, id_ + 1000
finally:
self.lock.release()
def on_allocate_id(self, msg): def on_allocate_id(self, msg):
if msg == mitogen.core._DEAD: if msg == mitogen.core._DEAD:
return return
id_ = self.allocate() id_, last_id = self.allocate_block()
requestee = self.router.context_by_id(msg.src_id) requestee = self.router.context_by_id(msg.src_id)
allocated = self.router.context_by_id(id_, msg.src_id) allocated = self.router.context_by_id(id_, msg.src_id)
LOG.debug('%r: allocating %r to %r', self, allocated, requestee) LOG.debug('%r: allocating [%r..%r) to %r', self, allocated, requestee)
msg.reply(id_) msg.reply((id_, last_id))
LOG.debug('%r: publishing route to %r via %r', self, LOG.debug('%r: publishing route to %r via %r', self,
allocated, requestee) allocated, requestee)

@ -37,6 +37,7 @@ import socket
import sys import sys
import termios import termios
import textwrap import textwrap
import threading
import time import time
import zlib import zlib
@ -423,12 +424,24 @@ class Stream(mitogen.core.Stream):
class ChildIdAllocator(object): class ChildIdAllocator(object):
def __init__(self, router): def __init__(self, router):
self.router = router self.router = router
self.lock = threading.Lock()
self.it = iter(xrange(0))
def allocate(self): def allocate(self):
master = mitogen.core.Context(self.router, 0) self.lock.acquire()
return master.send_await( try:
mitogen.core.Message(dst_id=0, handle=mitogen.core.ALLOCATE_ID) for id_ in self.it:
) return id_
master = mitogen.core.Context(self.router, 0)
start, end = master.send_await(
mitogen.core.Message(dst_id=0, handle=mitogen.core.ALLOCATE_ID)
)
self.it = iter(xrange(start, end))
finally:
self.lock.release()
return self.allocate()
class Router(mitogen.core.Router): class Router(mitogen.core.Router):

@ -8,8 +8,20 @@ import id_allocation
class SlaveTest(testlib.RouterMixin, testlib.TestCase): class SlaveTest(testlib.RouterMixin, testlib.TestCase):
def test_slave_allocates_id(self): def test_slave_allocates_id(self):
context = self.router.local() context = self.router.local()
# Master's allocator named the context 1.
self.assertEquals(1, context.context_id)
# First call from slave allocates a block (2..1001)
id_ = context.call(id_allocation.allocate_an_id)
self.assertEqual(id_, 2)
# Second call from slave allocates from block (3..1001)
id_ = context.call(id_allocation.allocate_an_id) id_ = context.call(id_allocation.allocate_an_id)
self.assertEqual(id_, self.router.id_allocator.next_id - 1) self.assertEqual(id_, 3)
# Subsequent master allocation does not collide
c2 = self.router.local()
self.assertEquals(1002, c2.context_id)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save