Skip to content

Commit

Permalink
386 bug overflow (#259)
Browse files Browse the repository at this point in the history
* 修复32平台overflow问题

* 修改注释

* 修改action
  • Loading branch information
guonaihong authored Oct 22, 2020
1 parent 5d2c07e commit ec4e476
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ jobs:
- name: Build
run: go build -v .

- name: Test
run: go test -v -coverprofile='coverage.out' -covermode=count ./...
- name: Test-386
run: env GOARCH=386 go test -test.run=Test_Retry_sleep -v
#run: env GOARCH=386 go test -v -coverprofile='coverage.out' -covermode=count ./...

- name: Test-amd64
run: env GOARCH=amd64 go test -v -coverprofile='coverage.out' -covermode=count ./...


- name: Upload Coverage report
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion dataflow/dataflow_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type testHeader2 struct {

type testSetHeader struct {
H1 string `header:"h1"`
H2 int `header:"h2"`
H2 int64 `header:"h2"`
H3 float32 `header:"h3"`
H4 float64 `header:"h4"`
H5 time.Time `header:"h5" time_format:"unix"`
Expand Down
2 changes: 1 addition & 1 deletion decode/decode_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestHeaderDecode(t *testing.T) {
assert.Equal(t, float32(32.1), theader.F32)
assert.Equal(t, int64(1562400033000000123), theader.CreateTime.UnixNano())
assert.Equal(t, int64(1562400033), theader.UnixTime.Unix())
assert.Equal(t, int(time.Hour)+int(time.Second), int(theader.Duration))
assert.Equal(t, int64(time.Hour)+int64(time.Second), int64(theader.Duration))
assert.Equal(t, true, theader.Bool)

assert.Equal(t, Tstruct{X: 3, Y: 4}, theader.Tstruct)
Expand Down
2 changes: 1 addition & 1 deletion encode/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func TestQueryStruct(t *testing.T) {

assert.NoError(t, err)

assert.Equal(t, q.End(), "q1=v1&q2=v2&q3=v3&q4="+strconv.Itoa(int(unixTime.Unix()))+"&q5="+strconv.Itoa(int(unixNano.UnixNano())))
assert.Equal(t, q.End(), "q1=v1&q2=v2&q3=v3&q4="+strconv.FormatInt(unixTime.Unix(), 10)+"&q5="+strconv.FormatInt(unixNano.UnixNano(), 10))
}
15 changes: 10 additions & 5 deletions filter/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,26 @@ func (r *Retry) init() {
}

// Does not pollute the namespace
func (r *Retry) min(a, b time.Duration) time.Duration {
func (r *Retry) min(a, b uint64) uint64 {
if a > b {
return b
}
return a
}

func (r *Retry) getSleep() time.Duration {
temp := r.waitTime * time.Duration(math.Exp2(float64(r.currAttempt)))
temp := uint64(r.waitTime * time.Duration(math.Exp2(float64(r.currAttempt))))
if temp <= 0 {
temp = r.waitTime
temp = uint64(r.waitTime)
}
temp = r.min(r.maxWaitTime, temp)
temp = r.min(uint64(r.maxWaitTime), uint64(temp))
//对int64边界处理, 后面使用rand.Int63n所以,最大值只能是int64的最大值防止溢出
if temp > math.MaxInt64 {
temp = math.MaxInt64
}

temp /= 2
return temp + time.Duration(rand.Intn(int(temp)))
return time.Duration(temp) + time.Duration(rand.Int63n(int64(temp)))
}

func (r *Retry) genContext(resp *http.Response, err error) *dataflow.Context {
Expand Down
18 changes: 12 additions & 6 deletions filter/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ func Test_Retry_min(t *testing.T) {

r := Retry{}
for _, v := range test {
assert.Equal(t, r.min(time.Duration(v.a), time.Duration(v.b)), time.Duration(v.need))
assert.Equal(t, r.min(uint64(v.a), uint64(v.b)), uint64(v.need))
}
}

func Test_Retry_sleep(t *testing.T) {
r := Retry{attempt: 100}
r := Retry{attempt: 100, maxWaitTime: 10 * time.Second, waitTime: time.Second}
r.init()

// 方便画出曲线图
for i := 0; i < r.attempt; i++ {
sleep := r.getSleep()
fmt.Printf("%d\n", sleep)
//fmt.Printf("%d,%v\n", sleep, sleep)
r.currAttempt++
cb := func() {
sleep := r.getSleep()
fmt.Printf("%d\n", sleep)
//fmt.Printf("%d,%v\n", sleep, sleep)
r.currAttempt++
}
b := assert.NotPanics(t, cb)
if !b {
break
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gout

// Version show version
const Version = "v0.1.2"
const Version = "v0.1.3"

0 comments on commit ec4e476

Please sign in to comment.