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 98a0a0e commit ceeb7e2
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 78 deletions.
71 changes: 71 additions & 0 deletions pkg/protect/protect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package protect

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

"github.com/apache/servicecomb-service-center/server/config"

"github.com/apache/servicecomb-service-center/pkg/log"
)

/**
for restart service center, set a restartProtectInterval time window to return RestartProtectHttpCode on discovery apis,
indicating that sdk not need to clear cache
*/

var (
isWithinProtection bool
startupTimestamp int64
enableInstanceNullProtect bool
restartProtectInterval time.Duration
RestartProtectHttpCode int
validProtectCode = []int{http.StatusNotModified, http.StatusUnprocessableEntity, http.StatusInternalServerError}
)

const (
maxInterval = 120 * time.Second
minInterval = 0 * time.Second
defaultRestartProtectInterval = 120 * time.Second
)

func Init() {
enableInstanceNullProtect = config.GetBool("instance_null_protect.enable", false)
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) {
log.Warn(fmt.Sprintf("invalid instance_null_protect.http_status: %d, must be %v", RestartProtectHttpCode, validProtectCode))
RestartProtectHttpCode = http.StatusNotModified
}

log.Info(fmt.Sprintf("instance_null_protect.enable: %t", enableInstanceNullProtect))
log.Info(fmt.Sprintf("instance_null_protect.restart_protect_interval: %d", restartProtectInterval))
log.Info(fmt.Sprintf("instance_null_protect.http_status: %d", RestartProtectHttpCode))
startupTimestamp = time.Now().UnixNano()
isWithinProtection = true
}

func IsWithinRestartProtection() bool {
if !enableInstanceNullProtect {
return false
}

if !isWithinProtection {
return false
}

if time.Now().Add(-restartProtectInterval).UnixNano() > startupTimestamp {
log.Info("restart protection stop")
isWithinProtection = false
return false
}
log.Info("within restart protection")
return true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rest
package protect

import (
"testing"
Expand Down
71 changes: 0 additions & 71 deletions pkg/rest/first_launch.go

This file was deleted.

16 changes: 16 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,19 @@ 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: 7 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package util

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

Expand Down Expand Up @@ -184,3 +185,9 @@ 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))
}
9 changes: 5 additions & 4 deletions server/resource/disco/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package disco

import (
"fmt"
"github.com/apache/servicecomb-service-center/pkg/protect"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -168,8 +169,8 @@ func (s *InstanceResource) FindInstances(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusNotModified)
return
}
if len(resp.Instances) == 0 && rest.IsWithinRestartProtection() {
w.WriteHeader(rest.RestartProtectHttpCode)
if len(resp.Instances) == 0 && protect.IsWithinRestartProtection() {
w.WriteHeader(protect.RestartProtectHttpCode)
return
}
rest.WriteResponse(w, r, nil, resp)
Expand Down Expand Up @@ -271,8 +272,8 @@ func (s *InstanceResource) ListInstance(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusNotModified)
return
}
if len(resp.Instances) == 0 && rest.IsWithinRestartProtection() {
w.WriteHeader(rest.RestartProtectHttpCode)
if len(resp.Instances) == 0 && protect.IsWithinRestartProtection() {
w.WriteHeader(protect.RestartProtectHttpCode)
return
}
rest.WriteResponse(w, r, nil, resp)
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package server
import (
"context"
"crypto/tls"
"github.com/apache/servicecomb-service-center/pkg/protect"
"os"

"github.com/gofiber/fiber/v2"

"github.com/apache/servicecomb-service-center/pkg/rest"
"github.com/apache/servicecomb-service-center/server/middleware"
"github.com/apache/servicecomb-service-center/server/resource/disco"

Expand Down Expand Up @@ -214,7 +214,7 @@ func (s *ServiceCenterServer) startServices() {

func (s *ServiceCenterServer) startAPIService() {
s.APIServer.SetHostPort(s.Endpoint.Host, s.Endpoint.Port)
rest.Init()
protect.Init()
s.APIServer.Start()
}

Expand Down

0 comments on commit ceeb7e2

Please sign in to comment.