Last active
August 29, 2015 14:22
-
-
Save spence/86979f5ff15b7c57c6a2 to your computer and use it in GitHub Desktop.
Failing issue with geventwebsocket + newrelic instrumentation. run via `chmod +x run.py && ./run.py` and navigate to http://localhost:9191. After the first websocket connection, no more connections are allowed. Commenting out NR instrumentation restores expected behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# pip install newrelic gevent gevent-websocket flask | |
# my versions: (2.50.0.39) (1.0.2) (0.9.3) (0.10.1) | |
import newrelic.agent | |
from geventwebsocket.handler import WebSocketHandler | |
from gevent.pywsgi import WSGIServer | |
from flask import Flask, request | |
# Flask app | |
app = Flask(__name__) | |
# NewRelic (w/ generic config) | |
newrelic.agent.initialize('newrelic.ini', 'Test') | |
@app.route('/') | |
def index(): | |
return """ | |
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>WebSocket Failure</title> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> | |
</head> | |
<body> | |
<h1>WebSocket Failure</h1> | |
<div id="conn_status">Not Connected</div> | |
<p><button onclick="location.reload()">Reload</button></p> | |
<p> | |
Connected right? Click Refresh (closes the first websocket) and it's | |
unable to connect. Restarting the server is required to connect again. | |
</p> | |
<script type="text/javascript"> | |
$(function() { | |
var ws = new WebSocket("ws://localhost:9191/chat/"); | |
ws.onmessage = function(evt) { | |
$("#placeholder").append('<p>' + evt.data + '</p>'); | |
}; | |
ws.onopen = function(evt) { | |
$('#conn_status').html('<b>Connected</b>'); | |
}; | |
ws.onerror = function(evt) { | |
$('#conn_status').html('<b>Error</b>'); | |
}; | |
ws.onclose = function(evt) { | |
$('#conn_status').html('<b>Closed</b>'); | |
}; | |
}); | |
</script> | |
</body> | |
</html> | |
""" | |
@app.route('/chat/') | |
def api(): | |
if request.environ.get('wsgi.websocket'): | |
websocket = request.environ['wsgi.websocket'] | |
while True: | |
message = websocket.receive() | |
websocket.send(message) | |
return '' | |
# Run server | |
WSGIServer(('0.0.0.0', 9191), app, handler_class=WebSocketHandler).serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment