Skip to content

Commit 86e7c07

Browse files
authored
feat: Add async functionality to providers (#413)
Signed-off-by: leohoare <[email protected]>
1 parent 154d834 commit 86e7c07

File tree

7 files changed

+1006
-138
lines changed

7 files changed

+1006
-138
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,25 @@ async def some_endpoint():
316316
return create_response()
317317
```
318318

319+
### Asynchronous Feature Retrieval
320+
321+
The OpenFeature API supports asynchronous calls, enabling non-blocking feature evaluations for improved performance, especially useful in concurrent or latency-sensitive scenarios. If a provider *hasn't* implemented asynchronous calls, the client can still be used asynchronously, but calls will be blocking (synchronous).
322+
323+
```python
324+
import asyncio
325+
from openfeature import api
326+
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider
327+
328+
my_flags = { "v2_enabled": InMemoryFlag("on", {"on": True, "off": False}) }
329+
api.set_provider(InMemoryProvider(my_flags))
330+
client = api.get_client()
331+
flag_value = await client.get_boolean_value_async("v2_enabled", False) # API calls are suffixed by _async
332+
333+
print("Value: " + str(flag_value))
334+
```
335+
336+
See the [develop a provider](#develop-a-provider) for how to support asynchronous functionality in providers.
337+
319338
### Shutdown
320339

321340
The OpenFeature API provides a shutdown function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down.
@@ -390,6 +409,56 @@ class MyProvider(AbstractProvider):
390409
...
391410
```
392411

412+
Providers can also be extended to support async functionality.
413+
To support add asynchronous calls to a provider:
414+
* Implement the `AbstractProvider` as shown above.
415+
* Define asynchronous calls for each data type.
416+
417+
```python
418+
class MyProvider(AbstractProvider):
419+
...
420+
async def resolve_boolean_details_async(
421+
self,
422+
flag_key: str,
423+
default_value: bool,
424+
evaluation_context: Optional[EvaluationContext] = None,
425+
) -> FlagResolutionDetails[bool]:
426+
...
427+
428+
async def resolve_string_details_async(
429+
self,
430+
flag_key: str,
431+
default_value: str,
432+
evaluation_context: Optional[EvaluationContext] = None,
433+
) -> FlagResolutionDetails[str]:
434+
...
435+
436+
async def resolve_integer_details_async(
437+
self,
438+
flag_key: str,
439+
default_value: int,
440+
evaluation_context: Optional[EvaluationContext] = None,
441+
) -> FlagResolutionDetails[int]:
442+
...
443+
444+
async def resolve_float_details_async(
445+
self,
446+
flag_key: str,
447+
default_value: float,
448+
evaluation_context: Optional[EvaluationContext] = None,
449+
) -> FlagResolutionDetails[float]:
450+
...
451+
452+
async def resolve_object_details_async(
453+
self,
454+
flag_key: str,
455+
default_value: Union[dict, list],
456+
evaluation_context: Optional[EvaluationContext] = None,
457+
) -> FlagResolutionDetails[Union[dict, list]]:
458+
...
459+
460+
```
461+
393462
> Built a new provider? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=provider&projects=&template=document-provider.yaml&title=%5BProvider%5D%3A+) so we can add it to the docs!
394463
395464
### Develop a hook

0 commit comments

Comments
 (0)