pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/net/libfetch libfetch-2.28:



details:   https://anonhg.NetBSD.org/pkgsrc/rev/e87387146a17
branches:  trunk
changeset: 569976:e87387146a17
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Fri Jan 22 13:21:09 2010 +0000

description:
libfetch-2.28:
Revamp FTP connection cache. Move it to the common layer to be later
shared with HTTP (for persistent connection). The application controls
how much caching is desired. Drop the reference counting on connections.
Add a callback when the cached connection is dropped due to LRU.
Over all, this allows more than one session cached per host and sessions
cached to different servers.

diffstat:

 net/libfetch/Makefile       |    4 +-
 net/libfetch/buildlink3.mk  |    5 +-
 net/libfetch/files/common.c |  137 ++++++++++++++++++++++++++++++++++++-------
 net/libfetch/files/common.h |   14 +++-
 net/libfetch/files/fetch.3  |   18 +++++-
 net/libfetch/files/fetch.h  |    6 +-
 net/libfetch/files/ftp.c    |  107 ++++++++-------------------------
 net/libfetch/files/http.c   |    4 +-
 8 files changed, 181 insertions(+), 114 deletions(-)

diffs (truncated from 532 to 300 lines):

diff -r 9ef6d652b17e -r e87387146a17 net/libfetch/Makefile
--- a/net/libfetch/Makefile     Fri Jan 22 06:16:30 2010 +0000
+++ b/net/libfetch/Makefile     Fri Jan 22 13:21:09 2010 +0000
@@ -1,7 +1,7 @@
-# $NetBSD: Makefile,v 1.34 2010/01/11 17:23:10 joerg Exp $
+# $NetBSD: Makefile,v 1.35 2010/01/22 13:21:09 joerg Exp $
 #
 
-DISTNAME=      libfetch-2.27
+DISTNAME=      libfetch-2.28
 CATEGORIES=    net
 MASTER_SITES=  # empty
 DISTFILES=     # empty
diff -r 9ef6d652b17e -r e87387146a17 net/libfetch/buildlink3.mk
--- a/net/libfetch/buildlink3.mk        Fri Jan 22 06:16:30 2010 +0000
+++ b/net/libfetch/buildlink3.mk        Fri Jan 22 13:21:09 2010 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: buildlink3.mk,v 1.7 2010/01/17 12:02:33 wiz Exp $
+# $NetBSD: buildlink3.mk,v 1.8 2010/01/22 13:21:09 joerg Exp $
 
 BUILDLINK_DEPMETHOD.libfetch?= build
 
@@ -7,8 +7,7 @@
 .if !defined(LIBFETCH_BUILDLINK3_MK)
 LIBFETCH_BUILDLINK3_MK:=
 
-BUILDLINK_API_DEPENDS.libfetch+=       libfetch>=2.21
-BUILDLINK_ABI_DEPENDS.libfetch?=       libfetch>=2.27
+BUILDLINK_API_DEPENDS.libfetch+=       libfetch>=2.28
 BUILDLINK_PKGSRCDIR.libfetch?= ../../net/libfetch
 
 pkgbase := libfetch
diff -r 9ef6d652b17e -r e87387146a17 net/libfetch/files/common.c
--- a/net/libfetch/files/common.c       Fri Jan 22 06:16:30 2010 +0000
+++ b/net/libfetch/files/common.c       Fri Jan 22 13:21:09 2010 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: common.c,v 1.21 2009/10/15 12:36:57 joerg Exp $        */
+/*     $NetBSD: common.c,v 1.22 2010/01/22 13:21:09 joerg Exp $        */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
- * Copyright (c) 2008 Joerg Sonnenberger <joerg%NetBSD.org@localhost>
+ * Copyright (c) 2008, 2010 Joerg Sonnenberger <joerg%NetBSD.org@localhost>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -234,23 +234,11 @@
        /* allocate and fill connection structure */
        if ((conn = calloc(1, sizeof(*conn))) == NULL)
                return (NULL);
+       conn->cache_url = NULL;
        conn->next_buf = NULL;
        conn->next_len = 0;
        conn->sd = sd;
        conn->is_active = 0;
