summaryrefslogtreecommitdiff
path: root/src/meetingtools/ac/__init__.py
blob: 2bbd25f5b3965adca7e380c63f5f31e941a72327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from meetingtools.ac.api import ACPClient
import time
from meetingtools.apps.cluster.models import acc_for_user
from django.core.cache import cache
from Queue import Queue
import logging
from django.contrib.auth.models import User

_pools = {}

MAXCALLS = 10    
MAXIDLE = 10

class ClientPool(object):
    
    def __init__(self,acc,maxsize=0,increment=2):
        self._q = Queue(maxsize)
        self._acc = acc
        self._increment = increment
    
    def allocate(self):
        now = time.time()
        api = None
        while not api:
            if self._q.empty():
                for i in range(1,self._increment):
                    logging.debug("adding instance %d" % i)
                    api = ACPClient(self._acc.api_url,self._acc.user,self._acc.password,cpool=self)
                    self._q.put_nowait(api)
            
            api = self._q.get()
            if api and (api.age > MAXCALLS or now - api.lastused > MAXIDLE):
                api = None
        return api

# with ac_api_client(acc) as api
#    ...

def ac_api_client(o):
    acc = o
    logging.debug("ac_api_client(%s)" % repr(o))
    if hasattr(o,'user') and isinstance(getattr(o,'user'),User):
        acc = acc_for_user(getattr(o,'user'))
    elif hasattr(o,'acc'):
        acc = getattr(o,'acc')
    
    tag = 'ac_api_client_%d' % acc.id
    pool = _pools.get(tag)
    if pool is None:
        pool = ClientPool(acc,maxsize=30)
        _pools[tag] = pool
        
    return pool.allocate()