Websocket URL timeout reached error in AWS Lambda

WBOY
Release: 2024-02-10 11:30:10
forward
964 people have browsed it

AWS Lambda 中的 Websocket URL 超时达到错误

When using Websocket in AWS Lambda, you sometimes encounter a Websocket URL timeout error. This problem may result in the inability to establish or maintain a connection to Websocket, affecting the normal operation of the application. This article will introduce the causes and solutions to this problem, help developers solve Websocket URL timeout errors, and ensure the stability and reliability of applications. Both beginners and experienced developers will gain valuable information about AWS Lambda and Websockets from this article.

Question content

I am using go language to develop aws lambda. I'm deploying go lang code, using chromedp and docker image, and getting websocket url timeout error. My lambda is set to 3008 mb ram memory, 512mb storage and 15 minutes timeout. Can you identify the problem and how to fix it? Here are the files main.go and dockerfile

File main.go (chromedp part)

func getpage(url string, linenum string, stationnm string) {
    // settings for crawling
    ctx, cancle := chromedp.newcontext(
        context.background(),
        chromedp.withlogf(log.printf),
    )
    defer cancle()

    opts := []chromedp.execallocatoroption{
        chromedp.disablegpu,
        chromedp.nosandbox,
        chromedp.headless,
        chromedp.flag("no-zygote", true),
        chromedp.flag("single-process", true),
        chromedp.flag("homedir", "/tmp"),
        chromedp.flag("data-path", "/tmp/data-path"),
        chromedp.flag("disk-cache-dir", "/tmp/cache-dir"),
        chromedp.flag("remote-debugging-port", "9222"),
        chromedp.flag("remote-debugging-address", "0.0.0.0"),
        chromedp.flag("disable-dev-shm-usage", true),
    }

    allocctx, cancel := chromedp.newexecallocator(ctx, opts...)
    defer cancel()

    ctx, cancel = chromedp.newcontext(allocctx, chromedp.withlogf(log.printf))
    defer cancel()

    var htmlcontent string

    ch := chromedp.waitnewtarget(ctx, func(i *target.info) bool {
        return strings.contains(i.url, "/timetable/web/")
    })

}
Copy after login

File dockerfile

FROM public.ecr.aws/lambda/provided:al2 AS build

ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

# Get rid of the extension warning
RUN mkdir -p /opt/extensions
RUN yum -y install golang
RUN go env -w GOPROXY=direct

# Clone git, copying go.mod, go.sum, main.go
WORKDIR /var/task/
RUN yum install git -y
RUN git clone https://github.com/seedspirit/NaverCrawler-CICD-go.git
RUN cp NaverCrawler-CICD-go/main.go /var/task/
RUN cp NaverCrawler-CICD-go/go.mod /var/task/
RUN cp NaverCrawler-CICD-go/go.sum /var/task/

# cache dependencies
RUN go mod download
RUN go build -o main .

FROM public.ecr.aws/lambda/provided:al2
COPY --from=build /var/task/main /var/task/main

# Install Chrome dependencies
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -o chrome.rpm && \
    yum install -y ./chrome.rpm && \
    yum install -y fontconfig libX11 GConf2 dbus-x11

ENTRYPOINT ["/var/task/main"]
Copy after login

Solution

It is recommended to use chromedp/headless-shell as it is small and more suitable for aws lambda.

I just tested a simple demo using chromedp/headless-shell and it works.

dockerfile

from golang:1.20.4-alpine3.17 as builder

workdir /app

copy go.mod go.sum ./
run go mod download

copy . .

run go build -o main

from chromedp/headless-shell:113.0.5672.93

workdir /app

copy --from=builder /app/main .

entrypoint [ "./main" ]
Copy after login

main.go

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "os"

    "github.com/aws/aws-lambda-go/lambda"
    "github.com/chromedp/chromedp"
)

func Handler(_ context.Context, _ json.RawMessage) error {
    opts := []chromedp.ExecAllocatorOption{
        chromedp.NoSandbox,
        chromedp.Flag("disable-setuid-sandbox", true),
        chromedp.Flag("disable-dev-shm-usage", true),
        chromedp.Flag("single-process", true),
        chromedp.Flag("no-zygote", true),
    }
    ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
    defer cancel()

    ctx, cancel = chromedp.NewContext(ctx, chromedp.WithDebugf(log.Printf))
    defer cancel()

    var content string
    if err := chromedp.Run(ctx, chromedp.Tasks{
        chromedp.Navigate("https://example.com/"),
        chromedp.Text("body > div > p:nth-child(2)", &content),
    }); err != nil {
        log.Fatal(err)
    }
    fmt.Println(content)
    return nil
}

func main() {
    if _, exists := os.LookupEnv("AWS_LAMBDA_RUNTIME_API"); exists {
        lambda.Start(Handler)
    } else {
        err := Handler(context.Background(), nil)
        if err != nil {
            log.Fatal(err)
        }
    }
}
Copy after login

This example is based on https://github.com/andiedie/chromedp-aws-lambda-example. Note that the chromedp.execallocatoroptions listed are copied directly from this repository. It works, but I'm not sure if this is the best list of options. Maybe you have to adjust them to your needs.

The above is the detailed content of Websocket URL timeout reached error in AWS Lambda. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!