/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/medium.py

  • Committer: Andrew Bennetts
  • Date: 2010-03-05 07:27:58 UTC
  • mto: This revision was merged to the branch mainline in revision 5117.
  • Revision ID: andrew.bennetts@canonical.com-20100305072758-9il5cvpa4gjo8tba
Try a bit harder to stop a SmartTCPServer sooner when _should_terminate has been set.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
bzrlib/transport/smart/__init__.py.
25
25
"""
26
26
 
 
27
import errno
27
28
import os
 
29
import socket
28
30
import sys
29
31
import urllib
30
32
 
31
33
from bzrlib.lazy_import import lazy_import
32
34
lazy_import(globals(), """
33
35
import atexit
34
 
import socket
35
36
import thread
36
37
import weakref
37
38
 
46
47
from bzrlib.smart import client, protocol, request, vfs
47
48
from bzrlib.transport import ssh
48
49
""")
 
50
#usually already imported, and getting IllegalScoperReplacer on it here.
49
51
from bzrlib import osutils
50
52
 
 
53
# We must not read any more than 64k at a time so we don't risk "no buffer
 
54
# space available" errors on some platforms.  Windows in particular is likely
 
55
# to throw WSAECONNABORTED or WSAENOBUFS if given too much data at once.
51
56
# Throughout this module buffer size parameters are either limited to be at
52
 
# most _MAX_READ_SIZE, or are ignored and _MAX_READ_SIZE is used instead.
53
 
# For this module's purposes, MAX_SOCKET_CHUNK is a reasonable size for reads
54
 
# from non-sockets as well.
55
 
_MAX_READ_SIZE = osutils.MAX_SOCKET_CHUNK
 
57
# most 64k, or are ignored and 64k is used instead.
 
58
_MAX_READ_SIZE = 64 * 1024
 
59
 
56
60
 
57
61
def _get_protocol_factory_for_bytes(bytes):
58
62
    """Determine the right protocol factory for 'bytes'.
274
278
    def _serve_one_request_unguarded(self, protocol):
275
279
        while protocol.next_read_size():
276
280
            # We can safely try to read large chunks.  If there is less data
277
 
            # than MAX_SOCKET_CHUNK ready, the socket will just return a
278
 
            # short read immediately rather than block.
279
 
            bytes = self.read_bytes(osutils.MAX_SOCKET_CHUNK)
 
281
            # than _MAX_READ_SIZE ready, the socket wil just return a short
 
282
            # read immediately rather than block.
 
283
            bytes = self.read_bytes(_MAX_READ_SIZE)
280
284
            if bytes == '':
281
285
                self.finished = True
282
286
                return
285
289
        self._push_back(protocol.unused_data)
286
290
 
287
291
    def _read_bytes(self, desired_count):
288
 
        return osutils.read_bytes_from_socket(
289
 
            self.socket, self._report_activity)
 
292
        return _read_bytes_from_socket(self.socket, self._report_activity)
290
293
 
291
294
    def terminate_due_to_error(self):
292
295
        # TODO: This should log to a server log file, but no such thing
607
610
            # which is newer than a previously supplied older-than version.
608
611
            # This indicates that some smart verb call is not guarded
609
612
            # appropriately (it should simply not have been tried).
610
 
            trace.mutter(
 
613
            raise AssertionError(
611
614
                "_remember_remote_is_before(%r) called, but "
612
615
                "_remember_remote_is_before(%r) was called previously."
613
 
                , version_tuple, self._remote_version_is_before)
614
 
            if 'hpss' in debug.debug_flags:
615
 
                ui.ui_factory.show_warning(
616
 
                    "_remember_remote_is_before(%r) called, but "
617
 
                    "_remember_remote_is_before(%r) was called previously."
618
 
                    % (version_tuple, self._remote_version_is_before))
619
 
            return
 
616
                % (version_tuple, self._remote_version_is_before))
620
617
        self._remote_version_is_before = version_tuple
621
618
 
622
619
    def protocol_version(self):
717
714
    This client does not manage the pipes: it assumes they will always be open.
718
715
 
719
716
    Note that if readable_pipe.read might raise IOError or OSError with errno
720
 
    of EINTR, it must be safe to retry the read.  Plain CPython fileobjects
721
 
    (such as used for sys.stdin) are safe.
 
717
    of EINTR, it must be safe to retry the read.
722
718
    """
723
719
 
724
720
    def __init__(self, readable_pipe, writeable_pipe, base):
906
902
        """See SmartClientMedium.read_bytes."""
907
903
        if not self._connected:
908
904
            raise errors.MediumNotConnected(self)
909
 
        return osutils.read_bytes_from_socket(
910
 
            self._socket, self._report_activity)
 
905
        return _read_bytes_from_socket(self._socket, self._report_activity)
911
906
 
912
907
 
913
908
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
950
945
        self._medium._flush()
951
946
 
952
947
 
 
948
def _read_bytes_from_socket(sock, report_activity,
 
949
        max_read_size=_MAX_READ_SIZE):
 
950
    """Read up to max_read_size of bytes from sock and notify of progress.
 
951
 
 
952
    Translates "Connection reset by peer" into file-like EOF (return an
 
953
    empty string rather than raise an error), and repeats the recv if
 
954
    interrupted by a signal.
 
955
    """
 
956
    while 1:
 
957
        try:
 
958
            bytes = sock.recv(_MAX_READ_SIZE)
 
959
        except socket.error, e:
 
960
            eno = e.args[0]
 
961
            if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
 
962
                # The connection was closed by the other side.  Callers expect
 
963
                # an empty string to signal end-of-stream.
 
964
                return ""
 
965
            elif eno == errno.EINTR:
 
966
                # Retry the interrupted recv.
 
967
                continue
 
968
            raise
 
969
        else:
 
970
            report_activity(len(bytes), 'read')
 
971
            return bytes
 
972
 
 
973