tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

send-backpressure_wsh.py (1519B)


      1 #!/usr/bin/python
      2 
      3 import time
      4 
      5 # The amount of internal buffering a WebSocket connection has is not
      6 # standardised, and varies depending upon the OS. Setting this number too small
      7 # will result in false negatives, as the entire message gets buffered. Setting
      8 # this number too large will result in false positives, when it takes more than
      9 # 2 seconds to transmit the message anyway. This number was arrived at by
     10 # trial-and-error.
     11 MESSAGE_SIZE = 1024 * 1024
     12 
     13 # With Windows 10 and Python 3, the OS will buffer an entire message in memory
     14 # and return from send() immediately, even if it is very large. To work around
     15 # this problem, send multiple messages.
     16 MESSAGE_COUNT = 16
     17 
     18 
     19 def web_socket_do_extra_handshake(request):
     20    # Turn off permessage-deflate, otherwise it shrinks our big message to a
     21    # tiny message.
     22    request.ws_extension_processors = []
     23 
     24 
     25 def web_socket_transfer_data(request):
     26    # Send empty message to fill the ReadableStream queue
     27    request.ws_stream.send_message(b'', binary=True)
     28 
     29    # TODO(ricea@chromium.org): Use time.perf_counter() when migration to python
     30    # 3 is complete. time.time() can go backwards.
     31    start_time = time.time()
     32 
     33    # The large messages that will be blocked by backpressure.
     34    for i in range(MESSAGE_COUNT):
     35        request.ws_stream.send_message(b' ' * MESSAGE_SIZE, binary=True)
     36 
     37    # Report the time taken to send the large message.
     38    request.ws_stream.send_message(str(time.time() - start_time),
     39                                   binary=False)