Function Development Overview
Function Syntax
FunctionGraph supports Go 1.x. The following shows the function syntax.
func Handler (payload []byte, ctx context.RuntimeContext)
- Handler: name of the handler function.
- payload: event parameter defined for the function. The parameter is in JSON format.
- ctx: runtime information provided for executing the function. For details, see SDK APIs.
The format of the Go function handler is the same as the name of the executable file in the code package. Ensure that the name of the dynamic library file is consistent with the plug-in name of the handler. For example, if the name of the dynamic library file is testplugin.so, set the handler name to testplugin.Handler. You can configure or modify the handler on the function details page on the FunctionGraph console.
SDK APIs
The Go SDK provides event, context, and logging APIs. Download the Go SDK (Go SDK.sha256).
- Event APIs
Event structure definitions are added to the Go SDK. Currently, DIS, DDS, SMN, Timer, and APIG triggers are supported. The definitions make coding much simpler when triggers are required.
- APIG trigger field description
- APIGTriggerEvent fields
Table 1 APIGTriggerEvent fields Field
Description
IsBase64Encoded
Whether the body of an event is encoded using Base64.
HttpMethod
HTTP request method.
Path
HTTP request path.
Body
HTTP request body.
PathParameters
All path parameters.
RequestContext
API Gateway configurations (APIGRequestContext object).
Headers
HTTP request header.
QueryStringParameters
Query parameters.
UserData
User data set in the APIG custom authorizer.
- APIGTriggerResponse fields
Table 3 APIGTriggerResponse fields Field
Description
Body
Message body.
Headers
HTTP response header to be returned.
StatusCode
HTTP status code. Type: int.
IsBase64Encoded
Whether the body has been encoded using Base64. Type: bool.
APIGTriggerEvent provides the GetRawBody() method to obtain the body decoded using Base64. APIGTriggerResponse provides the SetBase64EncodedBody() method to set the body encoded using Base64.
- APIGTriggerEvent fields
- DIS trigger field description
Table 4 DISTriggerEvent fields Field
Description
ShardID
Partition ID.
Message
DIS message body (DISMessage structure).
Tag
Function version.
StreamName
Stream name.
Table 5 DISMessage fields Field
Description
NextPartitionCursor
Next partition cursor.
Records
Message records (DISRecord structure).
MillisBehindLatest
Reserved parameter.
- Kafka trigger field description
Table 7 KAFKATriggerEvent fields Field
Description
InstanceId
Instance ID.
Records
Message records (Table 8).
TriggerType
Trigger type (Kafka).
Region
Region.
EventTime
Time when an event occurred (seconds).
EventVersion
Event version.
- SMN trigger field description
Table 9 SMNTriggerEvent fields Field
Description
Record
Message records (SMNRecord structure).
Table 10 SMNRecord fields Field
Description
EventVersion
Event version. (Currently, the version is 1.0.)
EventSubscriptionUrn
Subscription URN.
EventSource
Event source.
Smn
Message body (SMNBody structure).
- Timer trigger field description
Table 12 TimerTriggerEvent fields Field
Description
Version
Version. (Currently, the version is v1.0.)
Time
Current time.
TriggerType
Trigger type (Timer).
TriggerName
Trigger name.
UserEvent
Additional information about the trigger.
- When using an APIG trigger, set the first parameter of the handler function (for example, handler) to handler(APIGTriggerEvent event, Context context). For details about the constraints, see Base64 Decoding and Response Structure.
- The preceding TriggerEvent methods have corresponding set methods, which are recommended for local debugging. DIS and LTS triggers have getRawData() methods, but do not have setRawData() methods.
- APIG trigger field description
- Context APIs
The context APIs are used to obtain the context, such as agency AK/SK, current request ID, allocated memory space, and number of CPUs, required for executing a function.
Table 13 describes the context APIs provided by FunctionGraph.
Table 13 Context methods Method
Description
getRequestID( )
Obtains a request ID.
getRemainingTimeInMilligetRunningTimeInSecondsSeconds ( )
Obtains the remaining running time of a function.
getAccessKey( )
Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.
FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.
getSecretKey( )
Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.
FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.
getSecurityAccessKey( )
Obtains the SecurityAccessKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.
getSecuritySecretKey( )
Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.
getSecurityToken( )
Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.
getUserData(string key)
Uses keys to obtain the values passed by environment variables.
getFunctionName( )
Obtains the name of a function.
getRunningTimeInSeconds ( )
Obtains the timeout of a function.
getVersion( )
Obtains the version of a function.
getMemorySize( )
Obtains the allocated memory.
getCPUNumber( )
Obtains CPU usage of a function.
getPackage( )
Obtains a function group.
getToken( )
Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.
getLogger( )
Obtains the logger method provided by the context. By default, information such as the time and request ID is output.
getAlias()
Obtains function alias.
- Table 14 describes the logging API provided in the Go SDK.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot