justxuewei commented on code in PR #2067:
URL: https://github.com/apache/dubbo-go/pull/2067#discussion_r991371530


##########
cluster/metrics/sliding_window_counter.go:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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 metrics
+
+import (
+       "fmt"
+       "sync"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+)
+
+// SlidingWindowCounter is a policy for ring window based on time duration.
+// SlidingWindowCounter moves bucket offset with time duration.
+// e.g. If the last point is appended one bucket duration ago,
+// SlidingWindowCounter will increment current offset.
+type SlidingWindowCounter struct {
+       size           int
+       mu             sync.Mutex
+       buckets        []int64
+       count          int64
+       offset         int
+       bucketDuration time.Duration
+       lastAppendTime time.Time
+}
+
+// SlidingWindowCounterOpts contains the arguments for creating 
SlidingWindowCounter.
+type SlidingWindowCounterOpts struct {
+       Size           int
+       BucketDuration time.Duration
+}
+
+// NewSlidingWindowCounter creates a new SlidingWindowCounter based on the 
given window and SlidingWindowCounterOpts.
+func NewSlidingWindowCounter(opts SlidingWindowCounterOpts) 
*SlidingWindowCounter {
+       buckets := make([]int64, opts.Size)
+
+       return &SlidingWindowCounter{
+               size:           opts.Size,
+               offset:         0,
+               buckets:        buckets,
+               bucketDuration: opts.BucketDuration,
+               lastAppendTime: time.Now(),
+       }
+}
+
+func (c *SlidingWindowCounter) timespan() int {
+       v := int(time.Since(c.lastAppendTime) / c.bucketDuration)
+       if v > -1 { // maybe time backwards
+               return v
+       }
+       return c.size
+}
+
+func (c *SlidingWindowCounter) Add(_ int64) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+
+       //move offset
+       timespan := c.timespan()
+       if timespan > 0 {
+               start := (c.offset + 1) % c.size
+               end := (c.offset + timespan) % c.size
+               // reset the expired buckets
+               c.ResetBuckets(start, timespan)
+               c.offset = end
+               c.lastAppendTime = c.lastAppendTime.Add(time.Duration(timespan 
* int(c.bucketDuration)))
+       }
+
+       c.buckets[c.offset]++
+       c.count++
+}
+
+func (c *SlidingWindowCounter) Value() int64 {

Review Comment:
   Value is not a thread-safe method.



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