Commit dd3bb8b4 authored by Kirill Smelkov's avatar Kirill Smelkov

X py: Adjust msg_id a bit so it behaves like stream_id in HTTP/2

- even for server initiated streams
- odd  for client initiated streams

This way I will be able to use Pkt.msg_id as real stream_id in go's Conn
because with even / odd scheme there is no possibility for id conflicts
in between two peers.
parent 8d49cd60
...@@ -340,7 +340,8 @@ class Connection(BaseConnection): ...@@ -340,7 +340,8 @@ class Connection(BaseConnection):
def __init__(self, event_manager, *args, **kw): def __init__(self, event_manager, *args, **kw):
BaseConnection.__init__(self, event_manager, *args, **kw) BaseConnection.__init__(self, event_manager, *args, **kw)
self.read_buf = ReadBuffer() self.read_buf = ReadBuffer()
self.cur_id = 0 # NOTE cur_id will be set in Server|Client to maintain `cur_id % 2 == const` invariant
#self.cur_id = 0
self.aborted = False self.aborted = False
self.uuid = None self.uuid = None
self._queue = [] self._queue = []
...@@ -367,6 +368,7 @@ class Connection(BaseConnection): ...@@ -367,6 +368,7 @@ class Connection(BaseConnection):
return self.server return self.server
def asClient(self): def asClient(self):
# TODO adjust .cur_id % 2 to be as client
try: try:
del self.idle del self.idle
assert self.client assert self.client
...@@ -374,6 +376,7 @@ class Connection(BaseConnection): ...@@ -374,6 +376,7 @@ class Connection(BaseConnection):
self.client = True self.client = True
def asServer(self): def asServer(self):
# TODO adjust .cur_id % 2 to be as server
self.server = True self.server = True
def _closeClient(self): def _closeClient(self):
...@@ -406,7 +409,7 @@ class Connection(BaseConnection): ...@@ -406,7 +409,7 @@ class Connection(BaseConnection):
def _getNextId(self): def _getNextId(self):
next_id = self.cur_id next_id = self.cur_id
self.cur_id = (next_id + 1) & 0xffffffff self.cur_id = (next_id + 2) & 0xffffffff
return next_id return next_id
def updateTimeout(self, t=None): def updateTimeout(self, t=None):
...@@ -599,6 +602,7 @@ class ClientConnection(Connection): ...@@ -599,6 +602,7 @@ class ClientConnection(Connection):
"""A connection from this node to a remote node.""" """A connection from this node to a remote node."""
client = True client = True
cur_id = 1 # cur_id % 2 is 1 for client initiated "streams"
def __init__(self, app, handler, node): def __init__(self, app, handler, node):
self._ssl = app.ssl self._ssl = app.ssl
...@@ -660,6 +664,7 @@ class ServerConnection(Connection): ...@@ -660,6 +664,7 @@ class ServerConnection(Connection):
KEEP_ALIVE = Connection.KEEP_ALIVE + 5 KEEP_ALIVE = Connection.KEEP_ALIVE + 5
server = True server = True
cur_id = 0 # cur_id % 2 is 0 for server initated "streams"
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
Connection.__init__(self, *args, **kw) Connection.__init__(self, *args, **kw)
......
...@@ -77,14 +77,14 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -77,14 +77,14 @@ class ConnectionTests(NeoUnitTestBase):
use_case_list = ( use_case_list = (
# (a) For a single packet sent at T, # (a) For a single packet sent at T,
# the limit time for the answer is T + (1 * CRITICAL_TIMEOUT) # the limit time for the answer is T + (1 * CRITICAL_TIMEOUT)
((), (1., 0)), ((), (1., 1)),
# (b) Same as (a), even if send another packet at (T + CT/2). # (b) Same as (a), even if send another packet at (T + CT/2).
# But receiving a packet (at T + CT - ε) resets the timeout # But receiving a packet (at T + CT - ε) resets the timeout
# (which means the limit for the 2nd one is T + 2*CT) # (which means the limit for the 2nd one is T + 2*CT)
((.5, None), (1., 0, 2., 1)), ((.5, None), (1., 1, 2., 3)),
# (c) Same as (b) with a first answer at well before the limit # (c) Same as (b) with a first answer at well before the limit
# (T' = T + CT/2). The limit for the second one is T' + CT. # (T' = T + CT/2). The limit for the second one is T' + CT.
((.1, None, .5, 1), (1.5, 0)), ((.1, None, .5, 3), (1.5, 1)),
) )
def set_time(t): def set_time(t):
...@@ -106,7 +106,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -106,7 +106,7 @@ class ConnectionTests(NeoUnitTestBase):
try: try:
for use_case, expected in use_case_list: for use_case, expected in use_case_list:
i = iter(use_case) i = iter(use_case)
conn.cur_id = 0 conn.cur_id = 1 # XXX -> conn._reset() ?
set_time(0) set_time(0)
# No timeout when no pending request # No timeout when no pending request
self.assertEqual(conn._handlers.getNextTimeout(), None) self.assertEqual(conn._handlers.getNextTimeout(), None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment