From a3733b6fd702585148f621d3816492ebc771e494 Mon Sep 17 00:00:00 2001 From: sanxun0325 Date: Thu, 29 Oct 2020 19:38:26 +0800 Subject: [PATCH] Ban system metric collector for windows OS(#312) --- api/init.go | 8 ++++++-- core/system/slot.go | 5 +++++ util/safe.go | 11 ++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/api/init.go b/api/init.go index 6bfa8138c..f3355c762 100644 --- a/api/init.go +++ b/api/init.go @@ -6,10 +6,10 @@ import ( "github.com/alibaba/sentinel-golang/core/config" "github.com/alibaba/sentinel-golang/core/log/metric" "github.com/alibaba/sentinel-golang/core/system" + "github.com/alibaba/sentinel-golang/logging" "github.com/alibaba/sentinel-golang/util" ) -// InitDefault initializes Sentinel using the configuration from system // environment and the default value. func InitDefault() error { return initSentinel("") @@ -50,8 +50,12 @@ func initCoreComponents() error { if err := metric.InitTask(); err != nil { return err } + if !util.IsWindowsOS() { + system.InitCollector(config.SystemStatCollectIntervalMs()) + } else { + logging.Warn("[Init initCoreComponents] system metric collect is not available for system module in windows") + } - system.InitCollector(config.SystemStatCollectIntervalMs()) if config.UseCacheTime() { util.StartTimeTicker() } diff --git a/core/system/slot.go b/core/system/slot.go index 9c8e3e39e..ff4ab63d2 100644 --- a/core/system/slot.go +++ b/core/system/slot.go @@ -3,6 +3,7 @@ package system import ( "github.com/alibaba/sentinel-golang/core/base" "github.com/alibaba/sentinel-golang/core/stat" + "github.com/alibaba/sentinel-golang/util" ) type AdaptiveSlot struct { @@ -12,6 +13,10 @@ func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult { if ctx == nil || ctx.Resource == nil || ctx.Resource.FlowType() != base.Inbound { return nil } + // system module is not available for windows OS + if util.IsWindowsOS() { + return nil + } rules := GetRules() result := ctx.RuleCheckResult for _, rule := range rules { diff --git a/util/safe.go b/util/safe.go index d3765b788..69ac9f622 100644 --- a/util/safe.go +++ b/util/safe.go @@ -1,6 +1,9 @@ package util -import "unsafe" +import ( + "runtime" + "unsafe" +) // SliceHeader is a safe version of SliceHeader used within this project. type SliceHeader struct { @@ -14,3 +17,9 @@ type StringHeader struct { Data unsafe.Pointer Len int } + +const windows = "windows" + +func IsWindowsOS() bool { + return runtime.GOOS == windows +}