question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to throttle a Stream by an “elements by second” values? Something like this:

seq { ... }
|> Stream.ofSeq
|> Stream.throttle (perSecond 20)
|> ...

No elements must be lost (i.e. it should do back pressure if incoming stream rate is bigger that needed).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
polytypiccommented, Sep 21, 2016

I added Stream.duringEach for the kind of throttling where you actually do want to roughly aim at a given average time per element. Here is an example:

  let inline (^) x = x

  let randomTimeOutMillis min max = Alt.random <| fun r ->
    let t = int (r % (uint64 max - uint64 min) + uint64 min)
    timeOutMillis t ^->. t

  let test n =
    let start = Stopwatch.StartNew ()
    let source () = Stream.indefinitely ^ randomTimeOutMillis 0 1000

    source ()
    |> Stream.take n
    |> Stream.consumeFun ^ fun t ->
         printfn "%A plain  %3dms" start.Elapsed t

    source ()
    |> Stream.afterEach ^ timeOutMillis 1000
    |> Stream.take n
    |> Stream.consumeFun ^ fun t ->
         printfn "%A after  %3dms" start.Elapsed t

    source ()
    |> Stream.duringEach ^ timeOutMillis 1000
    |> Stream.take n
    |> Stream.consumeFun ^ fun t ->
         printfn "%A during %3dms" start.Elapsed t

Running test 20L gives the following output:

00:00:00.1576488 plain  153ms
00:00:00.2253215 during 221ms
00:00:00.2468896 after  244ms
00:00:01.0783881 during  71ms
00:00:01.0783523 plain  916ms
00:00:01.3960721 plain  313ms
00:00:02.0017792 plain  601ms
00:00:02.0422724 during  38ms
00:00:02.0588948 plain   57ms
00:00:02.1386503 after  884ms
00:00:02.1786046 plain  115ms
00:00:02.9503481 plain  768ms
00:00:03.0014354 plain   51ms
00:00:03.2534785 plain  248ms
00:00:03.7342758 after  590ms
00:00:03.7351870 during 731ms
00:00:04.1003132 plain  845ms
00:00:04.3714115 plain  267ms
00:00:04.5802716 plain  204ms
00:00:04.7805773 after   38ms
00:00:04.7805773 plain  198ms
00:00:04.9655979 during 957ms
00:00:05.5510391 plain  766ms
00:00:05.5575898 during 550ms
00:00:05.8140434 plain  258ms
00:00:05.8196720 plain    5ms
00:00:06.0293120 during  18ms
00:00:06.3057120 plain  485ms
00:00:06.4705700 after  682ms
00:00:06.9917063 plain  682ms
00:00:07.2592276 during 244ms
00:00:07.4995643 plain  504ms
00:00:07.9431675 after  464ms
00:00:08.3516039 plain  852ms
00:00:08.6102287 during 595ms
00:00:09.4811373 during 461ms
00:00:09.6259404 after  675ms
00:00:10.1214586 during  97ms
00:00:11.2936779 during 265ms
00:00:11.4575013 after  823ms
00:00:12.9393301 during 907ms
00:00:12.9787406 after  512ms
00:00:13.8574682 during 811ms
00:00:14.2032069 after  220ms
00:00:14.7311408 during 681ms
00:00:15.5234377 after  311ms
00:00:15.8989877 during 845ms
00:00:16.3184764 during 260ms
00:00:16.8427622 after  311ms
00:00:17.2441344 during 182ms
00:00:18.6631350 during 597ms
00:00:18.7536870 after  903ms
00:00:19.7451558 during 675ms
00:00:20.2443917 after  486ms
00:00:21.2817035 after   28ms
00:00:23.1290225 after  839ms
00:00:24.4493359 after  313ms
00:00:26.4266176 after  969ms
00:00:27.8035456 after  369ms
00:00:29.3323187 after  522ms

As you can see, plain completes in (very) roughly 10 seconds, during in roughly 20 seconds and after in roughly 30 seconds. Elements take a random amount of time between 0 and 1 seconds to generate.

0reactions
vasily-kirichenkocommented, Sep 21, 2016

Thanks, very cool! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throttling Definition & Meaning
The meaning of THROTTLING is the act or an instance of throttling something or someone. How to use throttling in a sentence.
Read more >
Bandwidth throttling
Bandwidth throttling works by limiting (throttling) the speed at which a bandwidth intensive device (a server) receives data or the speed (i.e. bytes...
Read more >
What is API Throttling?
API throttling is the process of limiting the number of API requests a user can make in a certain period. An application programming...
Read more >
Throttle API requests for better throughput
Amazon API Gateway provides four basic types of throttling-related settings: AWS throttling limits are applied across all accounts and clients in a region....
Read more >
THROTTLING | definition in the Cambridge English Dictionary
to press someone's throat very tightly so that they cannot breathe: informal Sometimes he annoys me so much that I could throttle him....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found