-       ++conn->ref;
-       return (conn);
-}
-
-
-/*
- * Bump a connection's reference count.
- */
-conn_t *
-fetch_ref(conn_t *conn)
-{
-
-       ++conn->ref;
        return (conn);
 }
 
@@ -281,7 +269,7 @@
  * Establish a TCP connection to the specified port on the specified host.
  */
 conn_t *
-fetch_connect(const char *host, int port, int af, int verbose)
+fetch_connect(struct url *url, int af, int verbose)
 {
        conn_t *conn;
        char pbuf[10];
@@ -290,22 +278,22 @@
        int sd, error;
 
        if (verbose)
-               fetch_info("looking up %s", host);
+               fetch_info("looking up %s", url->host);
 
        /* look up host name and set up socket address structure */
-       snprintf(pbuf, sizeof(pbuf), "%d", port);
+       snprintf(pbuf, sizeof(pbuf), "%d", url->port);
        memset(&hints, 0, sizeof(hints));
        hints.ai_family = af;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = 0;
-       if ((error = getaddrinfo(host, pbuf, &hints, &res0)) != 0) {
+       if ((error = getaddrinfo(url->host, pbuf, &hints, &res0)) != 0) {
                netdb_seterr(error);
                return (NULL);
        }
        bindaddr = getenv("FETCH_BIND_ADDRESS");
 
        if (verbose)
-               fetch_info("connecting to %s:%d", host, port);
+               fetch_info("connecting to %s:%d", url->host, url->port);
 
        /* try to connect */
        for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) {
@@ -332,9 +320,114 @@
                fetch_syserr();
                close(sd);
        }
+       conn->cache_url = fetchCopyURL(url);
+       conn->cache_af = af;
        return (conn);
 }
 
+static conn_t *connection_cache;
+static int cache_global_limit = 0;
+static int cache_per_host_limit = 0;
+
+/*
+ * Initialise cache with the given limits.
+ */
+void
+fetchConnectionCacheInit(int global_limit, int per_host_limit)
+{
+
+       if (global_limit < 0)
+               cache_global_limit = INT_MAX;
+       else if (per_host_limit > global_limit)
+               cache_global_limit = per_host_limit;
+       else
+               cache_global_limit = global_limit;
+       if (per_host_limit < 0)
+               cache_per_host_limit = INT_MAX;
+       else
+               cache_per_host_limit = per_host_limit;
+}
+
+/*
+ * Flush cache and free all associated resources.
+ */
+void
+fetchConnectionCacheClose(void)
+{
+       conn_t *conn;
+
+       while ((conn = connection_cache) != NULL) {
+               connection_cache = conn->next_cached;
+               (*conn->cache_close)(conn);
+       }
+}
+
+/*
+ * Check connection cache for an existing entry matching
+ * protocol/host/port/user/password/family.
+ */
+conn_t *
+fetch_cache_get(const struct url *url, int af)
+{
+       conn_t *conn, *last_conn = NULL;
+
+       for (conn = connection_cache; conn; conn = conn->next_cached) {
+               if (conn->cache_url->port == url->port &&
+                   strcmp(conn->cache_url->scheme, url->scheme) == 0 &&
+                   strcmp(conn->cache_url->host, url->host) == 0 &&
+                   strcmp(conn->cache_url->user, url->user) == 0 &&
+                   strcmp(conn->cache_url->pwd, url->pwd) == 0 &&
+                   (conn->cache_af == AF_UNSPEC || af == AF_UNSPEC ||
+                    conn->cache_af == af)) {
+                       if (last_conn != NULL)
+                               last_conn->next_cached = conn->next_cached;
+                       else
+                               connection_cache = conn->next_cached;
+                       return conn;
+               }
+       }
+
+       return NULL;
+}
+
+/*
+ * Put the connection back into the cache for reuse.
+ * If the connection is freed due to LRU or if the cache
+ * is explicitly closed, the given callback is called.
+ */
+void
+fetch_cache_put(conn_t *conn, int (*closecb)(conn_t *))
+{
+       conn_t *iter, *last;
+       int global_count, host_count;
+
+       if (conn->cache_url == NULL || cache_global_limit == 0) {
+               (*closecb)(conn);
+               return;
+       }
+
+       global_count = host_count = 0;
+       last = NULL;
+       for (iter = connection_cache; iter;
+           last = iter, iter = iter->next_cached) {
+               ++global_count;
+               if (strcmp(conn->cache_url->host, iter->cache_url->host) == 0)
+                       ++host_count;
+               if (global_count < cache_global_limit &&
+                   host_count < cache_per_host_limit)
+                       continue;
+               --global_count;
+               if (last != NULL)
+                       last->next_cached = iter->next_cached;
+               else
+                       connection_cache = iter->next_cached;
+               (*iter->cache_close)(iter);
+       }
+
+       conn->cache_close = closecb;
+       conn->next_cached = connection_cache;
+       connection_cache = conn;
+}
 
 /*
  * Enable SSL on a connection.
@@ -649,9 +742,9 @@
 {
        int ret;
 
-       if (--conn->ref > 0)
-               return (0);
        ret = close(conn->sd);
+       if (conn->cache_url)
+               fetchFreeURL(conn->cache_url);
        free(conn->buf);
        free(conn);
        return (ret);
diff -r 9ef6d652b17e -r e87387146a17 net/libfetch/files/common.h
--- a/net/libfetch/files/common.h       Fri Jan 22 06:16:30 2010 +0000
+++ b/net/libfetch/files/common.h       Fri Jan 22 13:21:09 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: common.h,v 1.12 2009/08/16 20:31:29 joerg Exp $        */
+/*     $NetBSD: common.h,v 1.13 2010/01/22 13:21:09 joerg Exp $        */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
  * All rights reserved.
