amenezes / config-client

config-client package for spring cloud config and cloud foundry
https://config-client.amenezes.net/
Apache License 2.0
24 stars 17 forks source link

There is not config_client decorator to get singleton instance #32

Closed luiscoms closed 2 years ago

luiscoms commented 4 years ago

If I decorate two or more functions, it will call server both times. It would be great a decorator that return singleton instance.

import logging
from config.spring import config_client

@config_client()
def foo(client):
   logging.debug(client.config)

@config_client()  # call again
def bar(client):
   logging.debug(client.config)

Suggestion

import logging
from config.spring import config_client

@config_client(singleton=True)
def foo(client):
   logging.debug(client.config)

@config_client(singleton=True)
def bar(client):
   logging.debug(client.config)

Or

import logging
from config.spring import config_client_singleton

@config_client_singleton
def foo(client):
   logging.debug(client.config)

@config_client_singleton
def bar(client):
   logging.debug(client.config)
amenezes commented 4 years ago

@luiscoms,

This behavior already exists. Try use @singleton decorator available in the core module. For example:

from config.spring import config_client
from config.core import singleton

@singleton
@config_client()
def foo(client):
   return client

@config_client()
def bar(client):
   return client

a = foo()
b = foo()
c = bar()
d = bar()

print(id(a))
print(id(b)) # a == b
print(id(c))
print(id(d)) # c != d

I'll keep this issue open until the documentation is updated.

luiscoms commented 4 years ago

I understood, my suggestion is create a library decorador, not create myself decorator.

amenezes commented 4 years ago

Sorry I didn't understand this point: "create a decorator library, not create myself decorator"?

The decorator @singleton is now available for use and is used in the lib. All you need to do is import the decorator and use it as in a stack.

Could you detail the issue request a little more?

luiscoms commented 4 years ago

Sure, my case is the same as I explained above.

import logging
from config.core import singleton
from config.spring import config_client

@singleton
@config_client()
def foo(client):
    # do stuff with client
    logging.debug(client.config)
    return client

@singleton
@config_client()
def bar(client):
   # do stuff with client
   logging.debug(client.config)
   return client

id(foo()) == id(bar())  # False
amenezes commented 2 years ago

@luiscoms,

Sorry, as this issue is approximately for 1 year and a half without any change, I decided to close it with wontfix status however if there is any change in the general interest we can reevaluate.