Skip to content

Conversation

@GarthSnyder
Copy link
Collaborator

This is not for direct integration yet, just review.

I groomed the Dispatcher objects I ended up writing for another project and put them into a release-ready state. Take a look and see if you'd be interested in having these somewhere in the PromiseKit universe.

They're simple but probably too peripheral to put in the core; however, they might make a reasonable add-on package. I suspect that these few applications would cover 90% of the things people might be interested in using Dispatcher objects for in the real world.

Contents:

  • RateLimitedDispatcher implements general dispatch rate limits with a token bucket (approximate) strategy.
  • StrictRateLimitedDispatcher is an exact and optimal sliding window rate limiter, but requires O(n) space (n = # of events/interval)
  • ConcurrencyLimitedDispatcher allows only n asynchronous closures to run at once.
  • CoreDataDispatcher lets you dispatch onto threads associated with NSManagedObjectContexts.

CoreDataDispatcher is trivial. ConcurrencyLimitedDispatcher is just a few lines of code as well, although the exact way to implement it might not be obvious to those without some GCD knowledge.

The other two are a bit tricky to implement correctly and efficiently, so that's where I would locate the main value of releasing this stuff.

@@ -0,0 +1,31 @@
#if !os(Linux)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can instead add this to the exclude list in Package.swift for this target.

/// A PromiseKit Dispatcher that allows no more than X simultaneous
/// executions at once.

class ConcurrencyLimitedDispatcher: Dispatcher {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, we can remove the when() that does this manually. I figure. Haven't analyzed them both.

@mxcl
Copy link
Owner

mxcl commented Feb 28, 2019

This is really great, and truly shows the value of your changes, my sincere thanks and respect!

@mxcl mxcl merged commit 70cdaad into mxcl:v7 Feb 28, 2019
mxcl added a commit that referenced this pull request Feb 28, 2019
* Add StrictRateLimitedDispatcher

* Add ConcurrencyLimitedDispatcher

* StrictRateLimitedDispatcher now schedules only on execution

* Refactor rate limited dispatchers onto common base

* Limiter dispatchers complete and tested

* Add CoreDataDispatcher

* Grooming, doc comments, NSLock -> DispatchQueue

* Break Dispatcher implementation tests into separate classes to allow parallelism

* Move time measurement outside of serialization for rate limit test

* Update testing for Linux and slow Travis environments

* Build all v7 branches
mxcl added a commit that referenced this pull request Feb 28, 2019
* Add StrictRateLimitedDispatcher

* Add ConcurrencyLimitedDispatcher

* StrictRateLimitedDispatcher now schedules only on execution

* Refactor rate limited dispatchers onto common base

* Limiter dispatchers complete and tested

* Add CoreDataDispatcher

* Grooming, doc comments, NSLock -> DispatchQueue

* Break Dispatcher implementation tests into separate classes to allow parallelism

* Move time measurement outside of serialization for rate limit test

* Update testing for Linux and slow Travis environments

* Build all v7 branches
mxcl added a commit that referenced this pull request Mar 1, 2019
* Add StrictRateLimitedDispatcher

* Add ConcurrencyLimitedDispatcher

* StrictRateLimitedDispatcher now schedules only on execution

* Refactor rate limited dispatchers onto common base

* Limiter dispatchers complete and tested

* Add CoreDataDispatcher

* Grooming, doc comments, NSLock -> DispatchQueue

* Break Dispatcher implementation tests into separate classes to allow parallelism

* Move time measurement outside of serialization for rate limit test

* Update testing for Linux and slow Travis environments

* Build all v7 branches
mxcl added a commit that referenced this pull request Mar 26, 2019
* Add StrictRateLimitedDispatcher

* Add ConcurrencyLimitedDispatcher

* StrictRateLimitedDispatcher now schedules only on execution

* Refactor rate limited dispatchers onto common base

* Limiter dispatchers complete and tested

* Add CoreDataDispatcher

* Grooming, doc comments, NSLock -> DispatchQueue

* Break Dispatcher implementation tests into separate classes to allow parallelism

* Move time measurement outside of serialization for rate limit test

* Update testing for Linux and slow Travis environments

* Build all v7 branches
mxcl added a commit that referenced this pull request Apr 8, 2019
* Add StrictRateLimitedDispatcher

* Add ConcurrencyLimitedDispatcher

* StrictRateLimitedDispatcher now schedules only on execution

* Refactor rate limited dispatchers onto common base

* Limiter dispatchers complete and tested

* Add CoreDataDispatcher

* Grooming, doc comments, NSLock -> DispatchQueue

* Break Dispatcher implementation tests into separate classes to allow parallelism

* Move time measurement outside of serialization for rate limit test

* Update testing for Linux and slow Travis environments

* Build all v7 branches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants