althepal has asked for the wisdom of the Perl Monks concerning the following question:
I think I've noticed a problem with IO::Socket::SSL, and I think I have a solution for it for my case, but I'd like to know what people think.
I have a server I've written in perl, and I wanted to make it work with SSL, so I used this very convenient module. Testing seemed to tell me that everything was working great. I was amazed at how easy it was. But then I noticed that many of the real-life SSL connections were timing out.
I don't use <>, or getline() to read from the socket. Instead, for greater control, I use select() to see if there's data waiting, then sysread() to read the data. Like this:
if (select($rout=$rin, undef, undef, $timeout)) { unless ($length = sysread($socket, $buf="", 1024)) { #connection error } # handle data in $buf } else { # timeout error }
It's more complicated than this, but this is the gist of it. Fyi, I've found through benchmarking that this method of reading the socket has considerably better performance than using an alarm to time out getline on the socket.
After a lot of debugging and trying different things, I realized that if I read in a larger data size with sysread (4096 instead of 1024), the timeout problem went away.
I think that what's happening is this: The SSL module must be reading ahead on the socket to do the proper decoding, so while sysread will return data, the select function does not see that the socket has data to read, because the SSL module has already cleared out the actual buffer. I don't understand exactly how the SSL module disguises the SSL socket as a normal socket, so I'm guessing here.
So, everything seems to work now with the larger value for sysread, but I'm concerned that I'm just getting lucky. I'm afraid that some length of data will cause the same problem, whereby select will tell me that no data is available, but sysread will still return data.
So what do you think? Is this going to come back to haunt me, or have I solved the problem?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Socket::SSL buffering issue?
by starbolin (Hermit) on Apr 25, 2005 at 07:25 UTC | |
by althepal (Acolyte) on Apr 25, 2005 at 08:38 UTC | |
by starbolin (Hermit) on Apr 25, 2005 at 16:09 UTC | |
|
Re: IO::Socket::SSL buffering issue?
by salva (Canon) on Apr 25, 2005 at 11:03 UTC | |
|
Re: IO::Socket::SSL buffering issue?
by 5p1d3r (Hermit) on Apr 25, 2005 at 15:05 UTC |