thriftpy2 icon indicating copy to clipboard operation
thriftpy2 copied to clipboard

connection_lost callback?

Open danieljfarrell opened this issue 6 years ago • 8 comments

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?

danieljfarrell avatar Jun 10 '19 07:06 danieljfarrell

There is still not yet, I will concern about implement it.

ethe avatar Jun 11 '19 09:06 ethe

Thank you!

danieljfarrell avatar Jun 11 '19 10:06 danieljfarrell

I think maybe there are some ways to walk around in current, thriftpy2 will raise a socket exception when connection lost.

ethe avatar Jun 11 '19 10:06 ethe

See https://github.com/Thriftpy/thriftpy2/blob/master/thriftpy2/contrib/aio/socket.py#L342 .

ethe avatar Jun 11 '19 10:06 ethe

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

danieljfarrell avatar Jun 11 '19 13:06 danieljfarrell

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.

ethe avatar Jun 12 '19 03:06 ethe

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

danieljfarrell avatar Aug 30 '19 12:08 danieljfarrell

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.

ethe avatar Aug 30 '19 16:08 ethe