Open brunoborges opened 6 years ago
For C# functions we have support forcancellation-tokens. We need to add similar support for java functions.
Hi @brunoborges , although there is a strong temptation to write functions depending on variables across invocations, it is against the best-practice of writing serverless code. One suggestion is to create the database connection and close it in every single invocation. And a better solution is to rely on the Azure Functions built-in annotation to communicate with your database.
From the Programming Model of Azure Functions Java developer guide
Your Azure function should be a stateless class method that processes input and produces output. Although you are allowed to write instance methods, your function must not depend on any instance fields of the class. All function methods must have a public access modifier.
What would work fine is to have either annotations (for before / after) or convention that certain methods will be called. I prefer the annotation approach, as it is more explicit, but I understand it adds a performance overhead to every call.
@jonathanGiles, the ask is to be notified when the instance is created and destroyed. Not before/after a single invocation.
@JunyiYi, look at the previous comment about Cancellation Token. That functionality is available for C#, but not for Java. That'd be a good place to start. Consistency.
On your comment about database connections, they are extremely expensive. And I am talking about functions that push code (e.g. http input, Kafka input, even cosmosdb as input) to the DB.
@brunoborges - cancellation tokens are going to be per-call, not really per-instance. So they'd get called at the timeout mark (or a bit before).
@Clockwork-Muse this goes against the recommendation of opening connections to resources (expensive objects to create) during function start, and reusing those through subsequent calls. By not having a way to be notified when the Java function instance shuts down, the recommendation is impossible to apply, and if applied, there is the risk of having stale connections that might be consuming important resources.
Possibly, yes, so an equivalent to Dispose
might be handy.
What might work better is letting functions take a database connection/pool object (DbConnection
in C#, probably DataSource
in Java), especially if the database connection string was an environment variable. That would be a different request, though.
@Clockwork-Muse adding support for a DB connection pool would be awesome, but that is a different ER indeed that I already shared with Jeff and Eduardo over email.
But there are many other cases where an equivalent to Dispose would be needed. There are some Java libraries that are a bit heavier and have notion of create/start/stop/destroy. One good example non-DB related is the Twitter4J library, that comes with reusable objects and Twitter API connection.
Hi @kamperiadis do you have any updates on this issue? A customer wants to do clean up actions in their functions code when it terminated
Developers should be allowed to be aware of when a Function is created, and when it is destroyed, to clean up resources (e.g. close DB connections; stop background processes; etc).
For Java functions, and I assume .NET, one can use the class constructor to be aware of when the Function is created, and perform a 'setup'. But the same does not happen for when the function is to be killed/removed/purged.