dirty python network graphite hacks

I’ve been pushing historical temperature data for Vancouver into a local graphite instance using python. While pushing the data, I received this error:

socket.error: [Errno 99] Cannot assign requested address  

This was strange, as I had been using the address in the loop many, many, MANY times before hitting this error. A little more sleuthing uncovered a ton of connections in the TIME_WAIT state. Around 26,000 open sockets was where I couldn’t open any more.

This little gem, was the fix:

sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,struct.pack('ii', 1, 0))

Normally, after cleanly closing the connecting in python, the system will still hold the connection open for a minute or two in the TIME_WAIT state. This was causing port exhaustion. The above lines told python to send a RST (reset) which will immediately drop the connection.

Graphite complains about the reset, but does log the data.

Connection to the other side was lost in a non-clean fashion.  

Perhaps, not the most elegant solution, but it served my purpose.