multipool (empty) → 0.1.0.0
raw patch · 7 files changed
+200/−0 lines, 7 filesdep +basedep +bytestringdep +hashablesetup-changed
Dependencies added: base, bytestring, hashable, hedis, mmorph, monad-logger, mtl, multipool, persistent, persistent-postgresql, postgresql-common-persistent, resource-pool, text, unordered-containers
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- multipool.cabal +74/−0
- src/Data/MultiPool.hs +88/−0
- test/Spec.hs +2/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for multipool++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# multipool
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ multipool.cabal view
@@ -0,0 +1,74 @@+-- This file has been generated from package.yaml by hpack version 0.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 94867a7572b048dffde7b9f36b385a6ca96a42467c6d513f22a25f60ed82c231++name: multipool+version: 0.1.0.0+synopsis: Generalized system for reading and writing to distributed systems that have primary/replica topologies.+description: Please see the README on GitHub at <https://github.com/githubuser/multipool#readme>+homepage: https://github.com/iand675/multipool#readme+bug-reports: https://github.com/iand675/multipool/issues+author: Ian Duncan+maintainer: ian@iankduncan.com+copyright: 2018 Ian Duncan+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10+extra-source-files:+ ChangeLog.md+ README.md++source-repository head+ type: git+ location: https://github.com/iand675/multipool++library+ exposed-modules:+ Data.MultiPool+ other-modules:+ Paths_multipool+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , bytestring+ , hashable+ , hedis+ , mmorph+ , monad-logger+ , mtl+ , persistent+ , persistent-postgresql+ , postgresql-common-persistent+ , resource-pool+ , text+ , unordered-containers+ default-language: Haskell2010++test-suite multipool-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Paths_multipool+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , bytestring+ , hashable+ , hedis+ , mmorph+ , monad-logger+ , mtl+ , multipool+ , persistent+ , persistent-postgresql+ , postgresql-common-persistent+ , resource-pool+ , text+ , unordered-containers+ default-language: Haskell2010
+ src/Data/MultiPool.hs view
@@ -0,0 +1,88 @@+{-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE RecordWildCards #-}++module Data.MultiPool where++import Control.Exception+import Control.Monad.Reader+import Control.Monad.Logger+import Data.Hashable+import Data.HashMap.Strict (HashMap)+import qualified Data.HashMap.Strict as HM+import Data.Pool+import Data.Text (Text)+import Data.Text.Encoding+import Data.Typeable+import Data.IORef+-- Todo: separate out implementations+import Data.ByteString (ByteString)+import Database.Persist.Postgresql+import Database.Persist.Postgresql.Common++newtype InstanceName backend = InstanceName+ { rawInstanceName :: Hashed Text+ } deriving (Show, Eq, Ord)++mkInstanceName :: Text -> InstanceName backend+mkInstanceName = InstanceName . hashed++instance Hashable (InstanceName backend) where+ hashWithSalt s r = hashWithSalt s (rawInstanceName r)+ hash = hash . rawInstanceName++data InstanceDoesNotExist backend = InstanceDoesNotExist+ { instanceDoesNotExist :: InstanceName backend+ } deriving (Show, Eq, Typeable)++instance (Show (InstanceDoesNotExist backend), Typeable backend) => Exception (InstanceDoesNotExist backend)++class Monad m => MultiPoolBackend m backend where+ type Masters backend :: *+ type Masters backend = Pool (MasterConnection backend)+ type Replicas backend :: *+ type Replicas backend = HashMap (ReplicaIdentifier backend) (Pool (ReplicaConnection backend))++ type MasterConnection backend :: *+ type ReplicaConnection backend :: *++ type MasterIdentifier backend :: *+ type MasterIdentifier backend = ()+ type ReplicaIdentifier backend :: *+ type ReplicaIdentifier backend = InstanceName backend++ runWriteAny :: MultiPool backend -> ReaderT (MasterConnection backend) m a -> m a+ runWrite :: MultiPool backend -> MasterIdentifier backend -> ReaderT (MasterConnection backend) m a -> m a++ runReadMaster :: MultiPool backend -> MasterIdentifier backend -> ReaderT (ReplicaConnection backend) m a -> m a+ runReadAnyMaster :: MultiPool backend -> ReaderT (ReplicaConnection backend) m a -> m a+ runReadAny :: MultiPool backend -> ReaderT (ReplicaConnection backend) m a -> m a+ runRead :: MultiPool backend -> ReplicaIdentifier backend -> ReaderT (ReplicaConnection backend) m a -> m a+++-- Invariant: MultiPool should not be modified after creation?+data MultiPool backend = MultiPool+ { multiPoolMaster :: !(Masters backend)+ , multiPoolReplica :: !(Replicas backend)+ , multiPoolAnyMasterSelector :: MultiPool backend -> IO (MasterIdentifier backend)+ , multiPoolAnyReplicaSelector :: MultiPool backend -> IO (Maybe (ReplicaIdentifier backend))+ }++forReplicas :: (MultiPoolBackend m backend, Replicas backend ~ HashMap k v) => MultiPool backend -> (k -> v -> m a) -> m [a]+forReplicas pool f = forM (HM.toList (multiPoolReplica pool)) $ \(k, v) -> f k v++roundRobin :: MonadIO m => [choice] -> m (a -> IO (Maybe choice))+roundRobin [] = return $ const $ return Nothing+roundRobin choices = do+ let infiniteChoice = cycle choices+ picker <- liftIO $ newIORef infiniteChoice+ return $ const $ atomicModifyIORef' picker $ \l -> case l of+ (x:xs) -> (xs, Just x)+ [] -> error "roundRobin: should have matched empty list in first clause"
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"