-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from geocode-city/basic-rate-limiting
Basic rate limiting
- Loading branch information
Showing
13 changed files
with
452 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table account.api_key | ||
add column if not exists monthly_quota bigint default 100000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ dependencies: | |
- resource-pool ^>= 0.2.3.2 | ||
- containers | ||
- lens | ||
- hedis | ||
|
||
ghc-options: | ||
- -Wall | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
module Effects | ||
( module Effects.Log | ||
, module Effects.Database | ||
, module Effects.Time | ||
, module Effects.Cache | ||
) | ||
where | ||
|
||
import Effects.Log | ||
|
||
import Effects.Database | ||
-- | ||
import Effects.Cache | ||
-- | ||
import Effects.Time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Effects.Cache where | ||
|
||
import Control.Algebra | ||
import Control.Carrier.Error.Either (ErrorC, Throw, runError) | ||
import Control.Carrier.Reader | ||
import Control.Carrier.Throw.Either (throwError) | ||
import qualified Database.Redis as R | ||
import Import | ||
|
||
--- | ||
|
||
data Cache (m :: Type -> Type) k where | ||
HllAdd :: ByteString -> [ByteString] -> Cache m Integer | ||
HllCount :: [ByteString] -> Cache m Integer | ||
|
||
hllAdd :: (Has Cache sig m) => ByteString -> [ByteString] -> m Integer | ||
hllAdd key values = send $ HllAdd key values | ||
|
||
hllCount :: (Has Cache sig m) => [ByteString] -> m Integer | ||
hllCount = send . HllCount | ||
|
||
newtype CacheIOC m a = CacheIOC {runCacheIO :: ReaderC R.Connection m a} | ||
deriving (Applicative, Functor, Monad, MonadIO) | ||
|
||
newtype CacheError = CacheError R.Reply | ||
deriving (Eq, Show) | ||
|
||
runCacheWithConnection :: R.Connection -> CacheIOC m hs -> m hs | ||
runCacheWithConnection conn = runReader conn . runCacheIO | ||
|
||
instance | ||
(Has (Throw CacheError) sig m, MonadIO m, Algebra sig m) => | ||
Algebra (Cache :+: sig) (CacheIOC m) | ||
where | ||
alg hdl sig ctx = CacheIOC $ case sig of | ||
L (HllAdd key values) -> do | ||
conn <- ask | ||
added <- liftIO $ | ||
R.runRedis conn $ do | ||
R.pfadd key values | ||
(<$ ctx) <$> either (throwError . CacheError) pure added | ||
L (HllCount keys) -> do | ||
conn <- ask | ||
count <- liftIO $ | ||
R.runRedis conn $ do | ||
R.pfcount keys | ||
(<$ ctx) <$> either (throwError . CacheError) pure count | ||
R other -> alg (runCacheIO . hdl) (R other) ctx | ||
|
||
runCacheEither :: ErrorC CacheError m a -> m (Either CacheError a) | ||
runCacheEither = runError @CacheError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Effects.Time where | ||
|
||
import Control.Algebra | ||
import Data.Time (UTCTime, getCurrentTime) | ||
import Import | ||
|
||
data Time (m :: Type -> Type) k where | ||
Now :: Time m UTCTime | ||
|
||
now :: (Has Time sig m) => m UTCTime | ||
now = send Now | ||
|
||
newtype TimeIOC m a = TimeIOC {runTimeIO :: m a} | ||
deriving (Applicative, Functor, Monad, MonadIO) | ||
|
||
instance | ||
(MonadIO m, Algebra sig m) => | ||
Algebra (Time :+: sig) (TimeIOC m) | ||
where | ||
alg hdl sig ctx = case sig of | ||
L Now -> (<$ ctx) <$> liftIO getCurrentTime | ||
R other -> TimeIOC $ alg (runTimeIO . hdl) other ctx |
Oops, something went wrong.