Skip to content

Commit

Permalink
修复轮询算法高并发报no valid问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed May 10, 2024
1 parent 0247644 commit afbf005
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
5 changes: 3 additions & 2 deletions upstream/round-robin/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package round_robin

import (
"errors"
"github.com/eolinker/apinto/utils/queue"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/eolinker/apinto/utils/queue"

eoscContext "github.com/eolinker/eosc/eocontext"

"github.com/eolinker/apinto/upstream/balance"
Expand Down Expand Up @@ -101,7 +102,7 @@ func (r *roundRobin) Next() (eoscContext.INode, int, error) {

nodeValue.effectiveWeight -= r.gcdWeight

if nodeValue.weight > 0 {
if nodeValue.effectiveWeight > 0 {
r.nodeQueue.Push(entry)
} else {
nodeValue.effectiveWeight = nodeValue.weight
Expand Down
49 changes: 47 additions & 2 deletions upstream/round-robin/round_robin_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package round_robin

import (
"github.com/eolinker/apinto/drivers/discovery/static"
"github.com/eolinker/eosc/eocontext"
"runtime"
"sync"
"testing"
"time"

"github.com/eolinker/apinto/drivers/discovery/static"
"github.com/eolinker/eosc/eocontext"
)

type demoNode struct {
Expand Down Expand Up @@ -107,3 +108,47 @@ func Test_roundRobin_Next_Retry_Status(t *testing.T) {
}
wg.Wait()
}

func Test_roundRobin_Next_Retry_Status_2(t *testing.T) {
discovery := static.CreateAnonymous(&static.Config{
HealthOn: false,
Health: nil,
})

app, err := discovery.GetApp("")
if err != nil {
return
}
robin := newRoundRobin(app, "http", time.Second)
for i := 0; i < 12; i++ {
n, _, err := robin.Select(nil)
if err != nil {
t.Error(err)
return
}
t.Log(i, n.Addr())
}
}

var ()

func Benchmark_roundRobin_Next_Retry_demo(b *testing.B) {
discovery := static.CreateAnonymous(&static.Config{
HealthOn: false,
Health: nil,
})
app, err := discovery.GetApp("127.0.0.1:8080;127.0.0.1:8081;127.0.0.1:8083;127.0.0.1:8084")
if err != nil {
b.Fatal(err)
}

robin := newRoundRobin(app, "http", time.Second)
for i := 0; i < b.N; i++ {
_, _, err := robin.Select(nil)
if err != nil {
b.Fatal(err)
return
}
//b.Log(i, n.Addr())
}
}

0 comments on commit afbf005

Please sign in to comment.