Skip to content

Commit 1555a81

Browse files
purge existing panic operations (#284)
* purge existing panic operations in core/stat module Co-authored-by: louyuting <[email protected]>
1 parent d68bfad commit 1555a81

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

core/stat/base/atomic_window_wrap_array_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func Test_atomicBucketWrapArray_elementOffset(t *testing.T) {
3939
t.Run(tt.name, func(t *testing.T) {
4040
now := uint64(1596199310000)
4141
aa := NewAtomicBucketWrapArrayWithTime(tt.args.len, tt.args.bucketLengthInMs, now, tt.args.bg)
42-
if got := uintptr(aa.elementOffset(tt.args.idx)) - uintptr(aa.base); got != tt.want {
42+
offset, ok := aa.elementOffset(tt.args.idx)
43+
assert.True(t, ok)
44+
if got := uintptr(offset) - uintptr(aa.base); got != tt.want {
4345
t.Errorf("AtomicBucketWrapArray.elementOffset() = %v, want %v \n", got, tt.want)
4446
}
4547
})

core/stat/base/bucket_leap_array.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package base
22

33
import (
4-
"fmt"
54
"sync/atomic"
65

76
"github.com/alibaba/sentinel-golang/core/base"
@@ -30,10 +29,9 @@ func (bla *BucketLeapArray) ResetBucketTo(bw *BucketWrap, startTime uint64) *Buc
3029

3130
// sampleCount is the number of slots
3231
// intervalInMs is the time length of sliding window
32+
// sampleCount and intervalInMs must be positive and intervalInMs%sampleCount == 0,
33+
// the validation must be done before call NewBucketLeapArray
3334
func NewBucketLeapArray(sampleCount uint32, intervalInMs uint32) *BucketLeapArray {
34-
if intervalInMs%sampleCount != 0 {
35-
panic(fmt.Sprintf("Invalid parameters, intervalInMs is %d, sampleCount is %d.", intervalInMs, sampleCount))
36-
}
3735
bucketLengthInMs := intervalInMs / sampleCount
3836
ret := &BucketLeapArray{
3937
data: LeapArray{

core/stat/base/leap_array.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"unsafe"
88

99
"github.com/alibaba/sentinel-golang/core/base"
10+
"github.com/alibaba/sentinel-golang/logging"
1011
"github.com/alibaba/sentinel-golang/util"
1112
"github.com/pkg/errors"
1213
)
@@ -93,25 +94,34 @@ func NewAtomicBucketWrapArray(len int, bucketLengthInMs uint32, generator Bucket
9394
return NewAtomicBucketWrapArrayWithTime(len, bucketLengthInMs, util.CurrentTimeMillis(), generator)
9495
}
9596

96-
func (aa *AtomicBucketWrapArray) elementOffset(idx int) unsafe.Pointer {
97+
func (aa *AtomicBucketWrapArray) elementOffset(idx int) (unsafe.Pointer, bool) {
9798
if idx >= aa.length || idx < 0 {
98-
panic(fmt.Sprintf("The index (%d) is out of bounds, length is %d.", idx, aa.length))
99+
logging.Error(errors.New("array index out of bounds"),
100+
"array index out of bounds in AtomicBucketWrapArray.elementOffset()",
101+
"idx", idx, "arrayLength", aa.length)
102+
return nil, false
99103
}
100104
basePtr := aa.base
101-
return unsafe.Pointer(uintptr(basePtr) + uintptr(idx*PtrSize))
105+
return unsafe.Pointer(uintptr(basePtr) + uintptr(idx*PtrSize)), true
102106
}
103107

104108
func (aa *AtomicBucketWrapArray) get(idx int) *BucketWrap {
105109
// aa.elementOffset(idx) return the secondary pointer of BucketWrap, which is the pointer to the aa.data[idx]
106110
// then convert to (*unsafe.Pointer)
107-
return (*BucketWrap)(atomic.LoadPointer((*unsafe.Pointer)(aa.elementOffset(idx))))
111+
if offset, ok := aa.elementOffset(idx); ok {
112+
return (*BucketWrap)(atomic.LoadPointer((*unsafe.Pointer)(offset)))
113+
}
114+
return nil
108115
}
109116

110117
func (aa *AtomicBucketWrapArray) compareAndSet(idx int, except, update *BucketWrap) bool {
111118
// aa.elementOffset(idx) return the secondary pointer of BucketWrap, which is the pointer to the aa.data[idx]
112119
// then convert to (*unsafe.Pointer)
113120
// update secondary pointer
114-
return atomic.CompareAndSwapPointer((*unsafe.Pointer)(aa.elementOffset(idx)), unsafe.Pointer(except), unsafe.Pointer(update))
121+
if offset, ok := aa.elementOffset(idx); ok {
122+
return atomic.CompareAndSwapPointer((*unsafe.Pointer)(offset), unsafe.Pointer(except), unsafe.Pointer(update))
123+
}
124+
return false
115125
}
116126

117127
// The BucketWrap leap array,

core/stat/base/metric_bucket.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package base
22

33
import (
4-
"fmt"
54
"sync/atomic"
65

76
"github.com/alibaba/sentinel-golang/core/base"
7+
"github.com/alibaba/sentinel-golang/logging"
8+
"github.com/pkg/errors"
89
)
910

1011
// MetricBucket represents the entity to record metrics per minimum time unit (i.e. the bucket time span).
@@ -25,7 +26,8 @@ func NewMetricBucket() *MetricBucket {
2526
// Add statistic count for the given metric event.
2627
func (mb *MetricBucket) Add(event base.MetricEvent, count int64) {
2728
if event >= base.MetricEventTotal || event < 0 {
28-
panic(fmt.Sprintf("Unknown metric event: %v", event))
29+
logging.Error(errors.Errorf("Unknown metric event: %v", event), "")
30+
return
2931
}
3032
if event == base.MetricEventRt {
3133
mb.AddRt(count)
@@ -41,7 +43,8 @@ func (mb *MetricBucket) addCount(event base.MetricEvent, count int64) {
4143
// Get current statistic count of the given metric event.
4244
func (mb *MetricBucket) Get(event base.MetricEvent) int64 {
4345
if event >= base.MetricEventTotal || event < 0 {
44-
panic(fmt.Sprintf("Unknown metric event: %v", event))
46+
logging.Error(errors.Errorf("Unknown metric event: %v", event), "")
47+
return 0
4548
}
4649
return atomic.LoadInt64(&mb.counter[event])
4750
}

0 commit comments

Comments
 (0)