pike.git / lib / modules / Protocols.pmod / HTTP.pmod / Session.pike

version» Context lines:

pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:593:   //! (live connections + kept-alive connections <= this number)   //! Defaults to 50 connections.   int maximum_total_connections=50;      //! Maximum times a connection is reused.   //! Defaults to 1000000. <2 means no reuse at all.   int maximum_connection_reuse=1000000;      // internal (but readable for debug purposes)   mapping(string:array(KeptConnection)) connection_cache=([]); + Thread.Mutex connection_cache_mux = Thread.Mutex();   int connections_kept_n=0;   int connections_inuse_n=0;   mapping(string:int) connections_host_n=([]);      protected class KeptConnection   {    string lookup;    Query q;       void create(string _lookup,Query _q)    { -  +  Thread.MutexKey key = connection_cache_mux->lock(2);    lookup=_lookup;    q=_q;       call_out(disconnect,time_to_keep_unused_connections);    connection_cache[lookup]=    (connection_cache[lookup]||({}))+({this});    connections_kept_n++;    }       void disconnect()    { -  +  Thread.MutexKey key = connection_cache_mux->lock(2);    connection_cache[lookup]-=({this});    if (!sizeof(connection_cache[lookup]))    m_delete(connection_cache,lookup);    remove_call_out(disconnect); // if called externally       if (q->con) destruct(q->con);    connections_kept_n--;    if (!--connections_host_n[lookup])    m_delete(connections_host_n,lookup);    destruct(q);    destruct(this);    }       Query use()    { -  +  Thread.MutexKey key = connection_cache_mux->lock(2);    connection_cache[lookup]-=({this});    if (!sizeof(connection_cache[lookup]))    m_delete(connection_cache,lookup);    remove_call_out(disconnect);       connections_kept_n--;    return q; // subsequently, this object is removed (no refs)    }   }   
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:651:   {    return url->scheme+"://"+url->host+":"+url->port;   }      //! Request a @[Query] object suitable to use for the   //! given URL. This may be a reused object from a keep-alive   //! connection.   Query give_me_connection(Standards.URI url)   {    Query q; +  Query old_q;    -  +  Thread.MutexKey key = connection_cache_mux->lock(); +     if (array(KeptConnection) v =    connection_cache[connection_lookup(url)])    { -  q=v[0]->use(); // removes itself -  // clean up -  q->buf=""; -  q->headerbuf=""; -  q->n_used++; +  old_q = v[0]->use(); // removes itself    } -  +  +  q = SessionQuery(); +  q->hostname_cache=hostname_cache; +  if (old_q) { +  // Transfer connection from the old Query object to the new. +  foreach(({ "con", "host", "port", + #if constant(SSL.Cipher) +  "https", "context", "ssl_session", + #endif +  }), string field) { +  q[field] = old_q[field]; +  } +  q->n_used = old_q->n_used+1; +  old_q->con = 0; +  destruct(old_q); +  }    else    {    if (connections_kept_n+connections_inuse_n+1    >= maximum_total_connections &&    sizeof(connection_cache))    {    // close one if we have it kept    array(KeptConnection) all=`+(@values(connection_cache));    KeptConnection one=all[random(sizeof(all))];    one->disconnect(); // removes itself    }    -  q=SessionQuery(); -  q->hostname_cache=hostname_cache; +     connections_host_n[connection_lookup(url)]++; // new    }    connections_inuse_n++;    return q;   }      //   // called when there might be a free connection      // queue of what to call when we get new free connections