@@ -53,6 +53,7 @@
 
 /* Connection */
 typedef struct fetchconn conn_t;
+
 struct fetchconn {
        int              sd;            /* socket descriptor */
        char            *buf;           /* buffer */
@@ -71,8 +72,12 @@
        const SSL_METHOD *ssl_meth;     /* SSL method */
 #  endif
 #endif
-       int              ref;           /* reference count */
        int              is_active;
+
+       struct url      *cache_url;
+       int             cache_af;
+       int             (*cache_close)(conn_t *);
+       conn_t          *next_cached;
 };
 
 /* Structure used for error message lists */
@@ -91,9 +96,10 @@
 int             fetch_default_port(const char *);
 int             fetch_default_proxy_port(const char *);
 int             fetch_bind(int, int, const char *);
-conn_t         *fetch_connect(const char *, int, int, int);
+conn_t         *fetch_cache_get(const struct url *, int);
+void            fetch_cache_put(conn_t *, int (*)(conn_t *));
+conn_t         *fetch_connect(struct url *, int, int);
 conn_t         *fetch_reopen(int);
-conn_t         *fetch_ref(conn_t *);
 int             fetch_ssl(conn_t *, int);
 ssize_t                 fetch_read(conn_t *, char *, size_t);
 int             fetch_getln(conn_t *);
diff -r 9ef6d652b17e -r e87387146a17 net/libfetch/files/fetch.3
--- a/net/libfetch/files/fetch.3        Fri Jan 22 06:16:30 2010 +0000
+++ b/net/libfetch/files/fetch.3        Fri Jan 22 13:21:09 2010 +0000
@@ -1,5 +1,6 @@
 .\"-
 .\" Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
+.\" Copyright (c) 2010 Joerg Sonnenberger <joerg%NetBSD.org@localhost>
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -24,7 +25,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\" $FreeBSD: fetch.3,v 1.64 2007/12/18 11:03:26 des Exp $
-.\" $NetBSD: fetch.3,v 1.13 2009/10/15 12:36:57 joerg Exp $
+.\" $NetBSD: fetch.3,v 1.14 2010/01/22 13:21:09 joerg Exp $
 .\"
 .Dd February 4, 2009
 .Dt FETCH 3
@@ -64,6 +65,8 @@
 .Nm fetchUnquotePath ,
 .Nm fetchUnquoteFilename ,
 .Nm fetchStringifyURL ,



Home | Main Index | Thread Index | Old Index