Skip to content

Commit 0c0e2f4

Browse files
committed
fix:1.重修随机浮点数方法,2.redis锁代码优化
1 parent a8a4cec commit 0c0e2f4

File tree

3 files changed

+9
-55
lines changed

3 files changed

+9
-55
lines changed

utils/number.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,11 @@ func RandomFloat64(start, end float64, precision int) float64 {
8181
if start == end {
8282
return start
8383
}
84-
var scale = 1
8584

86-
//对start进行放大
87-
for {
88-
startScaled := start * float64(scale)
89-
if startScaled == float64(int64(startScaled)) {
90-
break
91-
}
92-
scale *= 10
93-
}
94-
95-
//对end进行放大
96-
for {
97-
endScaled := end * float64(scale)
98-
if endScaled == float64(int64(endScaled)) {
99-
break
100-
}
101-
scale *= 10
102-
}
103-
104-
start *= float64(scale)
105-
end *= float64(scale)
106-
107-
randInt64 := RandomInt64(int64(start), int64(end))
85+
delta := end - start
86+
result := start + rand.Float64()*delta
10887

109-
return decimal.NewFromInt(randInt64).
110-
Div(decimal.NewFromInt(int64(scale))).
88+
return decimal.NewFromFloat(result).
11189
Truncate(int32(precision)).InexactFloat64()
11290
}
11391

utils/number_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestRandomFloat64(t *testing.T) {
4646
{-10.11, 0.11, 4},
4747
{-10.101, 100.10111111, 5},
4848
{-100.1011, 100.1011111111, 6},
49+
{-200, -100, 6},
4950
}
5051
for _, rn := range ranges {
5152
t.Log("range: from", rn.start, "to", rn.end, "precision", rn.precision)

utils/redislock.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ type CancelFunc func()
3535
// 该方法会立即返回锁定成功与否的结果
3636
func RedisTryLock(key string) bool {
3737
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
38-
go func() {
39-
for range time.After(redisExecuteTimeout) {
40-
cancel()
41-
break
42-
}
43-
}()
38+
defer cancel()
4439

4540
return RedisTryLockWithContext(ctx, key)
4641
}
@@ -143,12 +138,7 @@ func RedisUnlockWithContext(ctx context.Context, key string) {
143138
// using SetNX
144139
func RedisStandaloneLock(key string) bool {
145140
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
146-
go func() {
147-
for range time.After(redisExecuteTimeout) {
148-
cancel()
149-
break
150-
}
151-
}()
141+
defer cancel()
152142

153143
return RedisStandaloneLockWithContext(ctx, key)
154144
}
@@ -197,12 +187,7 @@ func RedisStandaloneLockWithContext(ctx context.Context, key string) bool {
197187
// using SetNX
198188
func RedisStandaloneUnlock(key string) {
199189
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
200-
go func() {
201-
for range time.After(redisExecuteTimeout) {
202-
cancel()
203-
break
204-
}
205-
}()
190+
defer cancel()
206191

207192
RedisStandaloneUnlockWithContext(ctx, key)
208193
}
@@ -232,12 +217,7 @@ func RedisStandaloneUnlockWithContext(ctx context.Context, key string) {
232217
// using SetNX
233218
func RedisClusterLock(key string) bool {
234219
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
235-
go func() {
236-
for range time.After(redisExecuteTimeout) {
237-
cancel()
238-
break
239-
}
240-
}()
220+
defer cancel()
241221

242222
return RedisClusterLockWithContext(ctx, key)
243223
}
@@ -286,12 +266,7 @@ func RedisClusterLockWithContext(ctx context.Context, key string) bool {
286266
// using SetNX
287267
func RedisClusterUnlock(key string) {
288268
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
289-
go func() {
290-
for range time.After(redisExecuteTimeout) {
291-
cancel()
292-
break
293-
}
294-
}()
269+
defer cancel()
295270

296271
RedisClusterUnlockWithContext(ctx, key)
297272
}

0 commit comments

Comments
 (0)