2

How to enumerate and kill the kept-alive connections using twisted.web.server ?

class Srv(Resource):
    isLeaf = True

    def __init__(self,port):
        self.listener = reactor.listenTCP(port, Site(self))

    def shutdown(self):
        self.listener.stopListening()
        ## HOW TO ENUMERATE AND KILL  OPEN CONNECTIONS

Update: For now, keeping transports in a set,and calling abortConnection() on them inside a try/except.

vrdhn
  • 4,024
  • 3
  • 31
  • 39

1 Answers1

1

Do you keep your connections open the whole time when your webserver is running? For long running connections, you could try using a wrapper like an object pool. That way you just have to shutdown your pool and it will be the responsibility of the object pool to shutdown and clean up all your resources. (database connections in your case)

If you are talking about individual connections: those should have already been disconnected at the end of the request/response call stack.

Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
  • appreciate your answer. This is a small script to test a custom proxy server, and ijust want to kill the connections , before starting the next set of test cases. – vrdhn Feb 12 '14 at 18:38
  • Does your server work with persistent connections? Or do you just want to do a graceful shutdown when the last connection is finished processing and block any further connections from being processed? – Kurt Du Bois Feb 12 '14 at 18:41
  • using 'ss |grep tcp' I find that connections are leftopen. Isn't by default server is HTTP 1.1, so leaves connections open ? Actually I want to connections to be kept alive, as it's part of proxy testing as well. Just that I want a list of live transports! – vrdhn Feb 12 '14 at 18:42
  • Could you post the code where your server keeps open those persistent connections? If you can get a collection of all your connections, you can as you already signaled just loop over that and suspend them. – Kurt Du Bois Feb 12 '14 at 18:45
  • Accroding to nice folks in IRC, using Site(..), this may not be possible. – vrdhn Feb 12 '14 at 18:51
  • It's not "not possible". It's that `Site` doesn't implement this feature for you. You can implement the feature yourself even if you're using `Site`: `Site` doesn't get in the way, it just doesn't do it for you. – Jean-Paul Calderone Feb 12 '14 at 19:28
  • @Jean-PaulCalderone , yes , I agree. How is the Q . – vrdhn Feb 12 '14 at 20:03