Copilot commented on code in PR #1345:
URL: https://github.com/apache/dubbo-admin/pull/1345#discussion_r2484248442
##########
pkg/core/bootstrap/bootstrap.go:
##########
@@ -57,7 +58,11 @@ func Bootstrap(appCtx context.Context, cfg app.AdminConfig)
(runtime.Runtime, er
if err := initializeConsole(builder); err != nil {
return nil, err
}
- // 6. initialize diagnotics
+ // 6. initialize counter manager
+ if err := initializeCounterManager(builder); err != nil {
+ return nil, err
+ }
+ // 7. initialize diagnotics
if err := initializeDiagnoticsServer(builder); err != nil {
logger.Errorf("got error when init diagnotics server %s", err)
Review Comment:
Corrected spelling of 'diagnotics' to 'diagnostics'.
```suggestion
// 7. initialize diagnostics
if err := initializeDiagnosticsServer(builder); err != nil {
logger.Errorf("got error when init diagnostics server %s", err)
```
##########
pkg/core/bootstrap/bootstrap.go:
##########
@@ -57,7 +58,11 @@ func Bootstrap(appCtx context.Context, cfg app.AdminConfig)
(runtime.Runtime, er
if err := initializeConsole(builder); err != nil {
return nil, err
}
- // 6. initialize diagnotics
+ // 6. initialize counter manager
+ if err := initializeCounterManager(builder); err != nil {
+ return nil, err
+ }
+ // 7. initialize diagnotics
if err := initializeDiagnoticsServer(builder); err != nil {
logger.Errorf("got error when init diagnotics server %s", err)
Review Comment:
Corrected spelling of 'diagnotics' to 'diagnostics'.
```suggestion
// 7. initialize diagnostics
if err := initializeDiagnosticsServer(builder); err != nil {
logger.Errorf("got error when init diagnostics server %s", err)
```
##########
pkg/console/counter/manager.go:
##########
@@ -0,0 +1,370 @@
+/*
+ * 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 counter
+
+import (
+ "fmt"
+ "math"
+
+ "k8s.io/client-go/tools/cache"
+
+ "github.com/apache/dubbo-admin/pkg/console/model"
+ "github.com/apache/dubbo-admin/pkg/core/events"
+ meshresource
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+ coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
+)
+
+type CounterType string
+
+const (
+ ApplicationCounter CounterType = "application"
+ ServiceCounter CounterType = "service"
+ InstanceCounter CounterType = "instance"
+
+ ProtocolCounter CounterType = "protocol"
+ ReleaseCounter CounterType = "release"
+ DiscoveryCounter CounterType = "discovery"
+)
+
+type CounterManager interface {
+ Overview() *model.OverviewResp
+ Reset()
+ Increment(counterType CounterType) error
+ Decrement(counterType CounterType) error
+ IncrementDistribution(counterType CounterType, key string) error
+ DecrementDistribution(counterType CounterType, key string) error
+ Bind(bus events.EventBus) error
+}
+
+type counterManager struct {
+ simpleCounters map[CounterType]*Counter
+ distCounters map[CounterType]*DistributionCounter
+}
+
+func NewCounterManager() CounterManager {
+ return newCounterManager()
+}
+
+func newCounterManager() *counterManager {
Review Comment:
[nitpick] The exported function NewCounterManager() simply wraps the
unexported newCounterManager() with no additional logic. This adds unnecessary
indirection. Consider removing newCounterManager() and implementing the logic
directly in NewCounterManager().
```suggestion
```
##########
pkg/console/counter/counter.go:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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 counter
+
+import (
+ "sync"
+ "sync/atomic"
+)
+
+type Counter struct {
+ name string
+ value atomic.Int64
+}
+
+func NewCounter(name string) *Counter {
+ return &Counter{name: name}
+}
+
+func (c *Counter) Get() int64 {
+ return c.value.Load()
+}
+
+func (c *Counter) Increment() {
+ c.value.Add(1)
+}
+
+func (c *Counter) Decrement() {
+ for {
+ current := c.value.Load()
+ if current == 0 {
+ return
+ }
+ if c.value.CompareAndSwap(current, current-1) {
+ return
+ }
+ }
+}
+
+func (c *Counter) Reset() {
+ c.value.Store(0)
+}
+
+type DistributionCounter struct {
+ name string
+ data map[string]int64
+ mu sync.RWMutex
+}
+
+func NewDistributionCounter(name string) *DistributionCounter {
+ return &DistributionCounter{
+ name: name,
+ data: make(map[string]int64),
+ }
+}
+
+func (c *DistributionCounter) Increment(key string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.data[key]++
+}
+
+func (c *DistributionCounter) Decrement(key string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ if value, ok := c.data[key]; ok {
+ value--
+ if value <= 0 {
+ delete(c.data, key)
+ } else {
+ c.data[key] = value
+ }
+ }
+}
+
+func (c *DistributionCounter) GetAll() map[string]int64 {
+ c.mu.RLock()
+ defer c.mu.RUnlock()
+ result := make(map[string]int64, len(c.data))
+ for k, v := range c.data {
+ result[k] = v
+ }
+ return result
+}
+
+func (c *DistributionCounter) Reset() {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ for k := range c.data {
+ delete(c.data, k)
+ }
Review Comment:
The Reset() method iteratively deletes map entries which can be inefficient.
Consider replacing the map with a new empty map: `c.data =
make(map[string]int64)`.
```suggestion
c.data = make(map[string]int64)
```
--
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]