# OpenSSL / Persistent connection

**URL:** <https://www.ruby-forum.com/t/openssl-persistent-connection/172243>\
**Category:** Ruby\
**Created:** [July 17, 2009, 10:31am UTC](https://www.ruby-forum.com/t/openssl-persistent-connection/172243 "2009-07-17T10:31:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabien\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@Fabien\_P](https://www.ruby-forum.com/u/Fabien_P)\
**Post date:** [July 17, 2009, 10:31am UTC](https://www.ruby-forum.com/t/openssl-persistent-connection/172243/1 "2009-07-17T10:31:03Z")

</div>

Hi.

I want to have a persistent OpenSSL over a TCPSocket connection. The  
idea is having a rails website, a backgroundrb daemon. I do keep the  
connections within Model.something and I reuse them when needed.

Sadly the OpenSSL socket does not generate any timeout, etc, when the  
server side disconnected (I don’t know if it did but it doesn’t work  
anymore so I guess it did).

OpenSSL documentation is non existent, does anyone has a hint how to  
do that ? As of today, this is how I connect :

```
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.key = OpenSSL::PKey::RSA.new(cert, self.passphrase)
  ctx.cert = OpenSSL::X509::Certificate.new(cert)
  ctx.timeout = 7200

  s = TCPSocket.new(host, PORT)
  ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
  ssl.sync = true
  ssl.sync_close = true
  ssl.connect

```

Then when I use it I have (conn has the persistent connection) :

```
  (0..10).each do
    begin
      conn[:ssl].write("something")
      conn[:ssl].flush
      conn[:socket].flush

      break
    rescue OpenSSL::SSL::SSLError => e
      self.reconnecting
    rescue Errno::EPIPE => e
      self.reconnecting
    rescue Errno::ENETDOWN => e
      self.reconnecting
    end
  end

```

Does that look good enough ? I get no error when the server is not  
available anymore, sadly.

---

<div class="post-metadata">

**Author:** ![Fabien\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@Fabien\_P](https://www.ruby-forum.com/u/Fabien_P)\
**Post date:** [July 18, 2009, 12:27pm UTC](https://www.ruby-forum.com/t/openssl-persistent-connection/172243/2 "2009-07-18T12:27:11Z")

</div>

try ?  
begin  
s = TCPSocket.new(host, PORT)  
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)  
ssl.sync = true  
ssl.sync\_close = true  
ssl.connect  
rescue  
…  
end
