~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/util/effbot/org/http_manager.py

  • Committer: Martin Packman
  • Date: 2012-01-05 11:18:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6431.
  • Revision ID: martin.packman@canonical.com-20120105111827-oojb8q88c847lm5d
Remove unused effbot package from util

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: http_manager.py 270 2004-10-09 10:38:54Z fredrik $
2
 
# effnews http
3
 
#
4
 
# manage a set of http clients
5
 
#
6
 
# Copyright (c) 2001-2004 by Fredrik Lundh.  All rights reserved.
7
 
#
8
 
 
9
 
from __future__ import absolute_import
10
 
 
11
 
import asyncore, time
12
 
import http_client
13
 
 
14
 
class http_manager:
15
 
 
16
 
    max_connections = 8
17
 
    max_size = 1000000
18
 
    max_time = 60
19
 
 
20
 
    def __init__(self):
21
 
        self.queue = []
22
 
 
23
 
    def request(self, uri, consumer, extra_headers=None):
24
 
        self.queue.append((uri, consumer, extra_headers))
25
 
 
26
 
    def priority_request(self, uri, consumer, extra_headers=None):
27
 
        self.queue.insert(0, (uri, consumer, extra_headers))
28
 
 
29
 
    def purge(self):
30
 
        for channel in asyncore.socket_map.values():
31
 
            channel.close()
32
 
        del self.queue[:]
33
 
 
34
 
    def prioritize(self, priority_uri):
35
 
        i = 0
36
 
        for uri, consumer, extra_headers in self.queue:
37
 
            if uri == priority_uri:
38
 
                del self.queue[i]
39
 
                self.priority_request(uri, consumer, extra_headers)
40
 
                return
41
 
            i = i + 1
42
 
 
43
 
    def poll(self, timeout=0.1):
44
 
        # sanity checks
45
 
        now = time.time()
46
 
        for channel in asyncore.socket_map.values():
47
 
            if channel.bytes_in > self.max_size:
48
 
                channel.close() # too much data
49
 
                try:
50
 
                    channel.consumer.http(
51
 
                        0, channel, ("HTTPManager", "too much data", None)
52
 
                        )
53
 
                except:
54
 
                    pass
55
 
            if channel.timestamp and now - channel.timestamp > self.max_time:
56
 
                channel.close() # too slow
57
 
                try:
58
 
                    channel.consumer.http(
59
 
                        0, channel, ("HTTPManager", "timeout", None)
60
 
                        )
61
 
                except:
62
 
                    pass
63
 
        # activate up to max_connections channels
64
 
        while self.queue and len(asyncore.socket_map) < self.max_connections:
65
 
            http_client.do_request(*self.queue.pop(0))
66
 
        # keep the network running
67
 
        asyncore.poll(timeout)
68
 
        # return non-zero if we should keep on polling
69
 
        return len(self.queue) or len(asyncore.socket_map)