aws-lambda-extensions
aws-lambda-extensions copied to clipboard
Go Extension - Cannot make an concurrent http request in external extension
Setup:
- Lambda that makes an http request to external extension.
- External Extension running an http.Server (localhost:8111)
- Incoming requests in the Extension are queued into a channel and handled async while returning status-code 201 to lambda request
- During async handling of the requests a http call is made.
Expected Behaviour: The HTTP call works during async handling
Actual Behaviour: Only sync calls (for example during the http.HandleFunc or just in the main-func) work. Async http call times out after shutdown event
I tried multiple ways to implement this. For example making "processEvents" async and "StartChannel" async. Or a simple go-routine call in the handlefunc. Nothing seemed to work. And it seems very strange to me.
func main(){
fmt.Println("So far its working")
//this works perfectly fine
_, err := http.Get("https://jsonplaceholder.typicode.com/posts/1")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
/**** async http.listenAndServe with handlefunc etc ****/
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-sigs
fmt.Println(s)
fmt.Println("closing now")
eventhubHandler.CloseChannelAndCauseShutdown()
}()
_, err = extensionClient.Register(ctx, extensionName)
if err != nil {
panic(err)
}
go handler.ReadChannel()
processEvents(ctx)
}
//handler.go
func ReadChannel() {
fmt.Println("starting channel")
dataChannel := channelConfig.dataChannel
for _:= range dataChannel {
// does not work and only times out after shutdown event
_, err := http.Get("https://jsonplaceholder.typicode.com/posts/1")
}
}
go.mod partial
github.com/aws/aws-lambda-go v1.27.1
github.com/aws/aws-sdk-go v1.42.25
github.com/aws/aws-xray-sdk-go v1.6.0
template.yaml partial
LAMBDA:
Type: AWS::Serverless::Function
Properties:
CodeUri: bin/lambda
Handler: bootstrap
Runtime: provided.al2
MemorySize: 256
Timeout: 5
Tracing: Active
Layers:
- !Ref EXTENSION