Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanghb1 committed Dec 2, 2024
1 parent ceeb7e2 commit 2905de9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
8 changes: 5 additions & 3 deletions pkg/protect/protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package protect

import (
"fmt"
"github.com/apache/servicecomb-service-center/pkg/util"
"net/http"
"time"

Expand All @@ -22,7 +21,7 @@ var (
enableInstanceNullProtect bool
restartProtectInterval time.Duration
RestartProtectHttpCode int
validProtectCode = []int{http.StatusNotModified, http.StatusUnprocessableEntity, http.StatusInternalServerError}
validProtectCode = map[int]struct{}{http.StatusNotModified: {}, http.StatusUnprocessableEntity: {}, http.StatusInternalServerError: {}}
)

const (
Expand All @@ -33,14 +32,17 @@ const (

func Init() {
enableInstanceNullProtect = config.GetBool("instance_null_protect.enable", false)
if !enableInstanceNullProtect {
return
}
restartProtectInterval = time.Duration(config.GetInt("instance_null_protect.restart_protect_interval", 120)) * time.Second
if restartProtectInterval > maxInterval || restartProtectInterval < minInterval {
log.Warn(fmt.Sprintf("invalid instance_null_protect.restart_protect_interval: %d,"+
" must between %d-%ds inclusively", restartProtectInterval, minInterval, maxInterval))
restartProtectInterval = defaultRestartProtectInterval
}
RestartProtectHttpCode = config.GetInt("instance_null_protect.http_status", http.StatusNotModified)
if !util.Contains(validProtectCode, RestartProtectHttpCode) {
if _, ok := validProtectCode[RestartProtectHttpCode]; !ok {
log.Warn(fmt.Sprintf("invalid instance_null_protect.http_status: %d, must be %v", RestartProtectHttpCode, validProtectCode))
RestartProtectHttpCode = http.StatusNotModified
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,3 @@ func GeneratePassword() (string, error) {
}
return pass, nil
}

// Contains reports whether v is present in s.
func Contains[S ~[]E, E comparable](s S, v E) bool {
return Index(s, v) >= 0
}

// Index returns the index of the first occurrence of v in s,
// or -1 if not present.
func Index[S ~[]E, E comparable](s S, v E) int {
for i := range s {
if v == s[i] {
return i
}
}
return -1
}
7 changes: 0 additions & 7 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package util

import (
"net/http"
"os"
"testing"

Expand Down Expand Up @@ -185,9 +184,3 @@ func TestGeneratePassword(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 8, len(password), password)
}

func TestContains(t *testing.T) {
slc := []int{http.StatusNotModified, http.StatusUnprocessableEntity, http.StatusInternalServerError}
assert.True(t, Contains(slc, 304))
assert.False(t, Contains(slc, 100))
}

0 comments on commit 2905de9

Please sign in to comment.