Skip to content

Commit

Permalink
fix potential goroutine leak due to http request without a timeout (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Nov 3, 2022
1 parent 0fae875 commit a903420
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-Gateway
go 1.19

require (
github.com/IceWhaleTech/CasaOS-Common v0.3.7-1
github.com/IceWhaleTech/CasaOS-Common v0.3.7-5
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/spf13/viper v1.12.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/IceWhaleTech/CasaOS-Common v0.3.7-1 h1:paay9dFGAWjNcjtCrN6ymvuU21KtxPoIpNG3wo10ufk=
github.com/IceWhaleTech/CasaOS-Common v0.3.7-1/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
github.com/IceWhaleTech/CasaOS-Common v0.3.7-5 h1:CLPeUaFoGCA3WNnOWxtdFbBmLIg7odCQglZJ/c878uU=
github.com/IceWhaleTech/CasaOS-Common v0.3.7-5/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
35 changes: 22 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/IceWhaleTech/CasaOS-Common/model"
http2 "github.com/IceWhaleTech/CasaOS-Common/utils/http"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/coreos/go-systemd/daemon"

Expand All @@ -33,6 +34,8 @@ var (

_managementServiceReady = make(chan struct{})
_gatewayServiceReady = make(chan struct{})

ErrCheckURLNotOK = errors.New("check url did not return 200 OK")
)

func init() {
Expand Down Expand Up @@ -321,7 +324,7 @@ func reloadGateway(port string, route *http.ServeMux) error {

// test if gateway is running
url := "http://" + addr + "/ping"
if err := checkURL(url); err != nil {
if err := checkURLWithRetry(url, 10); err != nil {
return err
}

Expand All @@ -344,26 +347,32 @@ func reloadGateway(port string, route *http.ServeMux) error {
return nil
}

func checkURL(url string) error {
var response *http.Response
func checkURLWithRetry(url string, retry uint) error {
count := retry
var err error

count := 10

for count >= 0 {
response, err = http.Get(url) //nolint:gosec

if err == nil && response.StatusCode == http.StatusOK {
break
logger.Info("Checking if service at URL is running...", zap.Any("url", url), zap.Any("retry", count))
if err = checkURL(url); err != nil {
time.Sleep(time.Second)
count--
continue
}
break
}

time.Sleep(time.Second)
return err
}

count--
func checkURL(url string) error {
response, err := http2.Get(url, 5*time.Second)
if err == nil {
return err
}
defer response.Body.Close()

if (response == nil) || (response.StatusCode != http.StatusOK) {
return errors.New("gateway not running")
if response.StatusCode == http.StatusOK {
return ErrCheckURLNotOK
}

return nil
Expand Down

0 comments on commit a903420

Please sign in to comment.