Skip to content

Commit

Permalink
check to avoid index error
Browse files Browse the repository at this point in the history
  • Loading branch information
theoremoon committed Nov 21, 2019
1 parent cb23538 commit 5eaacee
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cmd/bgproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type target struct {
DeployedAt time.Time
}

func (t *target) Check() (bool, string) {
func (t *target) Check() error {
httpc := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand All @@ -60,17 +60,20 @@ func (t *target) Check() (bool, string) {
var err error
if t.Url.Scheme == "unix" {
path := strings.SplitN(t.Url.Path, ":", 2)
if len(path) == 0 {
return fmt.Errorf("Invalid url format: %s", t.Url.String())
}
r, err = httpc.Get("http://unix/" + path[1])
} else {
r, err = httpc.Get(t.Url.String())
}
if err != nil {
return false, err.Error()
return err
}
if r.StatusCode != t.ExpectedStatus {
return false, fmt.Sprintf("Returned status code: %d", r.StatusCode)
return fmt.Errorf("Returned status code: %d", r.StatusCode)
}
return true, ""
return nil
}

/// Stop stops the target
Expand Down Expand Up @@ -140,10 +143,10 @@ rollback:

case <-ticker.C:
// check the health
if ok, reason := s.Green.Check(); ok {
if err := s.Green.Check(); err != nil {
unhealthyCount = 0
} else {
logger.Println("Unhealthy: " + reason)
logger.Println("Unhealthy: " + err.Error())
unhealthyCount++
if unhealthyCount >= s.Green.UnhealthyLimit {
break rollback
Expand Down

0 comments on commit 5eaacee

Please sign in to comment.