Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to have dynamic naming of the circuit depending on function parameters? #29

Open
sebathi opened this issue Dec 28, 2021 · 1 comment

Comments

@sebathi
Copy link

sebathi commented Dec 28, 2021

It will be really impresive if you can do something like this:

def dynamic_naming(params):
      return "dynamic_" + params.param1

@circuit(name-handler=dynamicNaming)
def external_call(param1, param2):
      requests.get('.......')
@pauloromeira
Copy link
Contributor

pauloromeira commented May 17, 2023

hey @sebathi it looks good indeed. right now it's not possible because per decorator the circuit is created by the time the function is declared.

but you could achieve that with a custom decorator:

from functools import wraps
from circuitbreaker import CircuitBreaker, CircuitBreakerMonitor


def dynamic_circuit(name_handler, **circuit_kwargs):
    def inner(function):
        @wraps(function)
        def wrapper(*args, **kwargs):
            name = name_handler(*args, **kwargs)
            cb = CircuitBreakerMonitor.get(name) or CircuitBreaker(name=name, **circuit_kwargs)
            return cb(function)(*args, **kwargs)
        return wrapper
    return inner


def dynamic_naming(*args, **kwargs):
    return "dynamic_" + args[0]


@dynamic_circuit(name_handler=dynamic_naming)
def external_call(param1, param2):
    requests.get('.......')

I hope my response still holds meaning to you or to others, despite the delay.

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

No branches or pull requests

2 participants