nagisa-kunhah commented on code in PR #26: URL: https://github.com/apache/dubbo-go-extensions/pull/26#discussion_r3890021057
########## filter/adaptivesvc/limiter/hill_climbing.go: ########## @@ -0,0 +1,332 @@ +/* + * 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" + "sync" + "time" +) + +import ( + "go.uber.org/atomic" +) + +var ( + _ Limiter = (*HillClimbing)(nil) + _ Updater = (*HillClimbingUpdater)(nil) +) + +type HillClimbingOption int64 + +const ( + HillClimbingOptionShrinkPlus HillClimbingOption = -2 + HillClimbingOptionShrink HillClimbingOption = -1 + HillClimbingOptionDoNothing HillClimbingOption = 0 + HillClimbingOptionExtend HillClimbingOption = 1 + HillClimbingOptionExtendPlus HillClimbingOption = 2 +) + +var ( + initialLimitation uint64 = 50 + maxLimitation uint64 = 500 + + radicalPeriod = 1000 * time.Millisecond + stablePeriod = 32000 * time.Millisecond +) + +// HillClimbing is a limiter using HillClimbing algorithm +type HillClimbing struct { + seq *atomic.Uint64 + round *atomic.Uint64 + + inflight *atomic.Uint64 + limitation *atomic.Uint64 + + mutex *sync.Mutex + // nextUpdateTime = lastUpdatedTime + updateInterval + updateInterval *atomic.Duration + lastUpdatedTime *atomic.Time + + // metrics of the current round + transactionNum *atomic.Uint64 + rttAvg *atomic.Float64 + + // best metrics in the history + bestMaxCapacity *atomic.Float64 + bestRTTAvg *atomic.Float64 + bestLimitation *atomic.Uint64 + bestTPS *atomic.Uint64 +} + +func NewHillClimbing() Limiter { + l := &HillClimbing{ + seq: new(atomic.Uint64), + round: new(atomic.Uint64), + inflight: new(atomic.Uint64), + limitation: atomic.NewUint64(initialLimitation), + mutex: new(sync.Mutex), + updateInterval: atomic.NewDuration(radicalPeriod), + lastUpdatedTime: atomic.NewTime(time.Now()), + transactionNum: new(atomic.Uint64), + rttAvg: new(atomic.Float64), + bestMaxCapacity: new(atomic.Float64), + bestRTTAvg: atomic.NewFloat64(math.MaxFloat64), + bestLimitation: new(atomic.Uint64), + bestTPS: new(atomic.Uint64), + } + + return l +} + +func (l *HillClimbing) Inflight() uint64 { + return l.inflight.Load() +} + +func (l *HillClimbing) Remaining() uint64 { + limitation := l.limitation.Load() + inflight := l.Inflight() + if limitation < inflight { + return 0 + } + return limitation - inflight +} + +func (l *HillClimbing) Acquire() (Updater, error) { + if l.Remaining() == 0 { + return nil, ErrReachLimitation + } + return NewHillClimbingUpdater(l), nil +} + +type HillClimbingUpdater struct { + startTime time.Time + limiter *HillClimbing + + // for debug purposes + seq uint64 +} + +func NewHillClimbingUpdater(limiter *HillClimbing) *HillClimbingUpdater { + inflight := limiter.inflight.Add(1) + u := &HillClimbingUpdater{ + startTime: time.Now(), + limiter: limiter, + seq: limiter.seq.Add(1) - 1, + } + VerboseDebugf("[NewHillClimbingUpdater] A new request arrived, seq: %d, inflight: %d, time: %s.", Review Comment: ok,改用Verbose了。 -- 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]
