-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from andrewshan/main
feat: 补齐相关用例,支持限流能力
- Loading branch information
Showing
24 changed files
with
809 additions
and
35 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
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) | ||
|
||
} |
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,4 @@ | ||
global: | ||
serverConnector: | ||
addresses: | ||
- 9.134.15.118:8091 |
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,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) | ||
} | ||
} |
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,4 @@ | ||
global: | ||
serverConnector: | ||
addresses: | ||
- 9.134.15.118:8091 |
10 changes: 5 additions & 5 deletions
10
examples/quickstart/pb/echo.pb.go → examples/common/pb/echo.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
global: | ||
serverConnector: | ||
addresses: | ||
- 127.0.0.1:8091 | ||
- 9.134.15.118:8091 |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
global: | ||
serverConnector: | ||
addresses: | ||
- 127.0.0.1:8091 | ||
- 9.134.15.118:8091 |
Oops, something went wrong.