aws-samples / aws-lambda-extensions

A collection of sample extensions to help you get started with AWS Lambda Extensions
MIT No Attribution
445 stars 146 forks source link

Go Extension - Cannot make an concurrent http request in external extension #60

Closed L4B0MB4 closed 1 year ago

L4B0MB4 commented 2 years ago

Setup:

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
julianwood commented 1 year ago

Can you please expand on how you expect this to work. You have a function that makes a call within the handler code to your extension. When its sync, it works. When it's an async call, you get no response? How is the handler code expecting to get a response if its not a blocking call? Are you wanting the extensions to respond back to the function handler code in some way?