From 4898f4842cb7642eef967e47bdac13083ea3d73c Mon Sep 17 00:00:00 2001 From: wj00037 <1292876134@qq.com> Date: Wed, 16 Oct 2024 18:22:26 +0800 Subject: [PATCH] add timeout --- go.mod | 6 ++---- go.sum | 4 ---- main.go | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index dd76018..a157859 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/metalogical/BigFiles go 1.21 -toolchain go1.23.1 +toolchain go1.21.4 require ( github.com/go-chi/chi v4.1.2+incompatible @@ -10,10 +10,7 @@ require ( sigs.k8s.io/yaml v1.4.0 ) -require github.com/aws/aws-lambda-go v1.43.0 // indirect - require ( - github.com/akrylysov/algnhsa v1.1.0 github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/goccy/go-json v0.10.3 // indirect @@ -27,4 +24,5 @@ require ( golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 82c9f83..ce38668 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -github.com/akrylysov/algnhsa v1.1.0 h1:G0SoP16tMRyiism7VNc3JFA0wq/cVgEkp/ExMVnc6PQ= -github.com/akrylysov/algnhsa v1.1.0/go.mod h1:+bOweRs/WBu5awl+ifCoSYAuKVPAmoTk8XOMrZ1xwiw= -github.com/aws/aws-lambda-go v1.43.0 h1:Tdu7SnMB5bD+CbdnSq1Dg4sM68vEuGIDcQFZ+IjUfx0= -github.com/aws/aws-lambda-go v1.43.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/main.go b/main.go index 24328bc..ded11cd 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,8 @@ import ( "log" "net/http" "os" - "strings" + "time" - "github.com/akrylysov/algnhsa" "github.com/metalogical/BigFiles/auth" "github.com/metalogical/BigFiles/config" "github.com/metalogical/BigFiles/server" @@ -112,16 +111,19 @@ func main() { IsAuthorized: auth.GiteeAuth(), SecretAccessKey: cfg.AwsSecretAccessKey, }) + srv := &http.Server{ + Addr: "0.0.0.0:5000", + Handler: s, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 30 * time.Second, + } if err != nil { log.Fatalln(err) } - if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") { - algnhsa.ListenAndServe(s, nil) - } else { - log.Println("serving on http://0.0.0.0:5000 ...") - if err := http.ListenAndServe("0.0.0.0:5000", s); err != nil { - log.Fatalln(err) - } + log.Println("serving on http://0.0.0.0:5000 ...") + if err := srv.ListenAndServe(); err != nil { + log.Fatalln(err) } }