nagisa-kunhah commented on code in PR #26:
URL: 
https://github.com/apache/dubbo-go-extensions/pull/26#discussion_r4111113711


##########
filter/adaptivesvc/limiter/hill_climbing.go:
##########
@@ -0,0 +1,316 @@
+/*
+ * 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 (
+       "github.com/dubbogo/gost/log/logger"
+
+       "go.uber.org/atomic"
+)
+
+var (
+       _ Limiter = (*HillClimbing)(nil)
+       _ Updater = (*HillClimbingUpdater)(nil)
+)
+
+// 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
+}
+

Review Comment:
   Thanks for catching this. It's a legacy issue inherited from the dubbo-go 
implementation. We benchmarked lock vs CAS reservation before deciding 
(b.RunParallel, -cpu=1,2,4,8,16,32, -benchtime=500ms, -count=3, linux/amd64, 
ns/op):
   - CAS 50.8/43.7/52.7/59.1/77.0/82.1 
   - mutex 72.7/68.9/99.6/161.0/185.0/192.7
   
   CAS wins on the realistic path at every core count, so Acquire now does an 
atomic CAS reservation.



-- 
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]

Reply via email to