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.

Reorder arguments in decorator methods

See original GitHub issue

I have a few decorator methods that have various behaviours including rate limiting, timing, and of course, circuit breaking! I apply these in a chain, as per the decorator pattern.

CircuitBreaker.decorateSupplier has a type signature Supplier<T> decorateSupplier(Supplier<T> supplier, CircuitBreaker circuitBreaker), so chaining multiple decorator methods looks like:

RateLimiter.decorateSupplier(rateLimitConfig,
    CircuitBreaker.decorateSupplier(
        Stopwatch.decorateSupplier(stopwatchConfig,
            mySupplier()), circuitBreaker));

Notice that the CircuitBreaker instance is far from where it is applied in the chain, and looks out of place.

If CircuitBreaker.decorateSupplier had a type signature of Supplier<T> decorateSupplier(CircuitBreaker circuitBreaker, Supplier<T> supplier), then the same code would look like:

RateLimiter.decorateSupplier(rateLimitConfig,
    CircuitBreaker.decorateSupplier(circuitBreaker
        Stopwatch.decorateSupplier(stopwatchConfig,
            mySupplier()));

Now it’s easier to associate the CircuitBreaker instance with the call to CircuitBreaker.decorateSupplier

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
RobWincommented, Jul 2, 2016

Hi, indeed. Looks much better that way.

If you use the library, I would love to get your GitHub Star.

0reactions
RobWincommented, Nov 22, 2016

Let’s move the discussion to #12. Could you copy your comment into this issue? Besides that, @tomfitzhenry is not the maintainer, but it’s me 😃

If a RateLimiter implementation is lightweight in the sense that we don’t need any other 3rd party libraries, we can add it to javaslang-circuitbreaker. A RateLimiter is also some sort of CircuitBreaker. A RateLimiter can forbid further calls, if a rate limit has exceeded.

The retry functionality is also not 100% related to CircuitBreakers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator execution order - python - Stack Overflow
When the interpreter calls the decorated method the decorators are called from top --> bottom. Consider the following code example: print("========== Definition ...
Read more >
Decorators with parameters in Python - GeeksforGeeks
In this article, we will learn about the Decorators with Parameters with help of multiple examples. Python functions are First Class ...
Read more >
The Ultimate Guide to Python Decorators, Part III
In this installment you will learn how to write decorators that take custom arguments, like any normal Python function.
Read more >
Python Decorator Tutorial with Example - DEV Community ‍ ‍
Python decorator are the function that receive a function as an argument and return another function as return value.
Read more >
Primer on Python Decorators
Decorators provide a simple syntax for calling higher-order functions. ... For our purposes, a function returns a value based on the given arguments....
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