Commit 92f572a2 authored by Joel Martin's avatar Joel Martin

Fix wsproxy CPU usage without affecting latency.

Instead of selecting on everything every time, only select the writers
that we have items queued for. Most of the time the writers are ready
so if we select on them even when we don't have anything to send we
will fall into a tight loop.
parent 4ce2696d
...@@ -9,7 +9,7 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates ...@@ -9,7 +9,7 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
''' '''
import sys, socket, ssl, time import sys, socket, ssl
from select import select from select import select
from websocket import * from websocket import *
...@@ -32,15 +32,16 @@ def do_proxy(client, target): ...@@ -32,15 +32,16 @@ def do_proxy(client, target):
cqueue = [] cqueue = []
cpartial = "" cpartial = ""
tqueue = [] tqueue = []
socks = [client, target] rlist = [client, target]
while True: while True:
time.sleep(0.01) # 10ms wlist = []
if tqueue: wlist.append(target)
ins, outs, excepts = select(socks, socks, socks, 1) if cqueue: wlist.append(client)
ins, outs, excepts = select(rlist, wlist, [], 1)
if excepts: raise Exception("Socket exception") if excepts: raise Exception("Socket exception")
if tqueue and target in outs: if target in outs:
dat = tqueue.pop(0) dat = tqueue.pop(0)
sent = target.send(dat) sent = target.send(dat)
if sent == len(dat): if sent == len(dat):
...@@ -50,7 +51,7 @@ def do_proxy(client, target): ...@@ -50,7 +51,7 @@ def do_proxy(client, target):
traffic(".>") traffic(".>")
##log.write("Target send: %s\n" % map(ord, dat)) ##log.write("Target send: %s\n" % map(ord, dat))
if cqueue and client in outs: if client in outs:
dat = cqueue.pop(0) dat = cqueue.pop(0)
sent = client.send(dat) sent = client.send(dat)
if sent == len(dat): if sent == len(dat):
......
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