Skip to content

Commit

Permalink
Merge pull request #30 from andrewshan/main
Browse files Browse the repository at this point in the history
feat: 补齐相关用例,支持限流能力
  • Loading branch information
andrewshan authored Apr 14, 2022
2 parents 409d3ce + 98da152 commit bd7b966
Show file tree
Hide file tree
Showing 24 changed files with 809 additions and 35 deletions.
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
target = fmt.Sprintf("%s?%s=%s", target, optionsKey, endpoint)
return grpc.DialContext(ctx, target, options.gRPCDialOptions...)
}

// BuildDialTarget build the invoke grpc target
func BuildTarget(target string, opts ...DialOption) (string, error) {
options := &dialOptions{}
for _, opt := range opts {
opt.apply(options)
}
jsonStr, err := json.Marshal(options)
if nil != err {
return "", fmt.Errorf("fail to marshal options: %v", err)
}
endpoint := base64.URLEncoding.EncodeToString(jsonStr)
target = fmt.Sprintf(prefix+"%s?%s=%s", target, optionsKey, endpoint)
return target, nil
}
117 changes: 117 additions & 0 deletions examples/circuitbreak/consumer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Tencent is pleased to support the open source community by making CL5 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package main

import (
"context"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"

"google.golang.org/grpc"

_ "github.com/polarismesh/grpc-go-polaris"
"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)

const (
listenPort = 0
defaultCount = 20
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn, err := grpc.DialContext(ctx, "polaris://CircuitBreakerEchoServerGRPC/", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()

address := fmt.Sprintf("0.0.0.0:%d", listenPort)
listen, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("Failed to listen addr %s: %v", address, err)
}
listenAddr := listen.Addr().String()
fmt.Printf("listen address is %s\n", listenAddr)

echoClient := pb.NewEchoServerClient(conn)
echoHandler := &EchoHandler{
echoClient: echoClient,
ctx: ctx,
}
if err := http.Serve(listen, echoHandler); nil != err {
log.Fatal(err)
}
}

type EchoHandler struct {
echoClient pb.EchoServerClient

ctx context.Context
}

func (s *EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if nil != err {
log.Printf("fail to parse request form: %v\n", err)
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
return
}
values := r.Form["value"]
log.Printf("receive value is %s\n", values)
var value string
if len(values) > 0 {
value = values[0]
}

counts := r.Form["count"]
log.Printf("receive count is %s\n", counts)
count := defaultCount
if len(counts) > 0 {
v, err := strconv.Atoi(counts[0])
if nil != err {
log.Printf("parse count value %s into int fail, err: %s", counts[0], err)
}
if v > 0 {
count = v
}
}
builder := strings.Builder{}
for i := 0; i < count; i++ {
resp, err := s.echoClient.Echo(s.ctx, &pb.EchoRequest{Value: value})
log.Printf("%d, send message %s, resp (%v), err(%v)\n", i, value, resp, err)
if nil != err {
builder.Write([]byte(err.Error()))
builder.WriteByte('\n')
continue
}
builder.Write([]byte(resp.GetValue()))
builder.WriteByte('\n')
}
w.WriteHeader(200)
_, _ = w.Write([]byte(builder.String()))
time.Sleep(100 * time.Millisecond)

}
4 changes: 4 additions & 0 deletions examples/circuitbreak/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
serverConnector:
addresses:
- 9.134.15.118:8091
79 changes: 79 additions & 0 deletions examples/circuitbreak/provider/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Tencent is pleased to support the open source community by making CL5 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package main

import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"

polaris "github.com/polarismesh/grpc-go-polaris"

"google.golang.org/grpc"

"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)

var (
listenPort int
)

// EchoCircuitBreakerService gRPC echo service struct
type EchoCircuitBreakerService struct {
address string
}

// Echo gRPC testing method
func (h *EchoCircuitBreakerService) Echo(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
return &pb.EchoResponse{Value: fmt.Sprintf("echo: %s, from %s", req.Value, h.address)}, nil
}

func main() {
address := fmt.Sprintf("0.0.0.0:%d", listenPort)
listen, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}
listenAddr := listen.Addr().String()
fmt.Printf("listen address is %s\n", listenAddr)
srv := grpc.NewServer()
pb.RegisterEchoServerServer(srv, &EchoCircuitBreakerService{address: listenAddr})
// 执行北极星的注册命令
_, err = polaris.Register(srv, listen,
polaris.WithServerApplication("CircuitBreakerEchoServerGRPC"),
polaris.WithHeartbeatEnable(false))
if nil != err {
log.Fatal(err)
}
go func() {
c := make(chan os.Signal)
signal.Notify(c)
s := <-c
log.Printf("receive quit signal: %v", s)
// 执行北极星的反注册命令
//pSrv.Deregister()
srv.GracefulStop()
}()
err = srv.Serve(listen)
if nil != err {
log.Printf("listen err: %v", err)
}
}
4 changes: 4 additions & 0 deletions examples/circuitbreak/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
serverConnector:
addresses:
- 9.134.15.118:8091

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
5 changes: 2 additions & 3 deletions examples/quickstart/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
"google.golang.org/grpc"

_ "github.com/polarismesh/grpc-go-polaris"
"github.com/polarismesh/grpc-go-polaris/examples/quickstart/pb"

"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)

const (
Expand All @@ -38,7 +37,7 @@ func main() {
// grpc客户端连接获取
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn, err := grpc.DialContext(ctx, "polaris://EchoServerGRPC/", grpc.WithInsecure())
conn, err := grpc.DialContext(ctx, "polaris://QuickStartEchoServerGRPC/", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/quickstart/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
- 9.134.15.118:8091
12 changes: 6 additions & 6 deletions examples/quickstart/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ import (

"google.golang.org/grpc"

"github.com/polarismesh/grpc-go-polaris/examples/quickstart/pb"
"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)

const (
listenPort = 16010
)

// EchoService gRPC echo service struct
type EchoService struct{}
// EchoQuickStartService gRPC echo service struct
type EchoQuickStartService struct{}

// Echo gRPC testing method
func (h *EchoService) Echo(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
func (h *EchoQuickStartService) Echo(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
return &pb.EchoResponse{Value: "echo: " + req.Value}, nil
}

func main() {
srv := grpc.NewServer()
pb.RegisterEchoServerServer(srv, &EchoService{})
pb.RegisterEchoServerServer(srv, &EchoQuickStartService{})
address := fmt.Sprintf("0.0.0.0:%d", listenPort)
listen, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}
// 执行北极星的注册命令
pSrv, err := polaris.Register(srv, listen, polaris.WithServerApplication("EchoServerGRPC"))
pSrv, err := polaris.Register(srv, listen, polaris.WithServerApplication("QuickStartEchoServerGRPC"))
if nil != err {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/quickstart/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
- 9.134.15.118:8091
Loading

0 comments on commit bd7b966

Please sign in to comment.