summaryrefslogtreecommitdiff
path: root/Types/TransferrerPool.hs
blob: c8c560e682d2d6491c6ef0303c140d14988cc242 (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
58
59
{- A pool of "git-annex transfer" processes available for use
 -
 - Copyright 2013-2020 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Types.TransferrerPool where

import Common

import Control.Concurrent.STM hiding (check)

type TransferrerPool = TVar [TransferrerPoolItem]

type CheckTransferrer = IO Bool
type MkCheckTransferrer = IO (IO Bool)

{- Each item in the pool may have a transferrer running, and has an
 - IO action that can be used to check if it's still ok to use the
 - transferrer. -}
data TransferrerPoolItem = TransferrerPoolItem (Maybe Transferrer) CheckTransferrer

data Transferrer = Transferrer
	{ transferrerRead :: Handle
	, transferrerWrite :: Handle
	, transferrerHandle :: ProcessHandle
	, transferrerShutdown :: IO ()
	-- ^ Closes the FDs and waits for the process to exit. 
	-- Should be used when the transferrer is in between transfers,
	-- as otherwise it may not shutdown promptly.
	}

newTransferrerPool :: IO TransferrerPool
newTransferrerPool = newTVarIO []

popTransferrerPool :: TransferrerPool -> STM (Maybe TransferrerPoolItem, Int)
popTransferrerPool p = do
	l <- readTVar p
	case l of
		[] -> return (Nothing, 0)
		(i:is) -> do
			writeTVar p is
			return $ (Just i, length is)

pushTransferrerPool :: TransferrerPool -> TransferrerPoolItem -> STM ()
pushTransferrerPool p i = do
	l <- readTVar p
	let l' = i:l
	writeTVar p l'

{- Note that making a CheckTransferrer may allocate resources,
 - such as a NotificationHandle, so it's important that the returned
 - TransferrerPoolItem is pushed into the pool, and not left to be
 - garbage collected. -}
mkTransferrerPoolItem :: MkCheckTransferrer -> Transferrer -> IO TransferrerPoolItem
mkTransferrerPoolItem mkcheck t = do
	check <- mkcheck
	return $ TransferrerPoolItem (Just t) check