diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-12-09 16:02:43 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-12-09 16:02:43 -0400 |
commit | 46f3063131f7711208c1134b31caf999e375306d (patch) | |
tree | 47ab66581b0d2de95865b0f7948db041f6589935 /RemoteDaemon/Common.hs | |
parent | 1eeae43d88c924dfac7c004ff7aeb67e17c52a13 (diff) |
remotedaemon: git change detection over tor hidden service
Diffstat (limited to 'RemoteDaemon/Common.hs')
-rw-r--r-- | RemoteDaemon/Common.hs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/RemoteDaemon/Common.hs b/RemoteDaemon/Common.hs index 982a84b43..711771f97 100644 --- a/RemoteDaemon/Common.hs +++ b/RemoteDaemon/Common.hs @@ -1,6 +1,6 @@ {- git-remote-daemon utilities - - - Copyright 2014 Joey Hess <id@joeyh.name> + - Copyright 2014-2016 Joey Hess <id@joeyh.name> - - Licensed under the GNU GPL version 3 or higher. -} @@ -9,6 +9,8 @@ module RemoteDaemon.Common ( liftAnnex , inLocalRepo , checkNewShas + , ConnectionStatus(..) + , robustConnection ) where import qualified Annex @@ -16,6 +18,7 @@ import Annex.Common import RemoteDaemon.Types import qualified Git import Annex.CatFile +import Utility.ThreadScheduler import Control.Concurrent @@ -40,3 +43,22 @@ checkNewShas transporthandle = check check [] = return True check (r:rs) = maybe (check rs) (const $ return False) =<< liftAnnex transporthandle (catObjectDetails r) + +data ConnectionStatus = ConnectionStopping | ConnectionClosed + +{- Make connection robust, retrying on error, with exponential backoff. -} +robustConnection :: Int -> IO ConnectionStatus -> IO () +robustConnection backoff a = + caught =<< a `catchNonAsync` (const $ return ConnectionClosed) + where + caught ConnectionStopping = return () + caught ConnectionClosed = do + threadDelaySeconds (Seconds backoff) + robustConnection increasedbackoff a + + increasedbackoff + | b2 > maxbackoff = maxbackoff + | otherwise = b2 + where + b2 = backoff * 2 + maxbackoff = 3600 -- one hour |