-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package rest | ||
package protect | ||
|
||
import ( | ||
"testing" | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters