-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add zookeeper * fix: update go.mod * feat: add example for zookeeper; use log insteadof grpclog Co-authored-by: Shengwen Yu <yushengwen@tron.network>
- Loading branch information
Showing
10 changed files
with
765 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package balancer | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"math/rand" | ||
"strconv" | ||
"sync" | ||
|
||
"google.golang.org/grpc/balancer" | ||
"google.golang.org/grpc/balancer/base" | ||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
const RoundRobin = "round_robin" | ||
|
||
// newRoundRobinBuilder creates a new roundrobin balancer builder. | ||
func newRoundRobinBuilder() balancer.Builder { | ||
return base.NewBalancerBuilderWithConfig(RoundRobin, &roundRobinPickerBuilder{}, base.Config{HealthCheck: true}) | ||
} | ||
|
||
func InitRoundRobin() { | ||
balancer.Register(newRoundRobinBuilder()) | ||
} | ||
|
||
type roundRobinPickerBuilder struct{} | ||
|
||
func (*roundRobinPickerBuilder) Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker { | ||
log.Printf("roundrobinPicker: newPicker called with readySCs: %v\n", readySCs) | ||
|
||
if len(readySCs) == 0 { | ||
return base.NewErrPicker(balancer.ErrNoSubConnAvailable) | ||
} | ||
var scs []balancer.SubConn | ||
for addr, sc := range readySCs { | ||
weight := 1 | ||
if addr.Metadata != nil { | ||
if m, ok := addr.Metadata.(*map[string]string); ok { | ||
w, ok := (*m)["weight"] | ||
if ok { | ||
n, err := strconv.Atoi(w) | ||
if err == nil && n > 0 { | ||
weight = n | ||
} | ||
} | ||
} | ||
} | ||
for i := 0; i < weight; i++ { | ||
scs = append(scs, sc) | ||
} | ||
} | ||
|
||
return &roundRobinPicker{ | ||
subConns: scs, | ||
next: rand.Intn(len(scs)), | ||
} | ||
} | ||
|
||
type roundRobinPicker struct { | ||
subConns []balancer.SubConn | ||
mu sync.Mutex | ||
next int | ||
} | ||
|
||
func (p *roundRobinPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) { | ||
p.mu.Lock() | ||
sc := p.subConns[p.next] | ||
p.next = (p.next + 1) % len(p.subConns) | ||
p.mu.Unlock() | ||
return sc, nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
registry "github.com/TRON-US/chaos/zookeeper" | ||
"github.com/TRON-US/chaos/zookeeper/balancer" | ||
"github.com/TRON-US/chaos/zookeeper/example/proto" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
"log" | ||
"time" | ||
) | ||
|
||
func main() { | ||
registry.RegisterResolver("zk", []string{"127.0.0.1:2181"}, "test", "v1.0") | ||
//c, err := grpc.Dial("zk:///", grpc.WithInsecure(), grpc.WithBalancerName(balancer.RoundRobin)) | ||
balancer.InitRoundRobin() | ||
c, err := grpc.Dial("zk:///", grpc.WithInsecure(), grpc.WithBalancerName(balancer.RoundRobin)) | ||
if err != nil { | ||
log.Printf("grpc dial: %s", err) | ||
return | ||
} | ||
defer c.Close() | ||
client := proto.NewTestClient(c) | ||
|
||
for i := 0; i < 5000; i++ { | ||
resp, err := client.Say(context.Background(), &proto.SayReq{Content: "round robin"}) | ||
if err != nil { | ||
log.Println("aa:", err) | ||
time.Sleep(time.Second) | ||
continue | ||
} | ||
time.Sleep(time.Second) | ||
log.Printf(resp.Content) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//protoc --go_out=plugins=grpc:. hello.proto | ||
|
||
syntax = "proto3"; | ||
|
||
package proto; | ||
|
||
message SayReq { | ||
string content = 1; | ||
} | ||
|
||
message SayResp { | ||
string content = 1; | ||
} | ||
|
||
service Test{ | ||
rpc Say(SayReq) returns (SayResp) {} | ||
} |
Oops, something went wrong.