Revision 68453d22 snf-common/synnefo/lib/pool/http.py

b/snf-common/synnefo/lib/pool/http.py
37 37
from httplib import (
38 38
        HTTPConnection  as http_class,
39 39
        HTTPSConnection as https_class,
40
        HTTPException,
41 40
        ResponseNotReady
42 41
)
43 42

  
44
from urlparse import urlparse
45 43
from new import instancemethod
46 44

  
45
import logging
46

  
47
log = logging.getLogger(__name__)
47 48

  
48 49
_pools = {}
49 50
pool_size = 8
......
59 60

  
60 61
def put_http_connection(conn):
61 62
    pool = conn._pool
63
    log.debug("HTTP-PUT-BEFORE: putting connection %r back to pool %r", conn, pool)
62 64
    if pool is None:
65
        log.debug("HTTP-PUT: connection %r does not have a pool", conn)
63 66
        return
64 67
    # conn._pool = None
65 68
    pool.pool_put(conn)
......
73 76
    }
74 77

  
75 78
    def __init__(self, scheme, netloc, size=None):
79
        log.debug("INIT-POOL: Initializing pool of size %d, scheme: %s, netloc: %s",
80
                 size, scheme, netloc)
76 81
        ObjectPool.__init__(self, size=size)
77 82

  
78 83
        connection_class = self._scheme_to_class.get(scheme, None)
......
85 90
        self.netloc = netloc
86 91

  
87 92
    def _pool_create(self):
93
        log.debug("CREATE-HTTP-BEFORE from pool %r", self)
88 94
        conn = self.connection_class(self.netloc)
89 95
        conn._use_counter = USAGE_LIMIT
90 96
        conn._pool = self
......
93 99
        return conn
94 100

  
95 101
    def _pool_verify(self, conn):
96
	if conn is None:
102
        log.debug("VERIFY-HTTP")
103
        if conn is None:
97 104
            return False
98
	sock = conn.sock
105
        sock = conn.sock
99 106
        if sock is None:
100 107
            return True
101 108
        if select((conn.sock,), (), (), 0)[0]:
......
103 110
        return True
104 111

  
105 112
    def _pool_cleanup(self, conn):
113
        log.debug("CLEANUP-HTTP")
106 114
        # every connection can be used a finite number of times
107 115
        conn._use_counter -= 1
108 116

  
109 117
        # see httplib source for connection states documentation
110 118
        if conn._use_counter > 0 and conn._HTTPConnection__state == 'Idle':
111 119
            try:
112
                resp = conn.getresponse()
120
                conn.getresponse()
113 121
            except ResponseNotReady:
114
               return False
122
                log.debug("CLEANUP-HTTP: Not closing connection. Will reuse.")
123
                return False
115 124

  
125
        log.debug("CLEANUP-HTTP: Closing connection. Will not reuse.")
116 126
        conn._real_close()
117 127
        return True
118 128

  
119 129

  
120 130
def get_http_connection(netloc=None, scheme='http', pool_size=pool_size):
131
    log.debug("HTTP-GET: Getting HTTP connection")
121 132
    if netloc is None:
122 133
        m = "netloc cannot be None"
123 134
        raise ValueError(m)
124 135
    # does the pool need to be created?
125 136
    if netloc not in _pools:
137
        log.debug("HTTP-GET: Creating pool for netloc %s", netloc)
126 138
        pool = HTTPConnectionPool(scheme, netloc, size=pool_size)
127 139
        _pools[netloc] = pool
128 140

  
129
    return _pools[netloc].pool_get()
130

  
141
    obj = _pools[netloc].pool_get()
142
    log.debug("HTTP-GET: Returning object %r", obj)
143
    return obj

Also available in: Unified diff