Bug 58091 - mod_socache_memcache does not reuse memcache connections
Summary: mod_socache_memcache does not reuse memcache connections
Status: RESOLVED FIXED
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: mod_socache_(dbm|dc|memcache|shmcb) (show other bugs)
Version: 2.5-HEAD
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords: FixedInTrunk
Depends on:
Blocks:
 
Reported: 2015-07-02 08:59 UTC by Valentin Vidic
Modified: 2015-10-25 05:48 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Valentin Vidic 2015-07-02 08:59:07 UTC
While trying to use memcache for SSL sessions

  SSLSessionCache memcache:127.0.0.1

I noticed the connection to the memcache server is not reused but dropped and recreated on every request. This can be checked using tcpdump.

The cause of this behavior seems to be incorrect TTL setting in mod_socache_memcache.c:

  #ifndef MC_DEFAULT_SERVER_TTL
  #define MC_DEFAULT_SERVER_TTL 600
  #endif

This is than used when creating memcache server:

        rv = apr_memcache_server_create(p,
                                        host_str, port,
                                        MC_DEFAULT_SERVER_MIN,
                                        MC_DEFAULT_SERVER_SMAX,
                                        thread_limit,
                                        MC_DEFAULT_SERVER_TTL,
                                        &st);

My guess is the author wanted a TTL of 600 seconds (10 minutes) which is reasonable, but the problem is the units for this parameter are in microseconds so the connection does not have a chance of reuse in next 600 microseconds.

Changing this constant to 600 * 1000 * 1000 restores the intended behavior of caching the connection for 10 minutes.
Comment 1 Christophe JAILLET 2015-07-02 19:58:29 UTC
Hi,

looks good to me.
See http://marc.info/?l=subversion-dev&m=139866123837158 for the same kind of issue in svn.

Thanks
Comment 2 Yann Ylavic 2015-07-03 00:14:10 UTC
The parameter would preferably be configurable, so that a close from the memcache side (which may also have a timeout) does not produce an error.

The TTL is an idle timeout, active only when the connection is unused, hence reset on each use.
There is also no mechanism to test the connection before reusing it in mod_socache_memcache, unlike in mod_proxy.
The timeouts with the memcache have to be synced (a little lower here).

The current code inhibited that potential error.
Comment 3 Yann Ylavic 2015-07-03 00:17:28 UTC
I would also advocate a low (15/20 seconds) default (or at worse hard) timeout.
Comment 4 Christophe JAILLET 2015-08-15 22:05:37 UTC
Change of default value + new option to configure the TTL (MemcacheConnTTL) proposed in r1696105


(In reply to Yann Ylavic from comment #2)
> The timeouts with the memcache have to be synced (a little lower here).

What do you mean? Can you elaborate?
Comment 5 Christophe JAILLET 2015-08-16 13:00:44 UTC
Proposed for backport in r1696139
Comment 6 Christophe JAILLET 2015-10-25 05:48:43 UTC
This is in 2.4.17