|
7 | 7 | "unsafe" |
8 | 8 |
|
9 | 9 | "github.com/alibaba/sentinel-golang/core/base" |
| 10 | + "github.com/alibaba/sentinel-golang/logging" |
10 | 11 | "github.com/alibaba/sentinel-golang/util" |
11 | 12 | "github.com/pkg/errors" |
12 | 13 | ) |
@@ -93,25 +94,34 @@ func NewAtomicBucketWrapArray(len int, bucketLengthInMs uint32, generator Bucket |
93 | 94 | return NewAtomicBucketWrapArrayWithTime(len, bucketLengthInMs, util.CurrentTimeMillis(), generator) |
94 | 95 | } |
95 | 96 |
|
96 | | -func (aa *AtomicBucketWrapArray) elementOffset(idx int) unsafe.Pointer { |
| 97 | +func (aa *AtomicBucketWrapArray) elementOffset(idx int) (unsafe.Pointer, bool) { |
97 | 98 | 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 |
99 | 103 | } |
100 | 104 | basePtr := aa.base |
101 | | - return unsafe.Pointer(uintptr(basePtr) + uintptr(idx*PtrSize)) |
| 105 | + return unsafe.Pointer(uintptr(basePtr) + uintptr(idx*PtrSize)), true |
102 | 106 | } |
103 | 107 |
|
104 | 108 | func (aa *AtomicBucketWrapArray) get(idx int) *BucketWrap { |
105 | 109 | // aa.elementOffset(idx) return the secondary pointer of BucketWrap, which is the pointer to the aa.data[idx] |
106 | 110 | // 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 |
108 | 115 | } |
109 | 116 |
|
110 | 117 | func (aa *AtomicBucketWrapArray) compareAndSet(idx int, except, update *BucketWrap) bool { |
111 | 118 | // aa.elementOffset(idx) return the secondary pointer of BucketWrap, which is the pointer to the aa.data[idx] |
112 | 119 | // then convert to (*unsafe.Pointer) |
113 | 120 | // 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 |
115 | 125 | } |
116 | 126 |
|
117 | 127 | // The BucketWrap leap array, |
|
0 commit comments