Skip to content

Commit

Permalink
Merge pull request #48 from wooparadog/add-on-tdecode-exception
Browse files Browse the repository at this point in the history
Add on_tdecode_exception hook to catch decode/encode exceptions
  • Loading branch information
wooparadog committed Oct 30, 2017
2 parents 7584472 + 552904a commit 36dd073
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gunicorn_thrift/config.py
Expand Up @@ -92,6 +92,23 @@ def on_connected(worker, addr):
"""


class TDecodeExceptionRaised(Setting):
name = "on_tdecode_exception"
section = "Server Hooks"
validator = validate_callable(1)
type = six.callable

def on_tdecode_exception(err):
pass

default = staticmethod(on_tdecode_exception)
desc = """\
Called if tdecode exception is raised
The callable needs to accept one variable for the exception raised.
"""


class ClientConnectClosed(Setting):
name = "post_connect_closed"
section = "Server Hooks"
Expand Down
4 changes: 4 additions & 0 deletions gunicorn_thrift/thriftpy_gevent_worker.py
Expand Up @@ -28,6 +28,7 @@
from thriftpy.transport import TTransportException
from thriftpy.protocol.exc import TProtocolException
from thriftpy.protocol.cybin import ProtocolError
from thriftpy.thrift import TDecodeException

from gunicorn.errors import AppImportError
from gunicorn.workers.ggevent import GeventWorker
Expand Down Expand Up @@ -175,6 +176,9 @@ def handle(self, listener, client, addr):
self.log.warning(
"Protocol error, is client(%s) correct? %s", addr, err
)
except TDecodeException as err:
self.log.exception('%r: %r', addr, err)
self.cfg.on_tdecode_exception(err)
except socket.timeout:
self.log.warning('Client timeout: %r', addr)
except socket.error as e:
Expand Down
4 changes: 4 additions & 0 deletions gunicorn_thrift/thriftpy_sync_worker.py
Expand Up @@ -12,6 +12,7 @@

from thriftpy.transport import TSocket
from thriftpy.transport import TTransportException
from thriftpy.thrift import TDecodeException

from gunicorn.errors import AppImportError
from gunicorn.workers.sync import SyncWorker
Expand Down Expand Up @@ -68,6 +69,9 @@ def handle(self, listener, client, addr):
self.notify()
except TTransportException:
pass
except TDecodeException as err:
self.log.exception('%r: %r', addr, err)
self.cfg.on_tdecode_exception(err)
except socket.timeout:
self.log.warning('Client timeout: %r', addr)
except socket.error as e:
Expand Down

0 comments on commit 36dd073

Please sign in to comment.