Copilot commented on code in PR #1345:
URL: https://github.com/apache/dubbo-admin/pull/1345#discussion_r2507676109


##########
pkg/console/counter/component.go:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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"
+
+       "github.com/apache/dubbo-admin/pkg/core/events"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/core/runtime"
+)
+
+const ComponentType runtime.ComponentType = "counter manager"
+
+func init() {
+       runtime.RegisterComponent(&managerComponent{})
+}
+
+type ManagerComponent interface {
+       runtime.Component
+       CounterManager() CounterManager
+}
+
+var _ ManagerComponent = &managerComponent{}
+
+type managerComponent struct {
+       manager CounterManager
+}
+
+func (c *managerComponent) Type() runtime.ComponentType {
+       return ComponentType
+}
+
+func (c *managerComponent) Order() int {
+       return math.MaxInt - 1
+}
+
+func (c *managerComponent) Init(runtime.BuilderContext) error {
+       mgr := NewCounterManager()
+       mgr.RegisterSimpleCounter(meshresource.ApplicationKind)
+       mgr.RegisterSimpleCounter(meshresource.ServiceKind)
+       mgr.RegisterSimpleCounter(meshresource.InstanceKind)
+
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ProtocolCounter, instanceProtocolKey)
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ReleaseCounter, instanceReleaseKey)
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
DiscoveryCounter, instanceMeshKey)

Review Comment:
   The functions `instanceProtocolKey`, `instanceReleaseKey`, and 
`instanceMeshKey` are referenced here but are defined in `manager.go` and are 
not exported. This creates an unnecessary dependency and couples the component 
to internal implementation details of the manager package. Consider either 
exporting these functions or passing them as configuration when creating the 
component.
   ```suggestion
        mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ProtocolCounter, InstanceProtocolKey)
        mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ReleaseCounter, InstanceReleaseKey)
        mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
DiscoveryCounter, InstanceMeshKey)
   ```



##########
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 `DistributionCounter.Reset()` method iterates through the map and 
deletes entries one by one. For better performance and idiomatic Go code, 
consider replacing the map with a new one: `c.data = make(map[string]int64)` 
instead of iterating and deleting.
   ```suggestion
        c.data = make(map[string]int64)
   ```



##########
pkg/console/counter/component.go:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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"
+
+       "github.com/apache/dubbo-admin/pkg/core/events"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/core/runtime"
+)
+
+const ComponentType runtime.ComponentType = "counter manager"
+
+func init() {
+       runtime.RegisterComponent(&managerComponent{})
+}
+
+type ManagerComponent interface {
+       runtime.Component
+       CounterManager() CounterManager
+}
+
+var _ ManagerComponent = &managerComponent{}
+
+type managerComponent struct {
+       manager CounterManager
+}
+
+func (c *managerComponent) Type() runtime.ComponentType {
+       return ComponentType
+}
+
+func (c *managerComponent) Order() int {
+       return math.MaxInt - 1
+}
+
+func (c *managerComponent) Init(runtime.BuilderContext) error {
+       mgr := NewCounterManager()
+       mgr.RegisterSimpleCounter(meshresource.ApplicationKind)
+       mgr.RegisterSimpleCounter(meshresource.ServiceKind)
+       mgr.RegisterSimpleCounter(meshresource.InstanceKind)
+
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ProtocolCounter, instanceProtocolKey)
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
ReleaseCounter, instanceReleaseKey)
+       mgr.RegisterDistributionCounter(meshresource.InstanceKind, 
DiscoveryCounter, instanceMeshKey)
+

Review Comment:
   The counter initialization logic is duplicated. `NewCounterManager()` 
already registers these counters (lines 73-79 in manager.go), but they are 
being registered again in the `Init` method here. This duplication is 
unnecessary and could lead to maintenance issues.
   ```suggestion
   
   ```



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

Review Comment:
   Typo in the comment: "diagnotics" should be "diagnostics".



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