This topic describes how to implement lifecycle hooks for function instances in the Go runtime environment.
Background information
After you configure a lifecycle hook for a function instance, Function Compute invokes the hook when a related lifecycle event for the instance occurs. The following lifecycle hooks can be configured for a function instance: Initializer, PreFreeze, and PreStop. For more information, see Function instance lifecycle.
The billing rules for the lifecycle hooks of function instances are the same as those for common invocation requests. However, the execution logs can be queried only in Function Logs, Instance Logs, or Advanced Logs. The logs for lifecycle hooks are not displayed in Call Request List. For more information, see View the logs of instance lifecycle hooks..
Hook method signature
- An Initializer hook is executed after a function instance is started and before a handler runs. Function Compute ensures that an Initializer hook is successfully invoked only once in the lifecycle of a function instance. For example, if an Initializer hook fails to be invoked, the system retries the Initializer hook until the Initializer hook is successfully invoked, and then runs your handler. Make sure that the Initializer hook is correctly configured when it is repeatedly invoked.
- A PreFreeze hook is executed before a function instance is frozen.
- A PreStop hook is executed before a function instance is destroyed.
function(ctx context.Context)
Implement hook methods
// Register the Initializer hook method.
fc.RegisterInitializerFunction(initialize)
// Register the PreStop hook method.
fc.RegisterPreStopFunction(preStop)
// Register the PreFreeze hook method.
fc.RegisterPreFreezeFunction(preFreeze)
package main
import (
"context"
"log"
"github.com/aliyun/fc-runtime-go-sdk/fc"
"github.com/aliyun/fc-runtime-go-sdk/fccontext"
)
func HandleRequest(ctx context.Context) (string, error) {
return "hello world!", nil
}
func preStop(ctx context.Context) {
log.Print("this is preStop handler")
fctx, _ := fccontext.FromContext(ctx)
fctx.GetLogger().Infof("context: %#v\n", fctx)
}
func preFreeze(ctx context.Context) {
log.Print("this is preFreeze handler")
fctx, _ := fccontext.FromContext(ctx)
fctx.GetLogger().Infof("context: %#v\n", fctx)
}
func initialize(ctx context.Context) {
log.Print("this is initialize handler")
fctx, _ := fccontext.FromContext(ctx)
fctx.GetLogger().Infof("context: %#v\n", fctx)
}
func main() {
fc.RegisterInitializerFunction(initialize)
fc.RegisterPreStopFunction(preStop)
fc.RegisterPreFreezeFunction(preFreeze)
fc.Start(HandleRequest)
}
func initialize(ctx context.Context)
: the Initializer hook method. Thectx context.Context
parameter provides the context information when the function is executed. For more information, see Context.func preStop(ctx context.Context)
: the PreStop hook method. Thectx context.Context
parameter provides the context information when the function is executed. For more information, see Context.func preFreeze(ctx context.Context)
: the PreFreeze hook method. Thectx context.Context
parameter provides the context information when the function is executed. For more information, see Context.- func main(): the entry point for function code in FC. A Go program must contain the
main
function. You can addfc.Start(HandleRequest)
to your code to specify the method to execute the handler, and addfc.RegisterInitializerFunction(initialize)
to register the Initializer hook method. Register the PreStop and PreFreeze hook methods in the similar way.Important- The preceding sample code applies only to event handlers. If you use an HTTP handler,
replace
fc.Start(HandleRequest)
in the code withfc.StartHttp(HandleRequest)
. - The lifecycle hook method must be registered before
fc.Start(HandleRequest)
orfc.StartHttp(HandleRequest)
. Otherwise, the registration fails.
- The preceding sample code applies only to event handlers. If you use an HTTP handler,
replace
Configure lifecycle hooks
Use the Function Compute console
You can configure the Initializer Hook, PreFreeze Hook, and PreStop Hook parameters on the Function Details page in the Function Compute console.FC Example:
For more information, see Function instance lifecycle.
Use Serverless Devs
Use Serverless Devs
s.yaml
file.
- Configure the Initializer hook
Add the initializer and initializationTimeout fields to the function parameter.
- Configure the PreFreeze hook
Add the instanceLifecycleConfig.preFreeze field, including handler and timeout, to the function parameter.
- Configure the PreStop hook
Add the instanceLifecycleConfig.preStop field, including handler and timeout, to the function parameter.
"true"
in the s.yaml
file, the double quotation marks cannot be removed.
Sample code:
edition: 1.0.0
name: hello-world # The name of the project.
access: default # The alias of the key.
vars: # The global variables.
region: cn-shanghai # The ID of the region.
service:
name: fc-example
description: 'fc example by serverless devs'
services:
helloworld: # The name of the service or module.
component: fc
props: # The property value of the component.
region: ${vars.region}
service: ${vars.service}
function:
name: golang-lifecycle-hook-demo
description: 'fc example by serverless devs'
runtime: go1
codeUri: ./target
handler: main
memorySize: 128
timeout: 60
initializationTimeout: 60
initializer: "true"
instanceLifecycleConfig:
preFreeze:
handler: "true"
timeout: 30
preStop:
handler: "true"
timeout: 30
For more information about the YAML syntax of Serverless Devs, see Serverless Devs commands.
View the logs of instance lifecycle hooks.
You can view the logs for lifecycle hook in Function Logs.
Sample programs
Function Compute provides the sample program for invoking the Initializer hook. The sample program provides an example on how to use the Initializer hook in the Go runtime environment to initialize a MySQL connection pool. In the sample program, the MySQL database is configured by using an environment variable of the function. For more information, see the s.yaml file. The Initializer hook obtains the database configuration based on the environment variable, creates a MySQL connection pool, and then tests the connectivity.
For more information, see go-initializer-mysql.