comparison mercurial/util.py @ 37052:8c3c47362934

wireproto: implement basic frame reading and processing We just implemented support for writing frames. Now let's implement support for reading them. The bulk of the new code is for a class that maintains the state of a server. Essentially, you construct an instance, feed frames to it, and it tells you what you should do next. The design is inspired by the "sans I/O" movement and the reactor pattern. We don't want to perform I/O or any major blocking event during frame ingestion because this arbitrarily limits ways that server pieces can be implemented. For example, it makes it much harder to swap in an alternate implementation based on asyncio or do crazy things like have requests dispatch to other processes. We do still implement readframe() which does I/O. But it is decoupled from the server reactor. And important parsing of frame headers is a standalone function. So I/O is only needed to obtain frame data. Because testing server-side ingest is useful and difficult on running servers, we create a new "debugreflect" endpoint that will echo back to the client what was received and how it was interpreted. This could be useful for a server admin, someone implementing a client. But immediately, it is useful for testing: we're able to demonstrate that frames are parsed correctly and turned into requests to run commands without having to implement command dispatch on the server! In addition, we implement Python level unit tests for the reactor. This is vastly more efficient than sending requests to the "debugreflect" endpoint and vastly more powerful for advanced testing. Differential Revision: https://phab.mercurial-scm.org/D2852
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 14 Mar 2018 15:25:06 -0700
parents d3a9036d9ae9
children b3079fea3838
comparison
equal deleted inserted replaced
37051:40206e227412 37052:8c3c47362934
2561 data = self._fh.read(min(n, self._left)) 2561 data = self._fh.read(min(n, self._left))
2562 self._left -= len(data) 2562 self._left -= len(data)
2563 assert self._left >= 0 2563 assert self._left >= 0
2564 2564
2565 return data 2565 return data
2566
2567 def readinto(self, b):
2568 res = self.read(len(b))
2569 if res is None:
2570 return None
2571
2572 b[0:len(res)] = res
2573 return len(res)
2566 2574
2567 def stringmatcher(pattern, casesensitive=True): 2575 def stringmatcher(pattern, casesensitive=True):
2568 """ 2576 """
2569 accepts a string, possibly starting with 're:' or 'literal:' prefix. 2577 accepts a string, possibly starting with 're:' or 'literal:' prefix.
2570 returns the matcher name, pattern, and matcher function. 2578 returns the matcher name, pattern, and matcher function.