-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable response timeout on websocket connections #2081
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2081 +/- ##
=============================================
+ Coverage 92.241% 92.253% +0.011%
=============================================
Files 38 38
Lines 3480 3485 +5
Branches 582 583 +1
=============================================
+ Hits 3210 3215 +5
Misses 183 183
Partials 87 87
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I like the added debug messages too.
How can we add a test for this?
@ashleysommer This should do. It was interesting to me that using Here was the working example I also used to visually confirm this works (and breaks without the patch). # server.py
from sanic import Sanic
from sanic.response import redirect
app = Sanic("websocket_example")
app.config.KEEP_ALIVE_TIMEOUT = 1
app.config.REQUEST_TIMEOUT = 1
app.config.RESPONSE_TIMEOUT = 10
app.config.WEBSOCKET_PING_INTERVAL = 1
@app.websocket("/feed")
async def feed(request, ws):
while True:
data = "pong"
print("Sending: " + data)
await ws.send(data)
data = await ws.recv()
print("Received: " + data)
app.static("/index.html", "./index.html")
@app.route("/")
def index(request):
return redirect("/index.html")
if __name__ == "__main__":
app.run(port=8001, debug=True) <!-- index.html -->
<!DOCTYPE html>
<script>
let ws
let countforping = 0
let countforpong = 0
let waitingpong = false
let running = false
async function sleep(seconds = 1) {
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
async function pingPong() {
log(".")
if (countforping > 5) {
log("sending ping")
ws.send("ping")
countforping = 0
waitingpong = true
}
if (waitingpong) {
countforpong += 1
}
if (countforpong > 3) {
log("NO PONG")
countforpong = 0
waitingpong = false
stop()
}
countforping += 1
await sleep()
if (running) {
pingPong()
}
}
function start() {
ws = new WebSocket('ws://' + document.domain + ':' + location.port + '/feed')
ws.onopen = async function (e) {
log("Event: open")
running = true
await sleep(1)
pingPong()
}
ws.onerror = function (e) {
log("Event: error")
console.log({readyState: this.readyState})
}
ws.onmessage = function (e) {
log("Event: incoming: " + e.data)
if (e.data === "pong") {
waitingpong = false
countforpong = 0
}
}
}
function stop() {
ws.close()
running = false
log("ws.close()")
}
function log(msg) {
console.log(msg)
logElem.innerHTML += msg + "<br>"
logElem.scrollTop = logElem.scrollHeight;
}
function reset() {
document.querySelector("#logElem").innerHTML = ""
}
</script>
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
<button onclick="reset()">Clear</button>
<pre id="logElem" style="margin: 6px 0; max-height: 600px; overflow-y: auto;"></pre> |
Will merge and release after I have a closer look at #2079 and if it is something that needs to be addressed now. |
Fixes #2078
This is only a bandaid. I think that when we address #2000 we might need to do something different here. Either incrementing the time or somehow merging the response timeout with the
WEBSOCKET_PING_TIMEOUT
.