justxuewei commented on code in PR #2114: URL: https://github.com/apache/dubbo-go/pull/2114#discussion_r1027079434
########## filter/adaptivesvc/limiter/auto_concurrency_limiter.go: ########## @@ -0,0 +1,276 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package limiter + +import ( + "math" + "math/rand" + "sync" + "time" +) + +import ( + "github.com/dubbogo/gost/log/logger" + + "go.uber.org/atomic" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/filter/adaptivesvc/limiter/cpu" +) + +var ( + _ Limiter = (*AutoConcurrency)(nil) + _ Updater = (*AutoConcurrencyUpdater)(nil) + cpuLoad *atomic.Uint64 = atomic.NewUint64(0) // range from 0 to 1000 +) + +// These parameters may need to be different between services +const ( + MaxExploreRatio = 0.3 + MinExploreRatio = 0.06 + SampleWindowSizeMs = 1000 + MinSampleCount = 40 + MaxSampleCount = 500 + CpuDecay = 0.95 +) + +type AutoConcurrency struct { + sync.RWMutex + + exploreRatio float64 + emaFactor float64 + noLoadLatency float64 //duration + maxQPS float64 + halfSampleIntervalMS int64 + maxConcurrency uint64 + + // metrics of the current round + startSampleTimeUs int64 + lastSamplingTimeUs *atomic.Int64 + resetLatencyUs int64 // time to reset noLoadLatency + remeasureStartUs int64 //time to reset req data (sampleCount, totalSampleUs, totalReqCount) + sampleCount int64 + totalSampleUs int64 + totalReqCount *atomic.Int64 + + inflight *atomic.Uint64 +} + +func init() { + go cpuproc() +} + +// cpu = cpuᵗ⁻¹ * decay + cpuᵗ * (1 - decay) +func cpuproc() { + ticker := time.NewTicker(time.Millisecond * 500) // same to cpu sample rate + defer func() { + ticker.Stop() + if err := recover(); err != nil { + go cpuproc() + } + }() + + for range ticker.C { + usage := cpu.CpuUsage() + prevCPU := cpuLoad.Load() + curCPU := uint64(float64(prevCPU)*CpuDecay + float64(usage)*(1.0-CpuDecay)) + logger.Debugf("current cpu usage: %d", curCPU) + cpuLoad.Store(curCPU) + } +} + +func CpuUsage() uint64 { Review Comment: CpuUsage -> CPUUsage -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
