thriftpy2
thriftpy2 copied to clipboard
connection_lost callback?
Transports in Twisted and asyncio have a connection_lost callback. Is there an equivalent callback in thiftpy2 which gets called when a transport loses a connection?
There is still not yet, I will concern about implement it.
Thank you!
I think maybe there are some ways to walk around in current, thriftpy2 will raise a socket exception when connection lost.
See https://github.com/Thriftpy/thriftpy2/blob/master/thriftpy2/contrib/aio/socket.py#L342 .
So, for now, one needs to catch a potential socket exception on every call. I suppose that is good practice!
What do you think about the following?
I could subclass,
https://github.com/Thriftpy/thriftpy2/blob/4065b80530107744172889ca909ce9270ac2f6d6/thriftpy2/contrib/aio/protocol/binary.py#L232
and then implement the lost_connection method from the asyncio.BaseProtocol and supply the custom protocol when, when constructing my client
https://github.com/Thriftpy/thriftpy2/blob/4065b80530107744172889ca909ce9270ac2f6d6/thriftpy2/contrib/aio/rpc.py#L13
I think making TAsyncBinaryProtocol inherit from asyncio.BaseProtocol directly is fine to me, and you can overload the connection_lost method to customize the behavior.
However, I think it is a little bit hard to realize, cause the design and structure of server, protocol and transport in thriftpy2 are completely different from asyncio.Protocol, it is almost a refactoring rather than an enhancement.
OK, maybe it causes a bit too many changes to inherit from asyncio.Protocol. Strictly speaking, it is not required to implement the functionality.
How would you recommend to implement this notification? I'm not roo familiar with socket programming.
For example, could something simple be added here? Such as a callback?
https://github.com/Thriftpy/thriftpy2/blob/f4aa812fd97d1786c51de0c04326dadf1e7dcf16/thriftpy2/contrib/aio/socket.py#L342
Yes, it could be. Passing into a call back function by the user and check if there is a call back function exists is feasible.