mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 02:43:36 +00:00
vendor update for CSI 0.3.0
This commit is contained in:
8
vendor/google.golang.org/grpc/examples/gotutorial.md
generated
vendored
8
vendor/google.golang.org/grpc/examples/gotutorial.md
generated
vendored
@ -313,7 +313,7 @@ Now let's look at how we call our service methods. Note that in gRPC-Go, RPCs op
|
||||
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local method.
|
||||
|
||||
```go
|
||||
feature, err := client.GetFeature(context.Background(), &pb.Point{409146138, -746188906})
|
||||
feature, err := client.GetFeature(ctx, &pb.Point{409146138, -746188906})
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
@ -331,7 +331,7 @@ Here's where we call the server-side streaming method `ListFeatures`, which retu
|
||||
|
||||
```go
|
||||
rect := &pb.Rectangle{ ... } // initialize a pb.Rectangle
|
||||
stream, err := client.ListFeatures(context.Background(), rect)
|
||||
stream, err := client.ListFeatures(ctx, rect)
|
||||
if err != nil {
|
||||
...
|
||||
}
|
||||
@ -364,7 +364,7 @@ for i := 0; i < pointCount; i++ {
|
||||
points = append(points, randomPoint(r))
|
||||
}
|
||||
log.Printf("Traversing %d points.", len(points))
|
||||
stream, err := client.RecordRoute(context.Background())
|
||||
stream, err := client.RecordRoute(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("%v.RecordRoute(_) = _, %v", client, err)
|
||||
}
|
||||
@ -387,7 +387,7 @@ The `RouteGuide_RecordRouteClient` has a `Send()` method that we can use to send
|
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. As in the case of `RecordRoute`, we only pass the method a context object and get back a stream that we can use to both write and read messages. However, this time we return values via our method's stream while the server is still writing messages to *their* message stream.
|
||||
|
||||
```go
|
||||
stream, err := client.RouteChat(context.Background())
|
||||
stream, err := client.RouteChat(ctx)
|
||||
waitc := make(chan struct{})
|
||||
go func() {
|
||||
for {
|
||||
|
5
vendor/google.golang.org/grpc/examples/helloworld/greeter_client/main.go
generated
vendored
5
vendor/google.golang.org/grpc/examples/helloworld/greeter_client/main.go
generated
vendored
@ -21,6 +21,7 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
@ -46,7 +47,9 @@ func main() {
|
||||
if len(os.Args) > 1 {
|
||||
name = os.Args[1]
|
||||
}
|
||||
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
|
||||
if err != nil {
|
||||
log.Fatalf("could not greet: %v", err)
|
||||
}
|
||||
|
88
vendor/google.golang.org/grpc/examples/helloworld/helloworld/helloworld.pb.go
generated
vendored
88
vendor/google.golang.org/grpc/examples/helloworld/helloworld/helloworld.pb.go
generated
vendored
@ -1,16 +1,6 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: helloworld.proto
|
||||
|
||||
/*
|
||||
Package helloworld is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
helloworld.proto
|
||||
|
||||
It has these top-level messages:
|
||||
HelloRequest
|
||||
HelloReply
|
||||
*/
|
||||
package helloworld
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
@ -35,13 +25,35 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
// The request message containing the user's name.
|
||||
type HelloRequest struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
|
||||
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*HelloRequest) ProtoMessage() {}
|
||||
func (*HelloRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
|
||||
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*HelloRequest) ProtoMessage() {}
|
||||
func (*HelloRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_helloworld_71e208cbdc16936b, []int{0}
|
||||
}
|
||||
func (m *HelloRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_HelloRequest.Unmarshal(m, b)
|
||||
}
|
||||
func (m *HelloRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_HelloRequest.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *HelloRequest) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_HelloRequest.Merge(dst, src)
|
||||
}
|
||||
func (m *HelloRequest) XXX_Size() int {
|
||||
return xxx_messageInfo_HelloRequest.Size(m)
|
||||
}
|
||||
func (m *HelloRequest) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_HelloRequest.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_HelloRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *HelloRequest) GetName() string {
|
||||
if m != nil {
|
||||
@ -52,13 +64,35 @@ func (m *HelloRequest) GetName() string {
|
||||
|
||||
// The response message containing the greetings
|
||||
type HelloReply struct {
|
||||
Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *HelloReply) Reset() { *m = HelloReply{} }
|
||||
func (m *HelloReply) String() string { return proto.CompactTextString(m) }
|
||||
func (*HelloReply) ProtoMessage() {}
|
||||
func (*HelloReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
func (m *HelloReply) Reset() { *m = HelloReply{} }
|
||||
func (m *HelloReply) String() string { return proto.CompactTextString(m) }
|
||||
func (*HelloReply) ProtoMessage() {}
|
||||
func (*HelloReply) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_helloworld_71e208cbdc16936b, []int{1}
|
||||
}
|
||||
func (m *HelloReply) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_HelloReply.Unmarshal(m, b)
|
||||
}
|
||||
func (m *HelloReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_HelloReply.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *HelloReply) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_HelloReply.Merge(dst, src)
|
||||
}
|
||||
func (m *HelloReply) XXX_Size() int {
|
||||
return xxx_messageInfo_HelloReply.Size(m)
|
||||
}
|
||||
func (m *HelloReply) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_HelloReply.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_HelloReply proto.InternalMessageInfo
|
||||
|
||||
func (m *HelloReply) GetMessage() string {
|
||||
if m != nil {
|
||||
@ -80,8 +114,9 @@ var _ grpc.ClientConn
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for Greeter service
|
||||
|
||||
// GreeterClient is the client API for Greeter service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type GreeterClient interface {
|
||||
// Sends a greeting
|
||||
SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
|
||||
@ -97,15 +132,14 @@ func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {
|
||||
|
||||
func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
|
||||
out := new(HelloReply)
|
||||
err := grpc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, c.cc, opts...)
|
||||
err := c.cc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Greeter service
|
||||
|
||||
// GreeterServer is the server API for Greeter service.
|
||||
type GreeterServer interface {
|
||||
// Sends a greeting
|
||||
SayHello(context.Context, *HelloRequest) (*HelloReply, error)
|
||||
@ -146,9 +180,9 @@ var _Greeter_serviceDesc = grpc.ServiceDesc{
|
||||
Metadata: "helloworld.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("helloworld.proto", fileDescriptor0) }
|
||||
func init() { proto.RegisterFile("helloworld.proto", fileDescriptor_helloworld_71e208cbdc16936b) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
var fileDescriptor_helloworld_71e208cbdc16936b = []byte{
|
||||
// 175 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xc8, 0x48, 0xcd, 0xc9,
|
||||
0xc9, 0x2f, 0xcf, 0x2f, 0xca, 0x49, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x42, 0x88,
|
||||
|
5
vendor/google.golang.org/grpc/examples/helloworld/mock_helloworld/hw_mock_test.go
generated
vendored
5
vendor/google.golang.org/grpc/examples/helloworld/mock_helloworld/hw_mock_test.go
generated
vendored
@ -21,6 +21,7 @@ package mock_helloworld_test
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/proto"
|
||||
@ -59,7 +60,9 @@ func TestSayHello(t *testing.T) {
|
||||
}
|
||||
|
||||
func testSayHello(t *testing.T, client helloworld.GreeterClient) {
|
||||
r, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "unit_test"})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "unit_test"})
|
||||
if err != nil || r.Message != "Mocked Interface" {
|
||||
t.Errorf("mocking failed")
|
||||
}
|
||||
|
72
vendor/google.golang.org/grpc/examples/oauth/client/main.go
generated
vendored
Normal file
72
vendor/google.golang.org/grpc/examples/oauth/client/main.go
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2018 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// The client demonstrates how to supply an OAuth2 token for every RPC.
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/oauth"
|
||||
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
)
|
||||
|
||||
func main() {
|
||||
perRPC := oauth.NewOauthAccess(fetchToken())
|
||||
opts := []grpc.DialOption{
|
||||
// In addition to the following grpc.DialOption, callers may also use
|
||||
// the grpc.CallOption grpc.PerRPCCredentials with the RPC invocation
|
||||
// itself.
|
||||
// See: https://godoc.org/google.golang.org/grpc#PerRPCCredentials
|
||||
grpc.WithPerRPCCredentials(perRPC),
|
||||
// oauth.NewOauthAccess requires the configuration of transport
|
||||
// credentials.
|
||||
grpc.WithTransportCredentials(
|
||||
credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}),
|
||||
),
|
||||
}
|
||||
conn, err := grpc.Dial(":8080", opts...)
|
||||
if err != nil {
|
||||
log.Fatalf("did not connect: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
c := pb.NewGreeterClient(conn)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "authenticated-client"})
|
||||
if err != nil {
|
||||
log.Fatalf("could not greet: %v", err)
|
||||
}
|
||||
log.Printf("Greeting: %s", r.Message)
|
||||
}
|
||||
|
||||
// fetchToken simulates a token lookup and omits the details of proper token
|
||||
// acquisition. For examples of how to acquire an OAuth2 token, see:
|
||||
// https://godoc.org/golang.org/x/oauth2
|
||||
func fetchToken() *oauth2.Token {
|
||||
return &oauth2.Token{
|
||||
AccessToken: "some-secret-token",
|
||||
}
|
||||
}
|
108
vendor/google.golang.org/grpc/examples/oauth/server/main.go
generated
vendored
Normal file
108
vendor/google.golang.org/grpc/examples/oauth/server/main.go
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2018 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// The server demonstrates how to consume and validate OAuth2 tokens provided by
|
||||
// clients for each RPC.
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials"
|
||||
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/grpc/testdata"
|
||||
)
|
||||
|
||||
var (
|
||||
errMissingMetadata = status.Errorf(codes.InvalidArgument, "missing metadata")
|
||||
errInvalidToken = status.Errorf(codes.Unauthenticated, "invalid token")
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("server starting on port 8080...")
|
||||
cert, err := tls.LoadX509KeyPair(testdata.Path("server1.pem"), testdata.Path("server1.key"))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load key pair: %s", err)
|
||||
}
|
||||
opts := []grpc.ServerOption{
|
||||
// The following grpc.ServerOption adds an interceptor for all unary
|
||||
// RPCs. To configure an interceptor for streaming RPCs, see:
|
||||
// https://godoc.org/google.golang.org/grpc#StreamInterceptor
|
||||
grpc.UnaryInterceptor(ensureValidToken),
|
||||
// Enable TLS for all incoming connections.
|
||||
grpc.Creds(credentials.NewServerTLSFromCert(&cert)),
|
||||
}
|
||||
s := grpc.NewServer(opts...)
|
||||
pb.RegisterGreeterServer(s, &server{})
|
||||
lis, err := net.Listen("tcp", ":8080")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
if err := s.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// server is used to implement helloworld.GreeterServer.
|
||||
type server struct{}
|
||||
|
||||
// SayHello implements helloworld.GreeterServer
|
||||
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
||||
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
|
||||
}
|
||||
|
||||
// valid validates the authorization.
|
||||
func valid(authorization []string) bool {
|
||||
if len(authorization) < 1 {
|
||||
return false
|
||||
}
|
||||
token := strings.TrimPrefix(authorization[0], "Bearer ")
|
||||
// Perform the token validation here. For the sake of this example, the code
|
||||
// here forgoes any of the usual OAuth2 token validation and instead checks
|
||||
// for a token matching an arbitrary string.
|
||||
if token != "some-secret-token" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ensureValidToken ensures a valid token exists within a request's metadata. If
|
||||
// the token is missing or invalid, the interceptor blocks execution of the
|
||||
// handler and returns an error. Otherwise, the interceptor invokes the unary
|
||||
// handler.
|
||||
func ensureValidToken(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return nil, errMissingMetadata
|
||||
}
|
||||
// The keys within metadata.MD are normalized to lowercase.
|
||||
// See: https://godoc.org/google.golang.org/grpc/metadata#New
|
||||
if !valid(md["authorization"]) {
|
||||
return nil, errInvalidToken
|
||||
}
|
||||
// Continue execution of handler after ensuring a valid token.
|
||||
return handler(ctx, req)
|
||||
}
|
16
vendor/google.golang.org/grpc/examples/route_guide/client/client.go
generated
vendored
16
vendor/google.golang.org/grpc/examples/route_guide/client/client.go
generated
vendored
@ -46,7 +46,9 @@ var (
|
||||
// printFeature gets the feature for the given point.
|
||||
func printFeature(client pb.RouteGuideClient, point *pb.Point) {
|
||||
log.Printf("Getting feature for point (%d, %d)", point.Latitude, point.Longitude)
|
||||
feature, err := client.GetFeature(context.Background(), point)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
feature, err := client.GetFeature(ctx, point)
|
||||
if err != nil {
|
||||
log.Fatalf("%v.GetFeatures(_) = _, %v: ", client, err)
|
||||
}
|
||||
@ -56,7 +58,9 @@ func printFeature(client pb.RouteGuideClient, point *pb.Point) {
|
||||
// printFeatures lists all the features within the given bounding Rectangle.
|
||||
func printFeatures(client pb.RouteGuideClient, rect *pb.Rectangle) {
|
||||
log.Printf("Looking for features within %v", rect)
|
||||
stream, err := client.ListFeatures(context.Background(), rect)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.ListFeatures(ctx, rect)
|
||||
if err != nil {
|
||||
log.Fatalf("%v.ListFeatures(_) = _, %v", client, err)
|
||||
}
|
||||
@ -82,7 +86,9 @@ func runRecordRoute(client pb.RouteGuideClient) {
|
||||
points = append(points, randomPoint(r))
|
||||
}
|
||||
log.Printf("Traversing %d points.", len(points))
|
||||
stream, err := client.RecordRoute(context.Background())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.RecordRoute(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("%v.RecordRoute(_) = _, %v", client, err)
|
||||
}
|
||||
@ -108,7 +114,9 @@ func runRouteChat(client pb.RouteGuideClient) {
|
||||
{Location: &pb.Point{Latitude: 0, Longitude: 2}, Message: "Fifth message"},
|
||||
{Location: &pb.Point{Latitude: 0, Longitude: 3}, Message: "Sixth message"},
|
||||
}
|
||||
stream, err := client.RouteChat(context.Background())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.RouteChat(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("%v.RouteChat(_) = _, %v", client, err)
|
||||
}
|
||||
|
5
vendor/google.golang.org/grpc/examples/route_guide/mock_routeguide/rg_mock_test.go
generated
vendored
5
vendor/google.golang.org/grpc/examples/route_guide/mock_routeguide/rg_mock_test.go
generated
vendored
@ -21,6 +21,7 @@ package mock_routeguide_test
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/proto"
|
||||
@ -59,7 +60,9 @@ func TestRouteChat(t *testing.T) {
|
||||
}
|
||||
|
||||
func testRouteChat(client rgpb.RouteGuideClient) error {
|
||||
stream, err := client.RouteChat(context.Background())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.RouteChat(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
207
vendor/google.golang.org/grpc/examples/route_guide/routeguide/route_guide.pb.go
generated
vendored
207
vendor/google.golang.org/grpc/examples/route_guide/routeguide/route_guide.pb.go
generated
vendored
@ -1,19 +1,6 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: route_guide.proto
|
||||
|
||||
/*
|
||||
Package routeguide is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
route_guide.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Point
|
||||
Rectangle
|
||||
Feature
|
||||
RouteNote
|
||||
RouteSummary
|
||||
*/
|
||||
package routeguide
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
@ -41,14 +28,36 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in
|
||||
// the range +/- 180 degrees (inclusive).
|
||||
type Point struct {
|
||||
Latitude int32 `protobuf:"varint,1,opt,name=latitude" json:"latitude,omitempty"`
|
||||
Longitude int32 `protobuf:"varint,2,opt,name=longitude" json:"longitude,omitempty"`
|
||||
Latitude int32 `protobuf:"varint,1,opt,name=latitude,proto3" json:"latitude,omitempty"`
|
||||
Longitude int32 `protobuf:"varint,2,opt,name=longitude,proto3" json:"longitude,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Point) Reset() { *m = Point{} }
|
||||
func (m *Point) String() string { return proto.CompactTextString(m) }
|
||||
func (*Point) ProtoMessage() {}
|
||||
func (*Point) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
func (m *Point) Reset() { *m = Point{} }
|
||||
func (m *Point) String() string { return proto.CompactTextString(m) }
|
||||
func (*Point) ProtoMessage() {}
|
||||
func (*Point) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_route_guide_dc79de2de4c66c19, []int{0}
|
||||
}
|
||||
func (m *Point) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Point.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Point) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Point.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *Point) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Point.Merge(dst, src)
|
||||
}
|
||||
func (m *Point) XXX_Size() int {
|
||||
return xxx_messageInfo_Point.Size(m)
|
||||
}
|
||||
func (m *Point) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Point.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Point proto.InternalMessageInfo
|
||||
|
||||
func (m *Point) GetLatitude() int32 {
|
||||
if m != nil {
|
||||
@ -68,15 +77,37 @@ func (m *Point) GetLongitude() int32 {
|
||||
// points "lo" and "hi".
|
||||
type Rectangle struct {
|
||||
// One corner of the rectangle.
|
||||
Lo *Point `protobuf:"bytes,1,opt,name=lo" json:"lo,omitempty"`
|
||||
Lo *Point `protobuf:"bytes,1,opt,name=lo,proto3" json:"lo,omitempty"`
|
||||
// The other corner of the rectangle.
|
||||
Hi *Point `protobuf:"bytes,2,opt,name=hi" json:"hi,omitempty"`
|
||||
Hi *Point `protobuf:"bytes,2,opt,name=hi,proto3" json:"hi,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Rectangle) Reset() { *m = Rectangle{} }
|
||||
func (m *Rectangle) String() string { return proto.CompactTextString(m) }
|
||||
func (*Rectangle) ProtoMessage() {}
|
||||
func (*Rectangle) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
func (m *Rectangle) Reset() { *m = Rectangle{} }
|
||||
func (m *Rectangle) String() string { return proto.CompactTextString(m) }
|
||||
func (*Rectangle) ProtoMessage() {}
|
||||
func (*Rectangle) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_route_guide_dc79de2de4c66c19, []int{1}
|
||||
}
|
||||
func (m *Rectangle) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Rectangle.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Rectangle) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Rectangle.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *Rectangle) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Rectangle.Merge(dst, src)
|
||||
}
|
||||
func (m *Rectangle) XXX_Size() int {
|
||||
return xxx_messageInfo_Rectangle.Size(m)
|
||||
}
|
||||
func (m *Rectangle) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Rectangle.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Rectangle proto.InternalMessageInfo
|
||||
|
||||
func (m *Rectangle) GetLo() *Point {
|
||||
if m != nil {
|
||||
@ -97,15 +128,37 @@ func (m *Rectangle) GetHi() *Point {
|
||||
// If a feature could not be named, the name is empty.
|
||||
type Feature struct {
|
||||
// The name of the feature.
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
// The point where the feature is detected.
|
||||
Location *Point `protobuf:"bytes,2,opt,name=location" json:"location,omitempty"`
|
||||
Location *Point `protobuf:"bytes,2,opt,name=location,proto3" json:"location,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Feature) Reset() { *m = Feature{} }
|
||||
func (m *Feature) String() string { return proto.CompactTextString(m) }
|
||||
func (*Feature) ProtoMessage() {}
|
||||
func (*Feature) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||
func (m *Feature) Reset() { *m = Feature{} }
|
||||
func (m *Feature) String() string { return proto.CompactTextString(m) }
|
||||
func (*Feature) ProtoMessage() {}
|
||||
func (*Feature) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_route_guide_dc79de2de4c66c19, []int{2}
|
||||
}
|
||||
func (m *Feature) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Feature.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Feature) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Feature.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *Feature) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Feature.Merge(dst, src)
|
||||
}
|
||||
func (m *Feature) XXX_Size() int {
|
||||
return xxx_messageInfo_Feature.Size(m)
|
||||
}
|
||||
func (m *Feature) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Feature.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Feature proto.InternalMessageInfo
|
||||
|
||||
func (m *Feature) GetName() string {
|
||||
if m != nil {
|
||||
@ -124,15 +177,37 @@ func (m *Feature) GetLocation() *Point {
|
||||
// A RouteNote is a message sent while at a given point.
|
||||
type RouteNote struct {
|
||||
// The location from which the message is sent.
|
||||
Location *Point `protobuf:"bytes,1,opt,name=location" json:"location,omitempty"`
|
||||
Location *Point `protobuf:"bytes,1,opt,name=location,proto3" json:"location,omitempty"`
|
||||
// The message to be sent.
|
||||
Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
|
||||
Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *RouteNote) Reset() { *m = RouteNote{} }
|
||||
func (m *RouteNote) String() string { return proto.CompactTextString(m) }
|
||||
func (*RouteNote) ProtoMessage() {}
|
||||
func (*RouteNote) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
|
||||
func (m *RouteNote) Reset() { *m = RouteNote{} }
|
||||
func (m *RouteNote) String() string { return proto.CompactTextString(m) }
|
||||
func (*RouteNote) ProtoMessage() {}
|
||||
func (*RouteNote) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_route_guide_dc79de2de4c66c19, []int{3}
|
||||
}
|
||||
func (m *RouteNote) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_RouteNote.Unmarshal(m, b)
|
||||
}
|
||||
func (m *RouteNote) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_RouteNote.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *RouteNote) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_RouteNote.Merge(dst, src)
|
||||
}
|
||||
func (m *RouteNote) XXX_Size() int {
|
||||
return xxx_messageInfo_RouteNote.Size(m)
|
||||
}
|
||||
func (m *RouteNote) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_RouteNote.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_RouteNote proto.InternalMessageInfo
|
||||
|
||||
func (m *RouteNote) GetLocation() *Point {
|
||||
if m != nil {
|
||||
@ -155,19 +230,41 @@ func (m *RouteNote) GetMessage() string {
|
||||
// the distance between each point.
|
||||
type RouteSummary struct {
|
||||
// The number of points received.
|
||||
PointCount int32 `protobuf:"varint,1,opt,name=point_count,json=pointCount" json:"point_count,omitempty"`
|
||||
PointCount int32 `protobuf:"varint,1,opt,name=point_count,json=pointCount,proto3" json:"point_count,omitempty"`
|
||||
// The number of known features passed while traversing the route.
|
||||
FeatureCount int32 `protobuf:"varint,2,opt,name=feature_count,json=featureCount" json:"feature_count,omitempty"`
|
||||
FeatureCount int32 `protobuf:"varint,2,opt,name=feature_count,json=featureCount,proto3" json:"feature_count,omitempty"`
|
||||
// The distance covered in metres.
|
||||
Distance int32 `protobuf:"varint,3,opt,name=distance" json:"distance,omitempty"`
|
||||
Distance int32 `protobuf:"varint,3,opt,name=distance,proto3" json:"distance,omitempty"`
|
||||
// The duration of the traversal in seconds.
|
||||
ElapsedTime int32 `protobuf:"varint,4,opt,name=elapsed_time,json=elapsedTime" json:"elapsed_time,omitempty"`
|
||||
ElapsedTime int32 `protobuf:"varint,4,opt,name=elapsed_time,json=elapsedTime,proto3" json:"elapsed_time,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *RouteSummary) Reset() { *m = RouteSummary{} }
|
||||
func (m *RouteSummary) String() string { return proto.CompactTextString(m) }
|
||||
func (*RouteSummary) ProtoMessage() {}
|
||||
func (*RouteSummary) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
|
||||
func (m *RouteSummary) Reset() { *m = RouteSummary{} }
|
||||
func (m *RouteSummary) String() string { return proto.CompactTextString(m) }
|
||||
func (*RouteSummary) ProtoMessage() {}
|
||||
func (*RouteSummary) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_route_guide_dc79de2de4c66c19, []int{4}
|
||||
}
|
||||
func (m *RouteSummary) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_RouteSummary.Unmarshal(m, b)
|
||||
}
|
||||
func (m *RouteSummary) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_RouteSummary.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (dst *RouteSummary) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_RouteSummary.Merge(dst, src)
|
||||
}
|
||||
func (m *RouteSummary) XXX_Size() int {
|
||||
return xxx_messageInfo_RouteSummary.Size(m)
|
||||
}
|
||||
func (m *RouteSummary) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_RouteSummary.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_RouteSummary proto.InternalMessageInfo
|
||||
|
||||
func (m *RouteSummary) GetPointCount() int32 {
|
||||
if m != nil {
|
||||
@ -213,8 +310,9 @@ var _ grpc.ClientConn
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for RouteGuide service
|
||||
|
||||
// RouteGuideClient is the client API for RouteGuide service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type RouteGuideClient interface {
|
||||
// A simple RPC.
|
||||
//
|
||||
@ -252,7 +350,7 @@ func NewRouteGuideClient(cc *grpc.ClientConn) RouteGuideClient {
|
||||
|
||||
func (c *routeGuideClient) GetFeature(ctx context.Context, in *Point, opts ...grpc.CallOption) (*Feature, error) {
|
||||
out := new(Feature)
|
||||
err := grpc.Invoke(ctx, "/routeguide.RouteGuide/GetFeature", in, out, c.cc, opts...)
|
||||
err := c.cc.Invoke(ctx, "/routeguide.RouteGuide/GetFeature", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -260,7 +358,7 @@ func (c *routeGuideClient) GetFeature(ctx context.Context, in *Point, opts ...gr
|
||||
}
|
||||
|
||||
func (c *routeGuideClient) ListFeatures(ctx context.Context, in *Rectangle, opts ...grpc.CallOption) (RouteGuide_ListFeaturesClient, error) {
|
||||
stream, err := grpc.NewClientStream(ctx, &_RouteGuide_serviceDesc.Streams[0], c.cc, "/routeguide.RouteGuide/ListFeatures", opts...)
|
||||
stream, err := c.cc.NewStream(ctx, &_RouteGuide_serviceDesc.Streams[0], "/routeguide.RouteGuide/ListFeatures", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -292,7 +390,7 @@ func (x *routeGuideListFeaturesClient) Recv() (*Feature, error) {
|
||||
}
|
||||
|
||||
func (c *routeGuideClient) RecordRoute(ctx context.Context, opts ...grpc.CallOption) (RouteGuide_RecordRouteClient, error) {
|
||||
stream, err := grpc.NewClientStream(ctx, &_RouteGuide_serviceDesc.Streams[1], c.cc, "/routeguide.RouteGuide/RecordRoute", opts...)
|
||||
stream, err := c.cc.NewStream(ctx, &_RouteGuide_serviceDesc.Streams[1], "/routeguide.RouteGuide/RecordRoute", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -326,7 +424,7 @@ func (x *routeGuideRecordRouteClient) CloseAndRecv() (*RouteSummary, error) {
|
||||
}
|
||||
|
||||
func (c *routeGuideClient) RouteChat(ctx context.Context, opts ...grpc.CallOption) (RouteGuide_RouteChatClient, error) {
|
||||
stream, err := grpc.NewClientStream(ctx, &_RouteGuide_serviceDesc.Streams[2], c.cc, "/routeguide.RouteGuide/RouteChat", opts...)
|
||||
stream, err := c.cc.NewStream(ctx, &_RouteGuide_serviceDesc.Streams[2], "/routeguide.RouteGuide/RouteChat", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -356,8 +454,7 @@ func (x *routeGuideRouteChatClient) Recv() (*RouteNote, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Server API for RouteGuide service
|
||||
|
||||
// RouteGuideServer is the server API for RouteGuide service.
|
||||
type RouteGuideServer interface {
|
||||
// A simple RPC.
|
||||
//
|
||||
@ -510,9 +607,9 @@ var _RouteGuide_serviceDesc = grpc.ServiceDesc{
|
||||
Metadata: "route_guide.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("route_guide.proto", fileDescriptor0) }
|
||||
func init() { proto.RegisterFile("route_guide.proto", fileDescriptor_route_guide_dc79de2de4c66c19) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
var fileDescriptor_route_guide_dc79de2de4c66c19 = []byte{
|
||||
// 404 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x53, 0xdd, 0xca, 0xd3, 0x40,
|
||||
0x10, 0xfd, 0x36, 0x7e, 0x9f, 0x6d, 0x26, 0x11, 0xe9, 0x88, 0x10, 0xa2, 0xa0, 0x8d, 0x37, 0xbd,
|
||||
|
24
vendor/google.golang.org/grpc/examples/route_guide/server/server.go
generated
vendored
24
vendor/google.golang.org/grpc/examples/route_guide/server/server.go
generated
vendored
@ -167,22 +167,20 @@ func toRadians(num float64) float64 {
|
||||
}
|
||||
|
||||
// calcDistance calculates the distance between two points using the "haversine" formula.
|
||||
// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
|
||||
// The formula is based on http://mathforum.org/library/drmath/view/51879.html.
|
||||
func calcDistance(p1 *pb.Point, p2 *pb.Point) int32 {
|
||||
const CordFactor float64 = 1e7
|
||||
const R float64 = float64(6371000) // metres
|
||||
lat1 := float64(p1.Latitude) / CordFactor
|
||||
lat2 := float64(p2.Latitude) / CordFactor
|
||||
lng1 := float64(p1.Longitude) / CordFactor
|
||||
lng2 := float64(p2.Longitude) / CordFactor
|
||||
φ1 := toRadians(lat1)
|
||||
φ2 := toRadians(lat2)
|
||||
Δφ := toRadians(lat2 - lat1)
|
||||
Δλ := toRadians(lng2 - lng1)
|
||||
const R float64 = float64(6371000) // earth radius in metres
|
||||
lat1 := toRadians(float64(p1.Latitude) / CordFactor)
|
||||
lat2 := toRadians(float64(p2.Latitude) / CordFactor)
|
||||
lng1 := toRadians(float64(p1.Longitude) / CordFactor)
|
||||
lng2 := toRadians(float64(p2.Longitude) / CordFactor)
|
||||
dlat := lat2 - lat1
|
||||
dlng := lng2 - lng1
|
||||
|
||||
a := math.Sin(Δφ/2)*math.Sin(Δφ/2) +
|
||||
math.Cos(φ1)*math.Cos(φ2)*
|
||||
math.Sin(Δλ/2)*math.Sin(Δλ/2)
|
||||
a := math.Sin(dlat/2)*math.Sin(dlat/2) +
|
||||
math.Cos(lat1)*math.Cos(lat2)*
|
||||
math.Sin(dlng/2)*math.Sin(dlng/2)
|
||||
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
||||
|
||||
distance := R * c
|
||||
|
62
vendor/google.golang.org/grpc/examples/rpc_errors/client/main.go
generated
vendored
Normal file
62
vendor/google.golang.org/grpc/examples/rpc_errors/client/main.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2018 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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 (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc"
|
||||
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Set up a connection to the server.
|
||||
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Fatalf("did not connect: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if e := conn.Close(); e != nil {
|
||||
log.Printf("failed to close connection: %s", e)
|
||||
}
|
||||
}()
|
||||
c := pb.NewGreeterClient(conn)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world"})
|
||||
if err != nil {
|
||||
s := status.Convert(err)
|
||||
for _, d := range s.Details() {
|
||||
switch info := d.(type) {
|
||||
case *epb.QuotaFailure:
|
||||
log.Printf("Quota failure: %s", info)
|
||||
default:
|
||||
log.Printf("Unexpected type: %s", info)
|
||||
}
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Greeting: %s", r.Message)
|
||||
}
|
80
vendor/google.golang.org/grpc/examples/rpc_errors/server/main.go
generated
vendored
Normal file
80
vendor/google.golang.org/grpc/examples/rpc_errors/server/main.go
generated
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2018 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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 (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
const (
|
||||
port = ":50051"
|
||||
)
|
||||
|
||||
// server is used to implement helloworld.GreeterServer.
|
||||
type server struct {
|
||||
mu sync.Mutex
|
||||
count map[string]int
|
||||
}
|
||||
|
||||
// SayHello implements helloworld.GreeterServer
|
||||
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
// Track the number of times the user has been greeted.
|
||||
s.count[in.Name]++
|
||||
if s.count[in.Name] > 1 {
|
||||
st := status.New(codes.ResourceExhausted, "Request limit exceeded.")
|
||||
ds, err := st.WithDetails(
|
||||
&epb.QuotaFailure{
|
||||
Violations: []*epb.QuotaFailure_Violation{{
|
||||
Subject: fmt.Sprintf("name:%s", in.Name),
|
||||
Description: "Limit one greeting per person",
|
||||
}},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, st.Err()
|
||||
}
|
||||
return nil, ds.Err()
|
||||
}
|
||||
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Printf("server starting on port %s...", port)
|
||||
lis, err := net.Listen("tcp", port)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
s := grpc.NewServer()
|
||||
pb.RegisterGreeterServer(s, &server{count: make(map[string]int)})
|
||||
if err := s.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user