Mercurial > public > mercurial-scm > hg-stable
diff mercurial/wireprotoframing.py @ 39575:07b58266bce3
wireprotov2: implement commands as a generator of objects
Previously, wire protocol version 2 inherited version 1's model of
having separate types to represent the results of different wire
protocol commands.
As I implemented more powerful commands in future commits, I found
I was using a common pattern of returning a special type to hold a
generator. This meant the command function required a closure to
do most of the work. That made logic flow more difficult to follow.
I also noticed that many commands were effectively a sequence of
objects to be CBOR encoded.
I think it makes sense to define version 2 commands as generators.
This way, commands can simply emit the data structures they wish to
send to the client. This eliminates the need for a closure in
command functions and removes encoding from the bodies of commands.
As part of this commit, the handling of response objects has been
moved into the serverreactor class. This puts the reactor in the
driver's seat with regards to CBOR encoding and error handling.
Having error handling in the function that emits frames is
particularly important because exceptions in that function can lead
to things getting in a bad state: I'm fairly certain that uncaught
exceptions in the frame generator were causing deadlocks.
I also introduced a dedicated error type for explicit error reporting
in command handlers. This will be used in subsequent commits.
There's still a bit of work to be done here, especially around
formalizing the error handling "protocol." I've added yet another
TODO to track this so we don't forget.
Test output changed because we're using generators and no longer know
we are at the end of the data until we hit the end of the generator.
This means we can't emit the end-of-stream flag until we've exhausted
the generator. Hence the introduction of 0-sized end-of-stream frames.
Differential Revision: https://phab.mercurial-scm.org/D4472
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 05 Sep 2018 09:06:40 -0700 |
parents | 43d92d68ac88 |
children | 84bf6ded9317 |
line wrap: on
line diff
--- a/mercurial/wireprotoframing.py Mon Aug 27 13:30:44 2018 -0700 +++ b/mercurial/wireprotoframing.py Wed Sep 05 09:06:40 2018 -0700 @@ -388,16 +388,14 @@ def createbytesresponseframesfromgen(stream, requestid, gen, maxframesize=DEFAULT_MAX_FRAME_SIZE): - overall = b''.join(cborutil.streamencode({b'status': b'ok'})) + """Generator of frames from a generator of byte chunks. - yield stream.makeframe(requestid=requestid, - typeid=FRAME_TYPE_COMMAND_RESPONSE, - flags=FLAG_COMMAND_RESPONSE_CONTINUATION, - payload=overall) - + This assumes that another frame will follow whatever this emits. i.e. + this always emits the continuation flag and never emits the end-of-stream + flag. + """ cb = util.chunkbuffer(gen) - - flags = 0 + flags = FLAG_COMMAND_RESPONSE_CONTINUATION while True: chunk = cb.read(maxframesize) @@ -411,12 +409,20 @@ flags |= FLAG_COMMAND_RESPONSE_CONTINUATION - flags ^= FLAG_COMMAND_RESPONSE_CONTINUATION - flags |= FLAG_COMMAND_RESPONSE_EOS - yield stream.makeframe(requestid=requestid, - typeid=FRAME_TYPE_COMMAND_RESPONSE, - flags=flags, - payload=b'') +def createcommandresponseokframe(stream, requestid): + overall = b''.join(cborutil.streamencode({b'status': b'ok'})) + + return stream.makeframe(requestid=requestid, + typeid=FRAME_TYPE_COMMAND_RESPONSE, + flags=FLAG_COMMAND_RESPONSE_CONTINUATION, + payload=overall) + +def createcommandresponseeosframe(stream, requestid): + """Create an empty payload frame representing command end-of-stream.""" + return stream.makeframe(requestid=requestid, + typeid=FRAME_TYPE_COMMAND_RESPONSE, + flags=FLAG_COMMAND_RESPONSE_EOS, + payload=b'') def createcommanderrorresponse(stream, requestid, message, args=None): # TODO should this be using a list of {'msg': ..., 'args': {}} so atom @@ -686,14 +692,69 @@ 'framegen': result, } - def oncommandresponsereadygen(self, stream, requestid, gen): - """Signal that a bytes response is ready, with data as a generator.""" + def oncommandresponsereadyobjects(self, stream, requestid, objs): + """Signal that objects are ready to be sent to the client. + + ``objs`` is an iterable of objects (typically a generator) that will + be encoded via CBOR and added to frames, which will be sent to the + client. + """ ensureserverstream(stream) + # We need to take care over exception handling. Uncaught exceptions + # when generating frames could lead to premature end of the frame + # stream and the possibility of the server or client process getting + # in a bad state. + # + # Keep in mind that if ``objs`` is a generator, advancing it could + # raise exceptions that originated in e.g. wire protocol command + # functions. That is why we differentiate between exceptions raised + # when iterating versus other exceptions that occur. + # + # In all cases, when the function finishes, the request is fully + # handled and no new frames for it should be seen. + def sendframes(): - for frame in createbytesresponseframesfromgen(stream, requestid, - gen): - yield frame + emitted = False + while True: + try: + o = next(objs) + except StopIteration: + if emitted: + yield createcommandresponseeosframe(stream, requestid) + break + + except error.WireprotoCommandError as e: + for frame in createcommanderrorresponse( + stream, requestid, e.message, e.messageargs): + yield frame + break + + except Exception as e: + for frame in createerrorframe(stream, requestid, + '%s' % e, + errtype='server'): + yield frame + + break + + try: + if not emitted: + yield createcommandresponseokframe(stream, requestid) + emitted = True + + # TODO buffer chunks so emitted frame payloads can be + # larger. + for frame in createbytesresponseframesfromgen( + stream, requestid, cborutil.streamencode(o)): + yield frame + except Exception as e: + for frame in createerrorframe(stream, requestid, + '%s' % e, + errtype='server'): + yield frame + + break self._activecommands.remove(requestid)