Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

HTTPTrigger: Recommend way to keep connections hot? #1134

Open thomasfrederikhoeck opened 2 years ago

thomasfrederikhoeck commented 2 years ago

Is your question related to a specific version? If so, please specify:

What binding does your question apply to, if any? (e.g. Blob Trigger, Event Hub Binding, etc)

HTTPTrigger

Question

We are using Azure Functions with Python (with custom Docker Image) using the Premium Plan to build rest api. It is important for these to reply fast. Hence we would like to ensure that connections stay alive/ tokens are refreshed even though no work is arriving and that they are ready before they receive requests.

My question is therefore two-fold:

  1. What is the recommend way to ensure that connections stay alive/ tokens stay fresh? Is that using the health check or using a Timer Trigger? I can see that health checks a some point weren't recommended for premium https://github.com/Azure/Azure-Functions/issues/2273 . On the other hand - are Time triggers guaranteed to happen per process running the function such that they will renew the token at hand? .

  2. When the function receives the first call it needs to authenticate with a Service Principal to get a token which is used for connecting to other Azure Resources. This can take a some seconds. Is it possible to limit traffic so a newly started function doesn't get incoming requests routed to it before it is ready (similar to readiness probe in Kubernetes). Ì can´t determine if post_function_load_app_level from azure.functions.AppExtensionBase can be used as it is not clear if requests are still routed (and just wait for post_function_load_app_level to complete) or they will go to other instances of the function.

thomasfrederikhoeck commented 2 years ago

I had missed warm-up trigger so I guess that solves 2. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-warmup?tabs=in-process&pivots=programming-language-python but my question for 1 still stands :-)

gavin-aguiar commented 2 years ago

Hi @thomasfrederikhoeck, Can you give more context on what connections stay alive/ tokens stay fresh you are referring to here? Also, could you give an example on how the connections are being used?

thomasfrederikhoeck commented 2 years ago

Hi @gavin-aguiar something like a static client like the following

from azure.identity.aio import ClientSecretCredential
import azure.functions as func

client = ClientSecretCredential(tenant_id, client_id, client_secret)

SCOPE = "some-scope"

async def main(req: func.HttpRequest) -> func.HttpResponse:
   token = await client.get_token(SCOPE)
   await some_func(token)
   return func.HttpResponse("Succes", status_code=200)

The first time this client calls it fetches it (slow) and subsequent calls uses a cache until the token expires.

thomasfrederikhoeck commented 2 years ago

So something that warms up by calling .get_token() and then keeps calling it every minute so there is always a cached token without delay.

thomasfrederikhoeck commented 2 years ago

Cross-linking to issue with warmup trigger for full picture: https://github.com/Azure/azure-functions-python-worker/issues/1135