Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
haskell-throttle
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gargantext
haskell-throttle
Commits
be38aa94
Verified
Commit
be38aa94
authored
Jul 29, 2024
by
Przemyslaw Kaminski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
README added
parent
02f5ed9e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
2 deletions
+46
-2
.gitignore
.gitignore
+3
-1
README.md
README.md
+42
-0
Throttle.hs
src/Control/Concurrent/Throttle.hs
+1
-1
No files found.
.gitignore
View file @
be38aa94
dist-newstyle/
\ No newline at end of file
.idea/
dist-newstyle/
docs/
\ No newline at end of file
README.md
0 → 100644
View file @
be38aa94
# haskell-throttle
A
[
throttle
](
https://en.wikipedia.org/wiki/Rate_limiting
)
implementation using
[
`Control.Concurrent.STM.TChan`
](
https://hackage.haskell.org/package/stm-2.5.3.1/docs/Control-Concurrent-STM-TChan.html
)
.
## Rationale
Current Haskell throttle implementations
(e.g.
[
`Data.Conduit.Throttle`
](
https://hackage.haskell.org/package/conduit-throttle-0.3.1.0/docs/Data-Conduit-Throttle.html
)
or
[
`io-throttle
](
https://hackage.haskell.org/package/io-throttle
)
)
work by just __slowing down__ incoming messages. However, sometimes we
don't care about delivering __all__ messages, but just want to limit
their rate. Hence this module is born.
The main function is in
[
`Control.Concurrent.Throttle`
](
./src/Control/Concurrent/Throttle.hs
)
:
```
Haskell
throttle :: (Ord id, Eq id, Show id) => Int -> TChan.TChan (id, a) -> (a -> IO ()) -> IO ()
```
It takes as arguments:
-
resolution (in milliseconds)
-
a
`TChan`
where
`(id, a)`
tuples are sent
-
`action`
which, for given
`a`
calls some
`IO`
action
In the above we have 2 types:
-
`a`
represents the incoming message which will be called with the supplied
`action`
-
`id`
represents some identifier associated with the message. By
using this
`id`
we can perform grouping of messages and throttle
them accordingly.
The
`throttle`
function itself should be spawned in a separate
thread. It then awaits for incoming messages on the given
`TChan`
.
See
[
`test` directory
](
./test
)
for a sample usage.
## Design
Currently, the
`throttle`
function spawns a
`mapCleaner`
thread which,
periodically, checks the internal
`TVar`
value for values to clean up
and values to hold before the given delay happens.
src/Control/Concurrent/Throttle.hs
View file @
be38aa94
...
...
@@ -76,7 +76,7 @@ throttle delay tchan action = do
-- * an item not in 'canRun' was added
atomically
$
TVar
.
modifyTVar
smap
$
Map
.
filterWithKey
(
\
k
(
_
,
t
)
->
(
isNothing
$
Map
.
lookup
k
canRun
)
||
(
now
-
t
>
0
&&
now
-
t
<
delay
))
Map
.
filterWithKey
(
\
k
(
_
,
t
)
->
isNothing
(
Map
.
lookup
k
canRun
)
||
(
now
-
t
>
0
&&
now
-
t
<
delay
))
threadDelay
(
delay
`
div
`
2
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment