]> gitweb.factorcode.org Git - factor.git/blob - basis/io/pools/pools.factor
factor: trim using lists
[factor.git] / basis / io / pools / pools.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien continuations destructors io.sockets
4 kernel namespaces sequences ;
5 IN: io.pools
6
7 TUPLE: pool connections disposed expired ;
8
9 : check-pool ( pool -- )
10     check-disposed
11     dup expired>> expired? [
12         31337 <alien> >>expired
13         connections>> delete-all
14     ] [ drop ] if ;
15
16 : <pool> ( class -- pool )
17     new V{ } clone
18         >>connections
19         dup check-pool ; inline
20
21 M: pool dispose* connections>> dispose-each ;
22
23 : with-pool ( pool quot -- )
24     [ pool swap with-variable ] curry with-disposal ; inline
25
26 TUPLE: return-connection-state conn pool ;
27
28 : return-connection ( conn pool -- )
29     dup check-pool connections>> push ;
30
31 GENERIC: make-connection ( pool -- conn )
32
33 : new-connection ( pool -- )
34     dup check-pool [ make-connection ] keep return-connection ;
35
36 : acquire-connection ( pool -- conn )
37     dup check-pool
38     [ dup connections>> empty? ] [ dup new-connection ] while
39     connections>> pop ;
40
41 : (with-pooled-connection) ( conn pool quot -- )
42     [ nip call ] [ drop return-connection ] 3bi ; inline
43
44 : with-pooled-connection ( pool quot -- )
45     [ [ acquire-connection ] keep ] dip
46     [ (with-pooled-connection) ] [ ] [ 2drop dispose ] cleanup ; inline
47
48 M: return-connection-state dispose
49     [ conn>> ] [ pool>> ] bi return-connection ;
50
51 : return-connection-later ( db pool -- )
52     \ return-connection-state boa &dispose drop ;
53
54 TUPLE: datagram-pool < pool addrspec ;
55
56 : <datagram-pool> ( addrspec -- pool )
57     datagram-pool <pool> swap >>addrspec ;
58
59 M: datagram-pool make-connection addrspec>> <datagram> ;