├── AUTHORS ├── security ├── advancedtls │ ├── testdata │ │ ├── crl │ │ │ ├── 0b35a562.r0 │ │ │ ├── 0b35a562.r1 │ │ │ ├── 1ab871c8.r0 │ │ │ ├── 71eac5a2.r0 │ │ │ ├── 7a1799af.r0 │ │ │ ├── 8828a7e6.r0 │ │ │ ├── deee447d.r0 │ │ │ ├── 2f11f022.r0 │ │ │ ├── 1.crl │ │ │ ├── 2.crl │ │ │ ├── 4.crl │ │ │ ├── 5.crl │ │ │ ├── 3.crl │ │ │ ├── 6.crl │ │ │ └── README.md │ │ ├── localhost-openssl.cnf │ │ ├── testdata.go │ │ ├── server_cert_3.pem │ │ └── server_key_3.pem │ ├── go.mod │ └── examples │ │ ├── go.mod │ │ └── credential_reloading_from_files │ │ └── README.md └── authorization │ └── go.mod ├── test ├── kokoro │ ├── README.md │ ├── xds_v3.sh │ ├── xds.cfg │ ├── xds_v3.cfg │ ├── xds_k8s_lb.cfg │ ├── xds_url_map.cfg │ ├── psm-security.cfg │ └── xds.sh ├── tools │ ├── go.mod │ ├── tools_vet.go │ └── tools.go ├── race.go ├── logging.go ├── timeouts.go ├── codec_perf │ └── perf.proto └── parse_config.go ├── GOVERNANCE.md ├── cmd └── protoc-gen-go-grpc │ ├── go.mod │ ├── go.sum │ └── README.md ├── CODE-OF-CONDUCT.md ├── testdata ├── README.md ├── x509 │ ├── README.md │ └── openssl.cnf ├── ca.pem ├── testdata.go ├── server1.pem └── server1.key ├── xds ├── internal │ ├── test │ │ └── e2e │ │ │ ├── run.sh │ │ │ ├── README.md │ │ │ └── e2e_utils.go │ ├── balancer │ │ ├── priority │ │ │ ├── utils.go │ │ │ ├── logging.go │ │ │ └── utils_test.go │ │ ├── cdsbalancer │ │ │ └── logging.go │ │ ├── ringhash │ │ │ ├── logging.go │ │ │ ├── util.go │ │ │ └── config.go │ │ ├── clusterimpl │ │ │ └── logging.go │ │ ├── clusterresolver │ │ │ └── logging.go │ │ ├── balancer.go │ │ └── clustermanager │ │ │ └── config.go │ ├── xdsclient │ │ ├── bootstrap │ │ │ ├── logging.go │ │ │ └── template.go │ │ ├── load │ │ │ └── reporter.go │ │ ├── xdsclient_test.go │ │ ├── logging.go │ │ ├── attributes.go │ │ ├── xdsresource │ │ │ └── test_utils_test.go │ │ └── clientimpl_loadreport.go │ ├── resolver │ │ └── logging.go │ └── testutils │ │ └── testutils.go └── bootstrap │ └── bootstrap_test.go ├── .github ├── ISSUE_TEMPLATE │ ├── question.md │ ├── feature.md │ └── bug.md ├── workflows │ ├── lock.yml │ ├── codeql-analysis.yml │ └── stale.yml └── mergeable.yml ├── SECURITY.md ├── examples ├── data │ ├── x509 │ │ ├── README.md │ │ └── openssl.cnf │ └── data.go ├── features │ ├── multiplex │ │ └── README.md │ ├── cancellation │ │ ├── README.md │ │ └── server │ │ │ └── main.go │ ├── compression │ │ ├── README.md │ │ └── server │ │ │ └── main.go │ ├── metadata │ │ └── README.md │ ├── keepalive │ │ └── README.md │ ├── wait_for_ready │ │ └── README.md │ ├── reflection │ │ └── README.md │ ├── errors │ │ └── README.md │ ├── unix_abstract │ │ └── README.md │ ├── debugging │ │ └── README.md │ ├── xds │ │ └── README.md │ ├── name_resolving │ │ ├── README.md │ │ └── server │ │ │ └── main.go │ ├── proto │ │ └── echo │ │ │ └── echo.proto │ └── encryption │ │ └── ALTS │ │ └── server │ │ └── main.go ├── go.mod ├── README.md ├── route_guide │ └── README.md └── helloworld │ ├── helloworld │ └── helloworld.proto │ ├── greeter_server │ └── main.go │ └── greeter_client │ └── main.go ├── reflection ├── grpc_testing_not_regenerate │ ├── README.md │ ├── dynamic.proto │ └── testv3.proto ├── README.md └── grpc_testing │ ├── proto2.proto │ ├── proto2_ext2.proto │ ├── proto2_ext.proto │ └── test.proto ├── NOTICE.txt ├── gcp └── observability │ └── go.mod ├── Documentation ├── proxy.md ├── versioning.md └── log_levels.md ├── codegen.sh ├── version.go ├── balancer ├── rls │ └── internal │ │ └── test │ │ └── e2e │ │ └── e2e.go └── weightedtarget │ ├── logging.go │ └── weightedtarget_config.go ├── internal ├── grpcutil │ ├── grpcutil.go │ ├── regex.go │ ├── metadata.go │ └── encode_duration_test.go ├── googlecloud │ ├── manufacturer.go │ ├── manufacturer_linux.go │ ├── manufacturer_windows.go │ └── googlecloud_test.go ├── channelz │ ├── util_nonlinux.go │ ├── util_linux.go │ ├── types_nonlinux.go │ └── types_linux.go ├── testutils │ ├── local_listener.go │ ├── marshal_any.go │ ├── status_equal.go │ └── xds │ │ └── e2e │ │ ├── bootstrap.go │ │ └── logging.go ├── profiling │ └── goid_regular.go ├── wrr │ ├── edf_test.go │ └── wrr.go ├── envconfig │ └── envconfig.go ├── balancergroup │ └── balancerstateaggregator.go ├── balancerload │ └── load.go ├── credentials │ ├── util.go │ ├── util_test.go │ ├── syscallconn_test.go │ └── credentials.go ├── grpctest │ ├── example_test.go │ └── grpctest_test.go ├── xds_handshake_cluster.go ├── transport │ └── networktype │ │ └── networktype.go ├── binarylog │ └── binarylog_testutil.go ├── resolver │ └── passthrough │ │ └── passthrough.go ├── grpcsync │ └── event.go └── grpcrand │ └── grpcrand.go ├── health ├── logging.go └── server_test.go ├── doc.go ├── grpc_test.go ├── resolver ├── resolver_test.go ├── passthrough │ └── passthrough.go └── dns │ └── dns_resolver.go ├── channelz ├── service │ ├── func_nonlinux.go │ ├── util_sktopt_amd64_test.go │ └── util_sktopt_386_test.go └── channelz.go ├── codec_test.go ├── admin ├── admin_test.go └── test │ └── admin_test.go ├── Makefile ├── go.mod ├── MAINTAINERS.md ├── serviceconfig └── serviceconfig.go ├── trace_test.go ├── profiling ├── profiling.go └── cmd │ └── main.go ├── interop └── xds │ ├── client │ └── Dockerfile │ └── server │ └── Dockerfile ├── credentials ├── alts │ └── internal │ │ └── conn │ │ └── counter.go └── oauth │ └── oauth_test.go ├── peer └── peer.go ├── codes └── code_string.go ├── encoding └── proto │ └── proto.go ├── stress └── grpc_testing │ └── metrics.proto └── codec.go /AUTHORS: -------------------------------------------------------------------------------- 1 | Google Inc. 2 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/0b35a562.r0: -------------------------------------------------------------------------------- 1 | 5.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/0b35a562.r1: -------------------------------------------------------------------------------- 1 | 1.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/1ab871c8.r0: -------------------------------------------------------------------------------- 1 | 2.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/71eac5a2.r0: -------------------------------------------------------------------------------- 1 | 4.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/7a1799af.r0: -------------------------------------------------------------------------------- 1 | 3.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/8828a7e6.r0: -------------------------------------------------------------------------------- 1 | 6.crl -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/deee447d.r0: -------------------------------------------------------------------------------- 1 | 5.crl -------------------------------------------------------------------------------- /test/kokoro/README.md: -------------------------------------------------------------------------------- 1 | The scripts in this directory are intended to be run by Kokoro CI jobs. 2 | -------------------------------------------------------------------------------- /test/kokoro/xds_v3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | XDS_V3_OPT="--xds_v3_support" `dirname $0`/xds.sh 4 | -------------------------------------------------------------------------------- /GOVERNANCE.md: -------------------------------------------------------------------------------- 1 | This repository is governed by the gRPC organization's [governance rules](https://github.com/grpc/grpc-community/blob/master/governance.md). 2 | -------------------------------------------------------------------------------- /cmd/protoc-gen-go-grpc/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/cmd/protoc-gen-go-grpc 2 | 3 | go 1.9 4 | 5 | require google.golang.org/protobuf v1.27.1 6 | -------------------------------------------------------------------------------- /CODE-OF-CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Community Code of Conduct 2 | 3 | gRPC follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md). 4 | -------------------------------------------------------------------------------- /testdata/README.md: -------------------------------------------------------------------------------- 1 | This directory contains x509 certificates used in cloud-to-prod interop tests. 2 | For tests within gRPC-Go repo, please use the files in testsdata/x509 3 | directory. 4 | -------------------------------------------------------------------------------- /xds/internal/test/e2e/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir binaries 4 | go build -o ./binaries/client ../../../../interop/xds/client/ 5 | go build -o ./binaries/server ../../../../interop/xds/server/ 6 | go test . -v 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Question 3 | about: Ask a question about gRPC-Go 4 | labels: 'Type: Question' 5 | 6 | --- 7 | 8 | Please see the FAQ in our main README.md before submitting your issue. 9 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | For information on gRPC Security Policy and reporting potentional security issues, please see [gRPC CVE Process](https://github.com/grpc/proposal/blob/master/P4-grpc-cve-process.md). 4 | -------------------------------------------------------------------------------- /examples/data/x509/README.md: -------------------------------------------------------------------------------- 1 | This directory contains x509 certificates and associated private keys used in 2 | examples. 3 | 4 | How were these test certs/keys generated ? 5 | ------------------------------------------ 6 | Run `./create.sh` 7 | -------------------------------------------------------------------------------- /testdata/x509/README.md: -------------------------------------------------------------------------------- 1 | This directory contains x509 certificates and associated private keys used in 2 | gRPC-Go tests. 3 | 4 | How were these test certs/keys generated ? 5 | ------------------------------------------ 6 | Run `./create.sh` 7 | -------------------------------------------------------------------------------- /examples/features/multiplex/README.md: -------------------------------------------------------------------------------- 1 | # Multiplex 2 | 3 | A `grpc.ClientConn` can be shared by two stubs and two services can share a 4 | `grpc.Server`. This example illustrates how to perform both types of sharing. 5 | 6 | ``` 7 | go run server/main.go 8 | ``` 9 | 10 | ``` 11 | go run client/main.go 12 | ``` 13 | -------------------------------------------------------------------------------- /test/kokoro/xds.cfg: -------------------------------------------------------------------------------- 1 | # Config file for internal CI 2 | 3 | # Location of the continuous shell script in repository. 4 | build_file: "grpc-go/test/kokoro/xds.sh" 5 | timeout_mins: 360 6 | action { 7 | define_artifacts { 8 | regex: "**/*sponge_log.*" 9 | regex: "github/grpc/reports/**" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/kokoro/xds_v3.cfg: -------------------------------------------------------------------------------- 1 | # Config file for internal CI 2 | 3 | # Location of the continuous shell script in repository. 4 | build_file: "grpc-go/test/kokoro/xds_v3.sh" 5 | timeout_mins: 360 6 | action { 7 | define_artifacts { 8 | regex: "**/*sponge_log.*" 9 | regex: "github/grpc/reports/**" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /examples/features/cancellation/README.md: -------------------------------------------------------------------------------- 1 | # Cancellation 2 | 3 | This example shows how clients can cancel in-flight RPCs by canceling the 4 | context passed to the RPC call. The client will receive a status with code 5 | `Canceled` and the service handler's context will be canceled. 6 | 7 | ``` 8 | go run server/main.go 9 | ``` 10 | 11 | ``` 12 | go run client/main.go 13 | ``` 14 | -------------------------------------------------------------------------------- /test/kokoro/xds_k8s_lb.cfg: -------------------------------------------------------------------------------- 1 | # Config file for internal CI 2 | 3 | # Location of the continuous shell script in repository. 4 | build_file: "grpc-go/test/kokoro/xds_k8s_lb.sh" 5 | timeout_mins: 180 6 | 7 | action { 8 | define_artifacts { 9 | regex: "artifacts/**/*sponge_log.xml" 10 | regex: "artifacts/**/*sponge_log.log" 11 | strip_prefix: "artifacts" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/kokoro/xds_url_map.cfg: -------------------------------------------------------------------------------- 1 | # Config file for internal CI 2 | 3 | # Location of the continuous shell script in repository. 4 | build_file: "grpc-go/test/kokoro/xds_url_map.sh" 5 | timeout_mins: 60 6 | 7 | action { 8 | define_artifacts { 9 | regex: "artifacts/**/*sponge_log.xml" 10 | regex: "artifacts/**/*sponge_log.log" 11 | strip_prefix: "artifacts" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /examples/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/examples 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/golang/protobuf v1.5.2 7 | golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d 8 | google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98 9 | google.golang.org/grpc v1.36.0 10 | google.golang.org/protobuf v1.27.1 11 | ) 12 | 13 | replace google.golang.org/grpc => ../ 14 | -------------------------------------------------------------------------------- /test/kokoro/psm-security.cfg: -------------------------------------------------------------------------------- 1 | # Config file for internal CI 2 | 3 | # Location of the continuous shell script in repository. 4 | build_file: "grpc-go/test/kokoro/psm-security.sh" 5 | timeout_mins: 180 6 | 7 | action { 8 | define_artifacts { 9 | regex: "artifacts/**/*sponge_log.xml" 10 | regex: "artifacts/**/*sponge_log.log" 11 | strip_prefix: "artifacts" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Suggest an idea for gRPC-Go 4 | labels: 'Type: Feature' 5 | 6 | --- 7 | 8 | Please see the FAQ in our main README.md before submitting your issue. 9 | 10 | ### Use case(s) - what problem will this feature solve? 11 | 12 | ### Proposed Solution 13 | 14 | ### Alternatives Considered 15 | 16 | ### Additional Context 17 | -------------------------------------------------------------------------------- /test/tools/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/test/tools 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/client9/misspell v0.3.4 7 | github.com/golang/protobuf v1.4.1 8 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 9 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 10 | google.golang.org/protobuf v1.25.0 // indirect 11 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc 12 | ) 13 | -------------------------------------------------------------------------------- /examples/features/compression/README.md: -------------------------------------------------------------------------------- 1 | # Compression 2 | 3 | This example shows how clients can specify compression options when performing 4 | RPCs, and how to install support for compressors on the server. For more 5 | information, please see [our detailed 6 | documentation](../../../Documentation/compression.md). 7 | 8 | ``` 9 | go run server/main.go 10 | ``` 11 | 12 | ``` 13 | go run client/main.go 14 | ``` 15 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/2f11f022.r0: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIHnMFICAQEwDQYJKoZIhvcNAQEMBQAwDDEKMAgGAioDDAI6KRcNMDkxMTEwMjMw 3 | MDAwWhcNMDkxMTExMDAwMDAwWqASMBAwDgYDVR0jBAcwBYADAQIDMA0GCSqGSIb3 4 | DQEBDAUAA4GBAMl2sjOjtOQ+OCsRyjM0IvqTn7lmdGJMvpYAym367JBamJPCbYrL 5 | MifCjCA1ra7gG0MweZbpm4SG2YLakwi1/B+XhApQ5VVv5SwDn6Yy5zr9ePLEF7Iy 6 | sP86e9s5XfOusLTW+Spre8q1vi7pJrRvUxhJGuUuLoM6Uhvh65ViilDJ 7 | -----END X509 CRL----- 8 | -------------------------------------------------------------------------------- /security/authorization/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/security/authorization 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0 7 | github.com/google/cel-go v0.10.1 8 | github.com/google/go-cmp v0.5.5 9 | google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 10 | google.golang.org/grpc v1.40.0 11 | google.golang.org/protobuf v1.27.1 12 | ) 13 | -------------------------------------------------------------------------------- /examples/features/metadata/README.md: -------------------------------------------------------------------------------- 1 | # Metadata example 2 | 3 | This example shows how to set and read metadata in RPC headers and trailers. 4 | Please see 5 | [grpc-metadata.md](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md) 6 | for more information. 7 | 8 | ## Start the server 9 | 10 | ``` 11 | go run server/main.go 12 | ``` 13 | 14 | ## Run the client 15 | 16 | ``` 17 | go run client/main.go 18 | ``` 19 | -------------------------------------------------------------------------------- /reflection/grpc_testing_not_regenerate/README.md: -------------------------------------------------------------------------------- 1 | testv3.pb.go is generated with an older version of codegen, to test reflection behavior with `grpc.SupportPackageIsVersion3`. DO NOT REGENERATE! 2 | 3 | testv3.pb.go is manually edited to replace `"golang.org/x/net/context"` with `"context"`. 4 | 5 | dynamic.pb.go is generated with the latest protoc and manually edited to remove everything except the descriptor bytes var, which is renamed and exported. -------------------------------------------------------------------------------- /security/advancedtls/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/security/advancedtls 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/hashicorp/golang-lru v0.5.4 7 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 8 | google.golang.org/grpc v1.38.0 9 | google.golang.org/grpc/examples v0.0.0-20201112215255-90f1b3ee835b 10 | ) 11 | 12 | replace google.golang.org/grpc => ../../ 13 | 14 | replace google.golang.org/grpc/examples => ../../examples 15 | -------------------------------------------------------------------------------- /.github/workflows/lock.yml: -------------------------------------------------------------------------------- 1 | name: 'Lock Threads' 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: '22 1 * * *' 7 | 8 | permissions: 9 | issues: write 10 | pull-requests: write 11 | 12 | jobs: 13 | lock: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: dessant/lock-threads@v2 17 | with: 18 | github-token: ${{ github.token }} 19 | issue-lock-inactive-days: 180 20 | pr-lock-inactive-days: 180 21 | -------------------------------------------------------------------------------- /examples/features/keepalive/README.md: -------------------------------------------------------------------------------- 1 | # Keepalive 2 | 3 | This example illustrates how to set up client-side keepalive pings and 4 | server-side keepalive ping enforcement and connection idleness settings. For 5 | more details on these settings, see the [full 6 | documentation](https://github.com/grpc/grpc-go/tree/master/Documentation/keepalive.md). 7 | 8 | 9 | ``` 10 | go run server/main.go 11 | ``` 12 | 13 | ``` 14 | GODEBUG=http2debug=2 go run client/main.go 15 | ``` 16 | -------------------------------------------------------------------------------- /xds/internal/test/e2e/README.md: -------------------------------------------------------------------------------- 1 | Build client and server binaries. 2 | 3 | ```sh 4 | go build -o ./binaries/client ../../../../interop/xds/client/ 5 | go build -o ./binaries/server ../../../../interop/xds/server/ 6 | ``` 7 | 8 | Run the test 9 | 10 | ```sh 11 | go test . -v 12 | ``` 13 | 14 | The client/server paths are flags 15 | 16 | ```sh 17 | go test . -v -client=$HOME/grpc-java/interop-testing/build/install/grpc-interop-testing/bin/xds-test-client 18 | ``` 19 | Note that grpc logs are only turned on for Go. 20 | -------------------------------------------------------------------------------- /security/advancedtls/examples/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/security/advancedtls/examples 2 | 3 | go 1.15 4 | 5 | require ( 6 | google.golang.org/grpc v1.38.0 7 | google.golang.org/grpc/examples v0.0.0-20201112215255-90f1b3ee835b 8 | google.golang.org/grpc/security/advancedtls v0.0.0-20201112215255-90f1b3ee835b 9 | ) 10 | 11 | replace google.golang.org/grpc => ../../.. 12 | 13 | replace google.golang.org/grpc/examples => ../../../examples 14 | 15 | replace google.golang.org/grpc/security/advancedtls => ../ 16 | -------------------------------------------------------------------------------- /reflection/README.md: -------------------------------------------------------------------------------- 1 | # Reflection 2 | 3 | Package reflection implements server reflection service. 4 | 5 | The service implemented is defined in: https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto. 6 | 7 | To register server reflection on a gRPC server: 8 | ```go 9 | import "google.golang.org/grpc/reflection" 10 | 11 | s := grpc.NewServer() 12 | pb.RegisterYourOwnServer(s, &server{}) 13 | 14 | // Register reflection service on gRPC server. 15 | reflection.Register(s) 16 | 17 | s.Serve(lis) 18 | ``` 19 | -------------------------------------------------------------------------------- /examples/features/wait_for_ready/README.md: -------------------------------------------------------------------------------- 1 | # Wait for ready example 2 | 3 | This example shows how to enable "wait for ready" in RPC calls. 4 | 5 | This code starts a server with a 2 seconds delay. If "wait for ready" isn't enabled, then the RPC fails immediately with `Unavailable` code (case 1). If "wait for ready" is enabled, then the RPC waits for the server. If context dies before the server is available, then it fails with `DeadlineExceeded` (case 3). Otherwise it succeeds (case 2). 6 | 7 | ## Run the example 8 | 9 | ``` 10 | go run main.go 11 | ``` 12 | -------------------------------------------------------------------------------- /examples/features/reflection/README.md: -------------------------------------------------------------------------------- 1 | # Reflection 2 | 3 | This example shows how reflection can be registered on a gRPC server. 4 | 5 | See 6 | https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md 7 | for a tutorial. 8 | 9 | 10 | # Try it 11 | 12 | ```go 13 | go run server/main.go 14 | ``` 15 | 16 | There are multiple existing reflection clients. 17 | 18 | To use `gRPC CLI`, follow 19 | https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md#grpc-cli. 20 | 21 | To use `grpcurl`, see https://github.com/fullstorydev/grpcurl. 22 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/1.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBYDCCAQYCAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjUm9vdCBDQSAoMjAyMS0wMi0wMlQwNzozMDozNi0wODowMCkX 6 | DTIxMDIwMjE1MzAzNloXDTIxMDIwOTE1MzAzNlqgLzAtMB8GA1UdIwQYMBaAFPQN 7 | tnCIBcG4ReQgoVi0kPgTROseMAoGA1UdFAQDAgEAMAoGCCqGSM49BAMCA0gAMEUC 8 | IQDB9WEPBPHEo5xjCv8CT9okockJJnkLDOus6FypVLqj5QIgYw9/PYLwb41/Uc+4 9 | LLTAsfdDWh7xBJmqvVQglMoJOEc= 10 | -----END X509 CRL----- 11 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/2.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBYDCCAQYCAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjbm9kZSBDQSAoMjAyMS0wMi0wMlQwNzozMDozNi0wODowMCkX 6 | DTIxMDIwMjE1MzAzNloXDTIxMDIwOTE1MzAzNlqgLzAtMB8GA1UdIwQYMBaAFBjo 7 | V5Jnk/gp1k7fmWwkvTk/cF/IMAoGA1UdFAQDAgEAMAoGCCqGSM49BAMCA0gAMEUC 8 | IQDgjA1Vj/pNFtNRL0vFEdapmFoArHM2+rn4IiP8jYLsCAIgAj2KEHbbtJ3zl5XP 9 | WVW6ZyW7r3wIX+Bt3vLJWPrQtf8= 10 | -----END X509 CRL----- 11 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/4.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBYDCCAQYCAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjbm9kZSBDQSAoMjAyMS0wMi0wMlQwNzozMTo1NC0wODowMCkX 6 | DTIxMDIwMjE1MzE1NFoXDTIxMDIwOTE1MzE1NFqgLzAtMB8GA1UdIwQYMBaAFIVn 7 | 8tIFgZpIdhomgYJ2c5ULLzpSMAoGA1UdFAQDAgEAMAoGCCqGSM49BAMCA0gAMEUC 8 | ICupTvOqgAyRa1nn7+Pe/1vvlJPAQ8gUfTQsQ6XX3v6oAiEA08B2PsK6aTEwzjry 9 | pXqhlUNZFzgaXrVVQuEJbyJ1qoU= 10 | -----END X509 CRL----- 11 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/5.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBXzCCAQYCAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjUm9vdCBDQSAoMjAyMS0wMi0wMlQwNzozMjo1Ny0wODowMCkX 6 | DTIxMDIwMjE1MzI1N1oXDTIxMDIwOTE1MzI1N1qgLzAtMB8GA1UdIwQYMBaAFN+g 7 | xTAtSTlb5Qqvrbp4rZtsaNzqMAoGA1UdFAQDAgEAMAoGCCqGSM49BAMCA0cAMEQC 8 | IHrRKjieY7w7gxvpkJAdszPZBlaSSp/c9wILutBTy7SyAiAwhaHfgas89iRfaBs2 9 | EhGIeK39A+kSzqu6qEQBHpK36g== 10 | -----END X509 CRL----- 11 | -------------------------------------------------------------------------------- /examples/features/errors/README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | This example demonstrates the use of status details in grpc errors. 4 | 5 | # Run the sample code 6 | 7 | Run the server: 8 | 9 | ```sh 10 | $ go run ./server/main.go 11 | ``` 12 | Then run the client in another terminal: 13 | 14 | ```sh 15 | $ go run ./client/main.go 16 | ``` 17 | 18 | It should succeed and print the greeting it received from the server. 19 | Then run the client again: 20 | 21 | ```sh 22 | $ go run ./client/main.go 23 | ``` 24 | 25 | This time, it should fail by printing error status details that it received from the server. 26 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2014 gRPC authors. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /gcp/observability/go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc/gcp/observability 2 | 3 | go 1.14 4 | 5 | require ( 6 | cloud.google.com/go/logging v1.4.2 7 | contrib.go.opencensus.io/exporter/stackdriver v0.13.12 8 | github.com/golang/protobuf v1.5.2 9 | github.com/google/uuid v1.3.0 10 | go.opencensus.io v0.23.0 11 | golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 12 | google.golang.org/grpc v1.46.0 13 | google.golang.org/protobuf v1.27.1 14 | ) 15 | 16 | // TODO(lidiz) remove the following line when we have a release containing the 17 | // necessary internal binary logging changes 18 | replace google.golang.org/grpc => ../../ 19 | -------------------------------------------------------------------------------- /.github/mergeable.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | mergeable: 3 | - when: pull_request.* 4 | validate: 5 | - do: label 6 | must_include: 7 | regex: '^Type:' 8 | - do: description 9 | must_include: 10 | # Allow: 11 | # RELEASE NOTES: none (case insensitive) 12 | # 13 | # RELEASE NOTES: N/A (case insensitive) 14 | # 15 | # RELEASE NOTES: 16 | # * 17 | regex: '^RELEASE NOTES:\s*([Nn][Oo][Nn][Ee]|[Nn]/[Aa]|\n(\*|-)\s*.+)$' 18 | regex_flag: 'm' 19 | - do: milestone 20 | must_include: 21 | regex: 'Release$' 22 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/3.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBiDCCAS8CAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjUm9vdCBDQSAoMjAyMS0wMi0wMlQwNzozMTo1NC0wODowMCkX 6 | DTIxMDIwMjE1MzE1NFoXDTIxMDIwOTE1MzE1NFowJzAlAhQAroEYW855BRqTrlov 7 | 5cBCGvkutxcNMjEwMjAyMTUzMTU0WqAvMC0wHwYDVR0jBBgwFoAUeq/TQ959KbWk 8 | /um08jSTXogXpWUwCgYDVR0UBAMCAQEwCgYIKoZIzj0EAwIDRwAwRAIgaSOIhJDg 9 | wOLYlbXkmxW0cqy/AfOUNYbz5D/8/FfvhosCICftg7Vzlu0Nh83jikyjy+wtkiJt 10 | ZYNvGFQ3Sp2L3A9e 11 | -----END X509 CRL----- 12 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/6.crl: -------------------------------------------------------------------------------- 1 | -----BEGIN X509 CRL----- 2 | MIIBiDCCAS8CAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI 3 | EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH 4 | b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs 5 | bjEsMCoGA1UEAxMjbm9kZSBDQSAoMjAyMS0wMi0wMlQwNzozMjo1Ny0wODowMCkX 6 | DTIxMDIwMjE1MzI1N1oXDTIxMDIwOTE1MzI1N1owJzAlAhQAxSe/pGmyvzN7mxm5 7 | 6ZJTYUXYuhcNMjEwMjAyMTUzMjU3WqAvMC0wHwYDVR0jBBgwFoAUpZ30UJXB4lI9 8 | j2SzodCtRFckrRcwCgYDVR0UBAMCAQEwCgYIKoZIzj0EAwIDRwAwRAIgRg3u7t3b 9 | oyV5FhMuGGzWnfIwnKclpT8imnp8tEN253sCIFUY7DjiDohwu4Zup3bWs1OaZ3q3 10 | cm+j0H/oe8zzCAgp 11 | -----END X509 CRL----- 12 | -------------------------------------------------------------------------------- /Documentation/proxy.md: -------------------------------------------------------------------------------- 1 | # Proxy 2 | 3 | HTTP CONNECT proxies are supported by default in gRPC. The proxy address can be 4 | specified by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or 5 | the lowercase versions thereof). 6 | 7 | ## Custom proxy 8 | 9 | Currently, proxy support is implemented in the default dialer. It does one more 10 | handshake (a CONNECT handshake in the case of HTTP CONNECT proxy) on the 11 | connection before giving it to gRPC. 12 | 13 | If the default proxy doesn't work for you, replace the default dialer with your 14 | custom proxy dialer. This can be done using 15 | [`WithDialer`](https://godoc.org/google.golang.org/grpc#WithDialer). -------------------------------------------------------------------------------- /codegen.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # This script serves as an example to demonstrate how to generate the gRPC-Go 4 | # interface and the related messages from .proto file. 5 | # 6 | # It assumes the installation of i) Google proto buffer compiler at 7 | # https://github.com/google/protobuf (after v2.6.1) and ii) the Go codegen 8 | # plugin at https://github.com/golang/protobuf (after 2015-02-20). If you have 9 | # not, please install them first. 10 | # 11 | # We recommend running this script at $GOPATH/src. 12 | # 13 | # If this is not what you need, feel free to make your own scripts. Again, this 14 | # script is for demonstration purpose. 15 | # 16 | proto=$1 17 | protoc --go_out=plugins=grpc:. $proto 18 | -------------------------------------------------------------------------------- /cmd/protoc-gen-go-grpc/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 2 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 3 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 4 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 5 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 6 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 7 | google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= 8 | google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 9 | -------------------------------------------------------------------------------- /version.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpc 20 | 21 | // Version is the current grpc version. 22 | const Version = "1.49.0" 23 | -------------------------------------------------------------------------------- /test/race.go: -------------------------------------------------------------------------------- 1 | //go:build race 2 | // +build race 3 | 4 | /* 5 | * Copyright 2016 gRPC authors. 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package test 22 | 23 | func init() { 24 | raceMode = true 25 | } 26 | -------------------------------------------------------------------------------- /balancer/rls/internal/test/e2e/e2e.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package e2e contains utilities for end-to-end RouteLookupService tests. 20 | package e2e 21 | -------------------------------------------------------------------------------- /internal/grpcutil/grpcutil.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package grpcutil provides utility functions used across the gRPC codebase. 20 | package grpcutil 21 | -------------------------------------------------------------------------------- /test/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package test 20 | 21 | import "google.golang.org/grpc/grpclog" 22 | 23 | var logger = grpclog.Component("testing") 24 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | schedule: 7 | - cron: '24 20 * * 3' 8 | 9 | permissions: 10 | contents: read 11 | security-events: write 12 | pull-requests: read 13 | actions: read 14 | 15 | jobs: 16 | analyze: 17 | name: Analyze 18 | runs-on: ubuntu-latest 19 | timeout-minutes: 30 20 | 21 | strategy: 22 | fail-fast: false 23 | 24 | steps: 25 | - name: Checkout repository 26 | uses: actions/checkout@v2 27 | 28 | # Initializes the CodeQL tools for scanning. 29 | - name: Initialize CodeQL 30 | uses: github/codeql-action/init@v1 31 | with: 32 | languages: go 33 | 34 | - name: Perform CodeQL Analysis 35 | uses: github/codeql-action/analyze@v1 36 | -------------------------------------------------------------------------------- /health/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package health 20 | 21 | import "google.golang.org/grpc/grpclog" 22 | 23 | var logger = grpclog.Component("health_service") 24 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # gRPC Hello World 2 | 3 | Follow these setup to run the [quick start][] example: 4 | 5 | 1. Get the code: 6 | 7 | ```console 8 | $ go get google.golang.org/grpc/examples/helloworld/greeter_client 9 | $ go get google.golang.org/grpc/examples/helloworld/greeter_server 10 | ``` 11 | 12 | 2. Run the server: 13 | 14 | ```console 15 | $ $(go env GOPATH)/bin/greeter_server & 16 | ``` 17 | 18 | 3. Run the client: 19 | 20 | ```console 21 | $ $(go env GOPATH)/bin/greeter_client 22 | Greeting: Hello world 23 | ``` 24 | 25 | For more details (including instructions for making a small change to the 26 | example code) or if you're having trouble running this example, see [Quick 27 | Start][]. 28 | 29 | [quick start]: https://grpc.io/docs/languages/go/quickstart 30 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/localhost-openssl.cnf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = req_distinguished_name 3 | req_extensions = v3_req 4 | 5 | [req_distinguished_name] 6 | countryName = Country Name (2 letter code) 7 | countryName_default = US 8 | stateOrProvinceName = State or Province Name (full name) 9 | stateOrProvinceName_default = Illinois 10 | localityName = Locality Name (eg, city) 11 | localityName_default = Chicago 12 | organizationName = Organization Name (eg, company) 13 | organizationName_default = Example, Co. 14 | commonName = Common Name (eg, YOUR name) 15 | commonName_max = 64 16 | 17 | [v3_req] 18 | basicConstraints = CA:FALSE 19 | keyUsage = nonRepudiation, digitalSignature, keyEncipherment 20 | subjectAltName = @alt_names 21 | 22 | [alt_names] 23 | DNS.1 = localhost 24 | -------------------------------------------------------------------------------- /test/tools/tools_vet.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package tools is used to pin specific versions of external tools in this 20 | // module's go.mod that gRPC uses for internal testing. 21 | package tools 22 | -------------------------------------------------------------------------------- /internal/googlecloud/manufacturer.go: -------------------------------------------------------------------------------- 1 | //go:build !(linux || windows) 2 | // +build !linux,!windows 3 | 4 | /* 5 | * 6 | * Copyright 2022 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package googlecloud 23 | 24 | func manufacturer() ([]byte, error) { 25 | return nil, nil 26 | } 27 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | //go:generate ./regenerate.sh 20 | 21 | /* 22 | Package grpc implements an RPC system called gRPC. 23 | 24 | See grpc.io for more information about gRPC. 25 | */ 26 | package grpc // import "google.golang.org/grpc" 27 | -------------------------------------------------------------------------------- /reflection/grpc_testing/proto2.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto2"; 16 | 17 | option go_package = "google.golang.org/grpc/reflection/grpc_testing"; 18 | 19 | package grpc.testing; 20 | 21 | message ToBeExtended { 22 | required int32 foo = 1; 23 | extensions 10 to 30; 24 | } 25 | -------------------------------------------------------------------------------- /grpc_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpc 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/internal/grpctest" 25 | ) 26 | 27 | type s struct { 28 | grpctest.Tester 29 | } 30 | 31 | func Test(t *testing.T) { 32 | grpctest.RunSubTests(t, s{}) 33 | } 34 | -------------------------------------------------------------------------------- /internal/channelz/util_nonlinux.go: -------------------------------------------------------------------------------- 1 | //go:build !linux 2 | // +build !linux 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package channelz 23 | 24 | // GetSocketOption gets the socket option info of the conn. 25 | func GetSocketOption(c interface{}) *SocketOptionData { 26 | return nil 27 | } 28 | -------------------------------------------------------------------------------- /testdata/x509/openssl.cnf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = req_distinguished_name 3 | attributes = req_attributes 4 | 5 | [req_distinguished_name] 6 | 7 | [req_attributes] 8 | 9 | [test_ca] 10 | basicConstraints = critical,CA:TRUE 11 | subjectKeyIdentifier = hash 12 | authorityKeyIdentifier = keyid:always,issuer:always 13 | keyUsage = critical,keyCertSign 14 | 15 | [test_server] 16 | basicConstraints = critical,CA:FALSE 17 | subjectKeyIdentifier = hash 18 | keyUsage = critical,digitalSignature,keyEncipherment,keyAgreement 19 | subjectAltName = @server_alt_names 20 | 21 | [server_alt_names] 22 | DNS.1 = *.test.example.com 23 | 24 | [test_client] 25 | basicConstraints = critical,CA:FALSE 26 | subjectKeyIdentifier = hash 27 | keyUsage = critical,nonRepudiation,digitalSignature,keyEncipherment 28 | extendedKeyUsage = critical,clientAuth 29 | -------------------------------------------------------------------------------- /examples/data/x509/openssl.cnf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = req_distinguished_name 3 | attributes = req_attributes 4 | 5 | [req_distinguished_name] 6 | 7 | [req_attributes] 8 | 9 | [test_ca] 10 | basicConstraints = critical,CA:TRUE 11 | subjectKeyIdentifier = hash 12 | authorityKeyIdentifier = keyid:always,issuer:always 13 | keyUsage = critical,keyCertSign 14 | 15 | [test_server] 16 | basicConstraints = critical,CA:FALSE 17 | subjectKeyIdentifier = hash 18 | keyUsage = critical,digitalSignature,keyEncipherment,keyAgreement 19 | subjectAltName = @server_alt_names 20 | 21 | [server_alt_names] 22 | DNS.1 = *.test.example.com 23 | 24 | [test_client] 25 | basicConstraints = critical,CA:FALSE 26 | subjectKeyIdentifier = hash 27 | keyUsage = critical,nonRepudiation,digitalSignature,keyEncipherment 28 | extendedKeyUsage = critical,clientAuth 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Report a non-security bug. For suspected security vulnerabilities or crashes, please use "Report a Security Vulnerability", below. 4 | labels: 'Type: Bug' 5 | 6 | --- 7 | 8 | NOTE: if you are reporting is a potential security vulnerability or a crash, 9 | please follow our CVE process at 10 | https://github.com/grpc/proposal/blob/master/P4-grpc-cve-process.md instead of 11 | filing an issue here. 12 | 13 | Please see the FAQ in our main README.md, then answer the questions below 14 | before submitting your issue. 15 | 16 | ### What version of gRPC are you using? 17 | 18 | ### What version of Go are you using (`go version`)? 19 | 20 | ### What operating system (Linux, Windows, …) and version? 21 | 22 | ### What did you do? 23 | If possible, provide a recipe for reproducing the error. 24 | 25 | ### What did you expect to see? 26 | 27 | ### What did you see instead? 28 | -------------------------------------------------------------------------------- /internal/googlecloud/manufacturer_linux.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2022 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package googlecloud 20 | 21 | import "io/ioutil" 22 | 23 | const linuxProductNameFile = "/sys/class/dmi/id/product_name" 24 | 25 | func manufacturer() ([]byte, error) { 26 | return ioutil.ReadFile(linuxProductNameFile) 27 | } 28 | -------------------------------------------------------------------------------- /internal/testutils/local_listener.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package testutils 20 | 21 | import "net" 22 | 23 | // LocalTCPListener returns a net.Listener listening on local address and port. 24 | func LocalTCPListener() (net.Listener, error) { 25 | return net.Listen("tcp", "localhost:0") 26 | } 27 | -------------------------------------------------------------------------------- /resolver/resolver_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package resolver 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/internal/grpctest" 25 | ) 26 | 27 | type s struct { 28 | grpctest.Tester 29 | } 30 | 31 | func Test(t *testing.T) { 32 | grpctest.RunSubTests(t, s{}) 33 | } 34 | -------------------------------------------------------------------------------- /xds/internal/balancer/priority/utils.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package priority 20 | 21 | func equalStringSlice(a, b []string) bool { 22 | if len(a) != len(b) { 23 | return false 24 | } 25 | for i := range a { 26 | if a[i] != b[i] { 27 | return false 28 | } 29 | } 30 | return true 31 | } 32 | -------------------------------------------------------------------------------- /cmd/protoc-gen-go-grpc/README.md: -------------------------------------------------------------------------------- 1 | # protoc-gen-go-grpc 2 | 3 | This tool generates Go language bindings of `service`s in protobuf definition 4 | files for gRPC. For usage information, please see our [quick start 5 | guide](https://grpc.io/docs/languages/go/quickstart/). 6 | 7 | ## Future-proofing services 8 | 9 | By default, to register services using the methods generated by this tool, the 10 | service implementations must embed the corresponding 11 | `UnimplementedServer` for future compatibility. This is a behavior 12 | change from the grpc code generator previously included with `protoc-gen-go`. 13 | To restore this behavior, set the option `require_unimplemented_servers=false`. 14 | E.g.: 15 | 16 | ``` 17 | protoc --go-grpc_out=require_unimplemented_servers=false[,other options...]:. \ 18 | ``` 19 | 20 | Note that this is not recommended, and the option is only provided to restore 21 | backward compatibility with previously-generated code. 22 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/bootstrap/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package bootstrap 20 | 21 | import ( 22 | "google.golang.org/grpc/grpclog" 23 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 24 | ) 25 | 26 | const prefix = "[xds-bootstrap] " 27 | 28 | var logger = internalgrpclog.NewPrefixLogger(grpclog.Component("xds"), prefix) 29 | -------------------------------------------------------------------------------- /test/timeouts.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package test 20 | 21 | import "time" 22 | 23 | const ( 24 | // Default timeout for tests in this package. 25 | defaultTestTimeout = 10 * time.Second 26 | // Default short timeout, to be used when waiting for events which are not 27 | // expected to happen. 28 | defaultTestShortTimeout = 100 * time.Millisecond 29 | ) 30 | -------------------------------------------------------------------------------- /examples/features/unix_abstract/README.md: -------------------------------------------------------------------------------- 1 | # Unix abstract sockets 2 | 3 | This examples shows how to start a gRPC server listening on a unix abstract 4 | socket and how to get a gRPC client to connect to it. 5 | 6 | ## What is a unix abstract socket 7 | 8 | An abstract socket address is distinguished from a regular unix socket by the 9 | fact that the first byte of the address is a null byte ('\0'). The address has 10 | no connection with filesystem path names. 11 | 12 | ## Try it 13 | 14 | ``` 15 | go run server/main.go 16 | ``` 17 | 18 | ``` 19 | go run client/main.go 20 | ``` 21 | 22 | ## Explanation 23 | 24 | The gRPC server in this example listens on an address starting with a null byte 25 | and the network is `unix`. The client uses the `unix-abstract` scheme with the 26 | endpoint set to the abstract unix socket address without the null byte. The 27 | `unix` resolver takes care of adding the null byte on the client. See 28 | https://github.com/grpc/grpc/blob/master/doc/naming.md for the more details. 29 | 30 | -------------------------------------------------------------------------------- /channelz/service/func_nonlinux.go: -------------------------------------------------------------------------------- 1 | //go:build !linux 2 | // +build !linux 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package service 23 | 24 | import ( 25 | channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" 26 | "google.golang.org/grpc/internal/channelz" 27 | ) 28 | 29 | func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOption { 30 | return nil 31 | } 32 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/load/reporter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package load 20 | 21 | // PerClusterReporter wraps the methods from the loadStore that are used here. 22 | type PerClusterReporter interface { 23 | CallStarted(locality string) 24 | CallFinished(locality string, err error) 25 | CallServerLoad(locality, name string, val float64) 26 | CallDropped(category string) 27 | } 28 | -------------------------------------------------------------------------------- /codec_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2014 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpc 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/encoding" 25 | "google.golang.org/grpc/encoding/proto" 26 | ) 27 | 28 | func (s) TestGetCodecForProtoIsNotNil(t *testing.T) { 29 | if encoding.GetCodec(proto.Name) == nil { 30 | t.Fatalf("encoding.GetCodec(%q) must not be nil by default", proto.Name) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /examples/route_guide/README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | The route guide server and client demonstrate how to use grpc go libraries to 3 | perform unary, client streaming, server streaming and full duplex RPCs. 4 | 5 | Please refer to [gRPC Basics: Go](https://grpc.io/docs/tutorials/basic/go.html) for more information. 6 | 7 | See the definition of the route guide service in `routeguide/route_guide.proto`. 8 | 9 | # Run the sample code 10 | To compile and run the server, assuming you are in the root of the `route_guide` 11 | folder, i.e., `.../examples/route_guide/`, simply: 12 | 13 | ```sh 14 | $ go run server/server.go 15 | ``` 16 | 17 | Likewise, to run the client: 18 | 19 | ```sh 20 | $ go run client/client.go 21 | ``` 22 | 23 | # Optional command line flags 24 | The server and client both take optional command line flags. For example, the 25 | client and server run without TLS by default. To enable TLS: 26 | 27 | ```sh 28 | $ go run server/server.go -tls=true 29 | ``` 30 | 31 | and 32 | 33 | ```sh 34 | $ go run client/client.go -tls=true 35 | ``` 36 | -------------------------------------------------------------------------------- /internal/grpcutil/regex.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpcutil 20 | 21 | import "regexp" 22 | 23 | // FullMatchWithRegex returns whether the full text matches the regex provided. 24 | func FullMatchWithRegex(re *regexp.Regexp, text string) bool { 25 | if len(text) == 0 { 26 | return re.MatchString(text) 27 | } 28 | re.Longest() 29 | rem := re.FindString(text) 30 | return len(rem) == len(text) 31 | } 32 | -------------------------------------------------------------------------------- /reflection/grpc_testing/proto2_ext2.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto2"; 16 | 17 | option go_package = "google.golang.org/grpc/reflection/grpc_testing"; 18 | 19 | package grpc.testing; 20 | 21 | import "reflection/grpc_testing/proto2.proto"; 22 | 23 | extend ToBeExtended { 24 | optional string frob = 23; 25 | optional AnotherExtension nitz = 29; 26 | } 27 | 28 | message AnotherExtension { 29 | optional int32 whatchamacallit = 1; 30 | } 31 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/xdsclient_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package xdsclient_test 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/internal/grpctest" 25 | _ "google.golang.org/grpc/xds/internal/xdsclient/controller/version/v2" // Register the v2 API client. 26 | ) 27 | 28 | type s struct { 29 | grpctest.Tester 30 | } 31 | 32 | func Test(t *testing.T) { 33 | grpctest.RunSubTests(t, s{}) 34 | } 35 | -------------------------------------------------------------------------------- /test/codec_perf/perf.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Messages used for performance tests that may not reference grpc directly for 16 | // reasons of import cycles. 17 | syntax = "proto3"; 18 | 19 | option go_package = "google.golang.org/grpc/test/codec_perf"; 20 | 21 | package codec.perf; 22 | 23 | // Buffer is a message that contains a body of bytes that is used to exercise 24 | // encoding and decoding overheads. 25 | message Buffer { 26 | bytes body = 1; 27 | } 28 | -------------------------------------------------------------------------------- /admin/admin_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package admin_test 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/admin/test" 25 | "google.golang.org/grpc/codes" 26 | ) 27 | 28 | func TestRegisterNoCSDS(t *testing.T) { 29 | test.RunRegisterTests(t, test.ExpectedStatusCodes{ 30 | ChannelzCode: codes.OK, 31 | // CSDS is not registered because xDS isn't imported. 32 | CSDSCode: codes.Unimplemented, 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: vet test testrace 2 | 3 | build: 4 | go build google.golang.org/grpc/... 5 | 6 | clean: 7 | go clean -i google.golang.org/grpc/... 8 | 9 | deps: 10 | GO111MODULE=on go get -d -v google.golang.org/grpc/... 11 | 12 | proto: 13 | @ if ! which protoc > /dev/null; then \ 14 | echo "error: protoc not installed" >&2; \ 15 | exit 1; \ 16 | fi 17 | go generate google.golang.org/grpc/... 18 | 19 | test: 20 | go test -cpu 1,4 -timeout 7m google.golang.org/grpc/... 21 | 22 | testsubmodule: 23 | cd security/advancedtls && go test -cpu 1,4 -timeout 7m google.golang.org/grpc/security/advancedtls/... 24 | cd security/authorization && go test -cpu 1,4 -timeout 7m google.golang.org/grpc/security/authorization/... 25 | 26 | testrace: 27 | go test -race -cpu 1,4 -timeout 7m google.golang.org/grpc/... 28 | 29 | testdeps: 30 | GO111MODULE=on go get -d -v -t google.golang.org/grpc/... 31 | 32 | vet: vetdeps 33 | ./vet.sh 34 | 35 | vetdeps: 36 | ./vet.sh -install 37 | 38 | .PHONY: \ 39 | all \ 40 | build \ 41 | clean \ 42 | proto \ 43 | test \ 44 | testrace \ 45 | vet \ 46 | vetdeps 47 | -------------------------------------------------------------------------------- /resolver/passthrough/passthrough.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package passthrough implements a pass-through resolver. It sends the target 20 | // name without scheme back to gRPC as resolved address. 21 | // 22 | // Deprecated: this package is imported by grpc and should not need to be 23 | // imported directly by users. 24 | package passthrough 25 | 26 | import _ "google.golang.org/grpc/internal/resolver/passthrough" // import for side effects after package was moved 27 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package xdsclient 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[xds-client %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *clientImpl) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /xds/internal/resolver/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package resolver 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[xds-resolver %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *xdsResolver) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /xds/internal/balancer/cdsbalancer/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package cdsbalancer 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[cds-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *cdsBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /xds/internal/balancer/priority/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package priority 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[priority-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *priorityBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /xds/internal/balancer/ringhash/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package ringhash 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[ring-hash-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *ringhashBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /channelz/service/util_sktopt_amd64_test.go: -------------------------------------------------------------------------------- 1 | //go:build amd64 && linux 2 | // +build amd64,linux 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package service 23 | 24 | import ( 25 | "golang.org/x/sys/unix" 26 | channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" 27 | ) 28 | 29 | func protoToTime(protoTime *channelzpb.SocketOptionTimeout) *unix.Timeval { 30 | timeout := &unix.Timeval{} 31 | timeout.Sec, timeout.Usec = convertToDuration(protoTime.GetDuration()) 32 | return timeout 33 | } 34 | -------------------------------------------------------------------------------- /reflection/grpc_testing/proto2_ext.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto2"; 16 | 17 | option go_package = "google.golang.org/grpc/reflection/grpc_testing"; 18 | 19 | package grpc.testing; 20 | 21 | import "reflection/grpc_testing/proto2.proto"; 22 | import "reflection/grpc_testing/test.proto"; 23 | 24 | extend ToBeExtended { 25 | optional int32 foo = 13; 26 | optional Extension bar = 17; 27 | optional SearchRequest baz = 19; 28 | } 29 | 30 | message Extension { 31 | optional int32 whatzit = 1; 32 | } 33 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | name: Stale bot 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: "44 */2 * * *" 7 | 8 | jobs: 9 | stale: 10 | runs-on: ubuntu-latest 11 | permissions: 12 | issues: write 13 | pull-requests: write 14 | 15 | steps: 16 | - uses: actions/stale@v4 17 | with: 18 | repo-token: ${{ secrets.GITHUB_TOKEN }} 19 | days-before-stale: 6 20 | days-before-close: 7 21 | only-labels: 'Status: Requires Reporter Clarification' 22 | stale-issue-label: 'stale' 23 | stale-pr-label: 'stale' 24 | operations-per-run: 999 25 | stale-issue-message: > 26 | This issue is labeled as requiring an update from the reporter, and no update has been received 27 | after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed. 28 | stale-pr-message: > 29 | This PR is labeled as requiring an update from the reporter, and no update has been received 30 | after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed. 31 | -------------------------------------------------------------------------------- /balancer/weightedtarget/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package weightedtarget 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[weighted-target-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *weightedTargetBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /internal/channelz/util_linux.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package channelz 20 | 21 | import ( 22 | "syscall" 23 | ) 24 | 25 | // GetSocketOption gets the socket option info of the conn. 26 | func GetSocketOption(socket interface{}) *SocketOptionData { 27 | c, ok := socket.(syscall.Conn) 28 | if !ok { 29 | return nil 30 | } 31 | data := &SocketOptionData{} 32 | if rawConn, err := c.SyscallConn(); err == nil { 33 | rawConn.Control(data.Getsockopt) 34 | return data 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /internal/profiling/goid_regular.go: -------------------------------------------------------------------------------- 1 | //go:build !grpcgoid 2 | // +build !grpcgoid 3 | 4 | /* 5 | * 6 | * Copyright 2019 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package profiling 23 | 24 | // This dummy function always returns 0. In some modified dev environments, 25 | // this may be replaced with a call to a function in a modified Go runtime that 26 | // retrieves the goroutine ID efficiently. See goid_modified.go for a different 27 | // version of goId that requires a grpcgoid build tag to compile. 28 | func goid() int64 { 29 | return 0 30 | } 31 | -------------------------------------------------------------------------------- /xds/internal/balancer/clusterimpl/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package clusterimpl 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[xds-cluster-impl-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *clusterImplBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module google.golang.org/grpc 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/cespare/xxhash/v2 v2.1.1 7 | github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 8 | github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 9 | github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 10 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b 11 | github.com/golang/protobuf v1.5.2 12 | github.com/google/go-cmp v0.5.6 13 | github.com/google/uuid v1.1.2 14 | golang.org/x/net v0.0.0-20201021035429-f5854403a974 15 | golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d 16 | golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 17 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 18 | google.golang.org/protobuf v1.27.1 19 | ) 20 | 21 | require ( 22 | cloud.google.com/go v0.34.0 // indirect 23 | github.com/census-instrumentation/opencensus-proto v0.2.1 // indirect 24 | github.com/envoyproxy/protoc-gen-validate v0.1.0 // indirect 25 | golang.org/x/text v0.3.3 // indirect 26 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect 27 | google.golang.org/appengine v1.4.0 // indirect 28 | ) 29 | -------------------------------------------------------------------------------- /examples/features/debugging/README.md: -------------------------------------------------------------------------------- 1 | # Debugging 2 | 3 | Currently, grpc provides two major tools to help user debug issues, which are logging and channelz. 4 | 5 | ## Logs 6 | gRPC has put substantial logging instruments on critical paths of gRPC to help users debug issues. 7 | The [Log Levels](https://github.com/grpc/grpc-go/blob/master/Documentation/log_levels.md) doc describes 8 | what each log level means in the gRPC context. 9 | 10 | To turn on the logs for debugging, run the code with the following environment variable: 11 | `GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info`. 12 | 13 | ## Channelz 14 | We also provide a runtime debugging tool, Channelz, to help users with live debugging. 15 | 16 | See the channelz blog post here ([link](https://grpc.io/blog/a-short-introduction-to-channelz/)) for 17 | details about how to use channelz service to debug live program. 18 | 19 | ## Try it 20 | The example is able to showcase how logging and channelz can help with debugging. See the channelz 21 | blog post linked above for full explanation. 22 | 23 | ``` 24 | go run server/main.go 25 | ``` 26 | 27 | ``` 28 | go run client/main.go 29 | ``` 30 | -------------------------------------------------------------------------------- /internal/wrr/edf_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package wrr 18 | 19 | import ( 20 | "testing" 21 | ) 22 | 23 | func (s) TestEDFOnEndpointsWithSameWeight(t *testing.T) { 24 | wrr := NewEDF() 25 | wrr.Add("1", 1) 26 | wrr.Add("2", 1) 27 | wrr.Add("3", 1) 28 | expected := []string{"1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3"} 29 | for i := 0; i < len(expected); i++ { 30 | item := wrr.Next().(string) 31 | if item != expected[i] { 32 | t.Errorf("wrr Next=%s, want=%s", item, expected[i]) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /xds/internal/balancer/clusterresolver/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package clusterresolver 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | internalgrpclog "google.golang.org/grpc/internal/grpclog" 26 | ) 27 | 28 | const prefix = "[xds-cluster-resolver-lb %p] " 29 | 30 | var logger = grpclog.Component("xds") 31 | 32 | func prefixLogger(p *clusterResolverBalancer) *internalgrpclog.PrefixLogger { 33 | return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p)) 34 | } 35 | -------------------------------------------------------------------------------- /internal/wrr/wrr.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | // Package wrr contains the interface and common implementations of wrr 19 | // algorithms. 20 | package wrr 21 | 22 | // WRR defines an interface that implements weighted round robin. 23 | type WRR interface { 24 | // Add adds an item with weight to the WRR set. 25 | // 26 | // Add and Next need to be thread safe. 27 | Add(item interface{}, weight int64) 28 | // Next returns the next picked item. 29 | // 30 | // Add and Next need to be thread safe. 31 | Next() interface{} 32 | } 33 | -------------------------------------------------------------------------------- /channelz/service/util_sktopt_386_test.go: -------------------------------------------------------------------------------- 1 | //go:build 386 && linux 2 | // +build 386,linux 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package service 23 | 24 | import ( 25 | "golang.org/x/sys/unix" 26 | channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" 27 | ) 28 | 29 | func protoToTime(protoTime *channelzpb.SocketOptionTimeout) *unix.Timeval { 30 | timeout := &unix.Timeval{} 31 | sec, usec := convertToDuration(protoTime.GetDuration()) 32 | timeout.Sec, timeout.Usec = int32(sec), int32(usec) 33 | return timeout 34 | } 35 | -------------------------------------------------------------------------------- /internal/envconfig/envconfig.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package envconfig contains grpc settings configured by environment variables. 20 | package envconfig 21 | 22 | import ( 23 | "os" 24 | "strings" 25 | ) 26 | 27 | const ( 28 | prefix = "GRPC_GO_" 29 | txtErrIgnoreStr = prefix + "IGNORE_TXT_ERRORS" 30 | ) 31 | 32 | var ( 33 | // TXTErrIgnore is set if TXT errors should be ignored ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false"). 34 | TXTErrIgnore = !strings.EqualFold(os.Getenv(txtErrIgnoreStr), "false") 35 | ) 36 | -------------------------------------------------------------------------------- /reflection/grpc_testing_not_regenerate/dynamic.proto: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | syntax = "proto3"; 19 | 20 | option go_package = "google.golang.org/grpc/reflection/grpc_testing_not_regenerate"; 21 | 22 | package grpc.testing; 23 | 24 | message DynamicRes {} 25 | 26 | message DynamicReq {} 27 | 28 | // DynamicService is used to test reflection on dynamically constructed protocol 29 | // buffer messages. 30 | service DynamicService { 31 | // DynamicMessage1 is a test RPC for dynamically constructed protobufs. 32 | rpc DynamicMessage1(DynamicReq) returns (DynamicRes); 33 | } -------------------------------------------------------------------------------- /security/advancedtls/examples/credential_reloading_from_files/README.md: -------------------------------------------------------------------------------- 1 | # Credential Reloading From Files 2 | 3 | Credential reloading is a feature supported in the advancedtls library. 4 | A very common way to achieve this is to reload from files. 5 | 6 | This example demonstrates how to set the reloading fields in advancedtls API. 7 | Basically, a set of file system locations holding the credential data need to be specified. 8 | Once the credential data needs to be updated, users just change the credential data in the file system, and gRPC will pick up the changes automatically. 9 | 10 | A couple of things to note: 11 | 1. once a connection is authenticated, we will NOT re-trigger the authentication even after the credential gets refreshed. 12 | 2. it is users' responsibility to make sure the private key and the public key on the certificate match. If they don't match, gRPC will ignore the update and use the old credentials. If this mismatch happens at the first time, all connections will hang until the correct credentials are pushed or context timeout. 13 | 14 | ## Try it 15 | In directory `security/advancedtls/examples`: 16 | 17 | ``` 18 | go run server/main.go 19 | ``` 20 | 21 | ``` 22 | go run client/main.go 23 | ``` -------------------------------------------------------------------------------- /test/tools/tools.go: -------------------------------------------------------------------------------- 1 | //go:build tools 2 | // +build tools 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | // This file is not intended to be compiled. Because some of these imports are 23 | // not actual go packages, we use a build constraint at the top of this file to 24 | // prevent tools from inspecting the imports. 25 | 26 | package tools 27 | 28 | import ( 29 | _ "github.com/client9/misspell/cmd/misspell" 30 | _ "github.com/golang/protobuf/protoc-gen-go" 31 | _ "golang.org/x/lint/golint" 32 | _ "golang.org/x/tools/cmd/goimports" 33 | _ "honnef.co/go/tools/cmd/staticcheck" 34 | ) 35 | -------------------------------------------------------------------------------- /internal/testutils/marshal_any.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package testutils 19 | 20 | import ( 21 | "fmt" 22 | 23 | "github.com/golang/protobuf/proto" 24 | "github.com/golang/protobuf/ptypes" 25 | "google.golang.org/protobuf/types/known/anypb" 26 | ) 27 | 28 | // MarshalAny is a convenience function to marshal protobuf messages into any 29 | // protos. It will panic if the marshaling fails. 30 | func MarshalAny(m proto.Message) *anypb.Any { 31 | a, err := ptypes.MarshalAny(m) 32 | if err != nil { 33 | panic(fmt.Sprintf("ptypes.MarshalAny(%+v) failed: %v", m, err)) 34 | } 35 | return a 36 | } 37 | -------------------------------------------------------------------------------- /reflection/grpc_testing/test.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto3"; 16 | 17 | option go_package = "google.golang.org/grpc/reflection/grpc_testing"; 18 | 19 | package grpc.testing; 20 | 21 | message SearchResponse { 22 | message Result { 23 | string url = 1; 24 | string title = 2; 25 | repeated string snippets = 3; 26 | } 27 | repeated Result results = 1; 28 | } 29 | 30 | message SearchRequest { 31 | string query = 1; 32 | } 33 | 34 | service SearchService { 35 | rpc Search(SearchRequest) returns (SearchResponse); 36 | rpc StreamingSearch(stream SearchRequest) returns (stream SearchResponse); 37 | } 38 | -------------------------------------------------------------------------------- /internal/testutils/status_equal.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package testutils 20 | 21 | import ( 22 | "github.com/golang/protobuf/proto" 23 | "google.golang.org/grpc/status" 24 | ) 25 | 26 | // StatusErrEqual returns true iff both err1 and err2 wrap status.Status errors 27 | // and their underlying status protos are equal. 28 | func StatusErrEqual(err1, err2 error) bool { 29 | status1, ok := status.FromError(err1) 30 | if !ok { 31 | return false 32 | } 33 | status2, ok := status.FromError(err2) 34 | if !ok { 35 | return false 36 | } 37 | return proto.Equal(status1.Proto(), status2.Proto()) 38 | } 39 | -------------------------------------------------------------------------------- /admin/test/admin_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // This file has the same content as admin_test.go, difference is that this is 20 | // in another package, and it imports "xds", so we can test that csds is 21 | // registered when xds is imported. 22 | 23 | package test_test 24 | 25 | import ( 26 | "testing" 27 | 28 | "google.golang.org/grpc/admin/test" 29 | "google.golang.org/grpc/codes" 30 | _ "google.golang.org/grpc/xds" 31 | ) 32 | 33 | func TestRegisterWithCSDS(t *testing.T) { 34 | test.RunRegisterTests(t, test.ExpectedStatusCodes{ 35 | ChannelzCode: codes.OK, 36 | CSDSCode: codes.OK, 37 | }) 38 | } 39 | -------------------------------------------------------------------------------- /resolver/dns/dns_resolver.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package dns implements a dns resolver to be installed as the default resolver 20 | // in grpc. 21 | // 22 | // Deprecated: this package is imported by grpc and should not need to be 23 | // imported directly by users. 24 | package dns 25 | 26 | import ( 27 | "google.golang.org/grpc/internal/resolver/dns" 28 | "google.golang.org/grpc/resolver" 29 | ) 30 | 31 | // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers. 32 | // 33 | // Deprecated: import grpc and use resolver.Get("dns") instead. 34 | func NewBuilder() resolver.Builder { 35 | return dns.NewBuilder() 36 | } 37 | -------------------------------------------------------------------------------- /testdata/ca.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDWjCCAkKgAwIBAgIUWrP0VvHcy+LP6UuYNtiL9gBhD5owDQYJKoZIhvcNAQEL 3 | BQAwVjELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM 4 | GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGdGVzdGNhMB4XDTIw 5 | MDMxNzE4NTk1MVoXDTMwMDMxNTE4NTk1MVowVjELMAkGA1UEBhMCQVUxEzARBgNV 6 | BAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0 7 | ZDEPMA0GA1UEAwwGdGVzdGNhMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 8 | AQEAsGL0oXflF0LzoM+Bh+qUU9yhqzw2w8OOX5mu/iNCyUOBrqaHi7mGHx73GD01 9 | diNzCzvlcQqdNIH6NQSL7DTpBjca66jYT9u73vZe2MDrr1nVbuLvfu9850cdxiUO 10 | Inv5xf8+sTHG0C+a+VAvMhsLiRjsq+lXKRJyk5zkbbsETybqpxoJ+K7CoSy3yc/k 11 | QIY3TipwEtwkKP4hzyo6KiGd/DPexie4nBUInN3bS1BUeNZ5zeaIC2eg3bkeeW7c 12 | qT55b+Yen6CxY0TEkzBK6AKt/WUialKMgT0wbTxRZO7kUCH3Sq6e/wXeFdJ+HvdV 13 | LPlAg5TnMaNpRdQih/8nRFpsdwIDAQABoyAwHjAMBgNVHRMEBTADAQH/MA4GA1Ud 14 | DwEB/wQEAwICBDANBgkqhkiG9w0BAQsFAAOCAQEAkTrKZjBrJXHps/HrjNCFPb5a 15 | THuGPCSsepe1wkKdSp1h4HGRpLoCgcLysCJ5hZhRpHkRihhef+rFHEe60UePQO3S 16 | CVTtdJB4CYWpcNyXOdqefrbJW5QNljxgi6Fhvs7JJkBqdXIkWXtFk2eRgOIP2Eo9 17 | /OHQHlYnwZFrk6sp4wPyR+A95S0toZBcyDVz7u+hOW0pGK3wviOe9lvRgj/H3Pwt 18 | bewb0l+MhRig0/DVHamyVxrDRbqInU1/GTNCwcZkXKYFWSf92U+kIcTth24Q1gcw 19 | eZiLl5FfrWokUNytFElXob0V0a5/kbhiLc3yWmvWqHTpqCALbVyF+rKJo2f5Kw== 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /health/server_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package health_test 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc" 25 | "google.golang.org/grpc/health" 26 | healthgrpc "google.golang.org/grpc/health/grpc_health_v1" 27 | "google.golang.org/grpc/internal/grpctest" 28 | ) 29 | 30 | type s struct { 31 | grpctest.Tester 32 | } 33 | 34 | func Test(t *testing.T) { 35 | grpctest.RunSubTests(t, s{}) 36 | } 37 | 38 | // Make sure the service implementation complies with the proto definition. 39 | func (s) TestRegister(t *testing.T) { 40 | s := grpc.NewServer() 41 | healthgrpc.RegisterHealthServer(s, health.NewServer()) 42 | s.Stop() 43 | } 44 | -------------------------------------------------------------------------------- /test/parse_config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2022 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package test 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/resolver/manual" 25 | "google.golang.org/grpc/serviceconfig" 26 | ) 27 | 28 | // parseServiceConfig is a test helper which uses the manual resolver to parse 29 | // the given service config. It calls t.Fatal() if service config parsing fails. 30 | func parseServiceConfig(t *testing.T, r *manual.Resolver, sc string) *serviceconfig.ParseResult { 31 | t.Helper() 32 | 33 | scpr := r.CC.ParseServiceConfig(sc) 34 | if scpr.Err != nil { 35 | t.Fatalf("Failed to parse service config %q: %v", sc, scpr.Err) 36 | } 37 | return scpr 38 | } 39 | -------------------------------------------------------------------------------- /xds/internal/test/e2e/e2e_utils.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package e2e 19 | 20 | import ( 21 | "testing" 22 | 23 | "github.com/google/go-cmp/cmp" 24 | channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" 25 | ) 26 | 27 | func verifySubConnStates(t *testing.T, scs []*channelzpb.Subchannel, want map[channelzpb.ChannelConnectivityState_State]int) { 28 | t.Helper() 29 | var scStatsCount = map[channelzpb.ChannelConnectivityState_State]int{} 30 | for _, sc := range scs { 31 | scStatsCount[sc.Data.State.State]++ 32 | } 33 | if diff := cmp.Diff(scStatsCount, want); diff != "" { 34 | t.Fatalf("got unexpected number of subchannels in state Ready, %v, scs: %v", diff, scs) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/attributes.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package xdsclient 19 | 20 | import "google.golang.org/grpc/resolver" 21 | 22 | type clientKeyType string 23 | 24 | const clientKey = clientKeyType("grpc.xds.internal.client.Client") 25 | 26 | // FromResolverState returns the Client from state, or nil if not present. 27 | func FromResolverState(state resolver.State) XDSClient { 28 | cs, _ := state.Attributes.Value(clientKey).(XDSClient) 29 | return cs 30 | } 31 | 32 | // SetClient sets c in state and returns the new state. 33 | func SetClient(state resolver.State, c XDSClient) resolver.State { 34 | state.Attributes = state.Attributes.WithValue(clientKey, c) 35 | return state 36 | } 37 | -------------------------------------------------------------------------------- /testdata/testdata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package testdata 19 | 20 | import ( 21 | "path/filepath" 22 | "runtime" 23 | ) 24 | 25 | // basepath is the root directory of this package. 26 | var basepath string 27 | 28 | func init() { 29 | _, currentFile, _, _ := runtime.Caller(0) 30 | basepath = filepath.Dir(currentFile) 31 | } 32 | 33 | // Path returns the absolute path the given relative file or directory path, 34 | // relative to the google.golang.org/grpc/testdata directory in the user's GOPATH. 35 | // If rel is already absolute, it is returned unmodified. 36 | func Path(rel string) string { 37 | if filepath.IsAbs(rel) { 38 | return rel 39 | } 40 | 41 | return filepath.Join(basepath, rel) 42 | } 43 | -------------------------------------------------------------------------------- /examples/features/xds/README.md: -------------------------------------------------------------------------------- 1 | # gRPC xDS example 2 | 3 | xDS is the protocol initially used by Envoy, that is evolving into a universal 4 | data plan API for service mesh. 5 | 6 | The xDS example is a Hello World client/server capable of being configured with 7 | the XDS management protocol. Out-of-the-box it behaves the same as [our other 8 | hello world 9 | example](https://github.com/grpc/grpc-go/tree/master/examples/helloworld). The 10 | server replies with responses including its hostname. 11 | 12 | ## xDS environment setup 13 | 14 | This example doesn't include instructions to setup xDS environment. Please refer 15 | to documentation specific for your xDS management server. Examples will be added 16 | later. 17 | 18 | The client also needs a bootstrap file. See [gRFC 19 | A27](https://github.com/grpc/proposal/blob/master/A27-xds-global-load-balancing.md#xdsclient-and-bootstrap-file) 20 | for the bootstrap format. 21 | 22 | ## The client 23 | 24 | The client application needs to import the xDS package to install the resolver and balancers: 25 | 26 | ```go 27 | _ "google.golang.org/grpc/xds" // To install the xds resolvers and balancers. 28 | ``` 29 | 30 | Then, use `xds` target scheme for the ClientConn. 31 | 32 | ``` 33 | $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json 34 | $ go run client/main.go "xDS world" xds:///target_service 35 | ``` 36 | -------------------------------------------------------------------------------- /Documentation/versioning.md: -------------------------------------------------------------------------------- 1 | # Versioning and Releases 2 | 3 | Note: This document references terminology defined at http://semver.org. 4 | 5 | ## Release Frequency 6 | 7 | Regular MINOR releases of gRPC-Go are performed every six weeks. Patch releases 8 | to the previous two MINOR releases may be performed on demand or if serious 9 | security problems are discovered. 10 | 11 | ## Versioning Policy 12 | 13 | The gRPC-Go versioning policy follows the Semantic Versioning 2.0.0 14 | specification, with the following exceptions: 15 | 16 | - A MINOR version will not _necessarily_ add new functionality. 17 | 18 | - MINOR releases will not break backward compatibility, except in the following 19 | circumstances: 20 | 21 | - An API was marked as EXPERIMENTAL upon its introduction. 22 | - An API was marked as DEPRECATED in the initial MAJOR release. 23 | - An API is inherently flawed and cannot provide correct or secure behavior. 24 | 25 | In these cases, APIs MAY be changed or removed without a MAJOR release. 26 | Otherwise, backward compatibility will be preserved by MINOR releases. 27 | 28 | For an API marked as DEPRECATED, an alternative will be available (if 29 | appropriate) for at least three months prior to its removal. 30 | 31 | ## Release History 32 | 33 | Please see our release history on GitHub: 34 | https://github.com/grpc/grpc-go/releases 35 | -------------------------------------------------------------------------------- /examples/helloworld/helloworld/helloworld.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2015 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto3"; 16 | 17 | option go_package = "google.golang.org/grpc/examples/helloworld/helloworld"; 18 | option java_multiple_files = true; 19 | option java_package = "io.grpc.examples.helloworld"; 20 | option java_outer_classname = "HelloWorldProto"; 21 | 22 | package helloworld; 23 | 24 | // The greeting service definition. 25 | service Greeter { 26 | // Sends a greeting 27 | rpc SayHello (HelloRequest) returns (HelloReply) {} 28 | } 29 | 30 | // The request message containing the user's name. 31 | message HelloRequest { 32 | string name = 1; 33 | } 34 | 35 | // The response message containing the greetings 36 | message HelloReply { 37 | string message = 1; 38 | } 39 | -------------------------------------------------------------------------------- /channelz/channelz.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package channelz exports internals of the channelz implementation as required 20 | // by other gRPC packages. 21 | // 22 | // The implementation of the channelz spec as defined in 23 | // https://github.com/grpc/proposal/blob/master/A14-channelz.md, is provided by 24 | // the `internal/channelz` package. 25 | // 26 | // Experimental 27 | // 28 | // Notice: All APIs in this package are experimental and may be removed in a 29 | // later release. 30 | package channelz 31 | 32 | import "google.golang.org/grpc/internal/channelz" 33 | 34 | // Identifier is an opaque identifier which uniquely identifies an entity in the 35 | // channelz database. 36 | type Identifier = channelz.Identifier 37 | -------------------------------------------------------------------------------- /internal/balancergroup/balancerstateaggregator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package balancergroup 20 | 21 | import ( 22 | "google.golang.org/grpc/balancer" 23 | ) 24 | 25 | // BalancerStateAggregator aggregates sub-picker and connectivity states into a 26 | // state. 27 | // 28 | // It takes care of merging sub-picker into one picker. The picking config is 29 | // passed directly from the the parent to the aggregator implementation (instead 30 | // via balancer group). 31 | type BalancerStateAggregator interface { 32 | // UpdateState updates the state of the id. 33 | // 34 | // It's up to the implementation whether this will trigger an update to the 35 | // parent ClientConn. 36 | UpdateState(id string, state balancer.State) 37 | } 38 | -------------------------------------------------------------------------------- /internal/testutils/xds/e2e/bootstrap.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package e2e 20 | 21 | import ( 22 | "encoding/json" 23 | "fmt" 24 | ) 25 | 26 | // DefaultFileWatcherConfig is a helper function to create a default certificate 27 | // provider plugin configuration. The test is expected to have setup the files 28 | // appropriately before this configuration is used to instantiate providers. 29 | func DefaultFileWatcherConfig(certPath, keyPath, caPath string) json.RawMessage { 30 | return json.RawMessage(fmt.Sprintf(`{ 31 | "plugin_name": "file_watcher", 32 | "config": { 33 | "certificate_file": %q, 34 | "private_key_file": %q, 35 | "ca_certificate_file": %q, 36 | "refresh_interval": "600s" 37 | } 38 | }`, certPath, keyPath, caPath)) 39 | } 40 | -------------------------------------------------------------------------------- /xds/internal/balancer/ringhash/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package ringhash 20 | 21 | import "context" 22 | 23 | type clusterKey struct{} 24 | 25 | func getRequestHash(ctx context.Context) uint64 { 26 | requestHash, _ := ctx.Value(clusterKey{}).(uint64) 27 | return requestHash 28 | } 29 | 30 | // GetRequestHashForTesting returns the request hash in the context; to be used 31 | // for testing only. 32 | func GetRequestHashForTesting(ctx context.Context) uint64 { 33 | return getRequestHash(ctx) 34 | } 35 | 36 | // SetRequestHash adds the request hash to the context for use in Ring Hash Load 37 | // Balancing. 38 | func SetRequestHash(ctx context.Context, requestHash uint64) context.Context { 39 | return context.WithValue(ctx, clusterKey{}, requestHash) 40 | } 41 | -------------------------------------------------------------------------------- /internal/grpcutil/metadata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpcutil 20 | 21 | import ( 22 | "context" 23 | 24 | "google.golang.org/grpc/metadata" 25 | ) 26 | 27 | type mdExtraKey struct{} 28 | 29 | // WithExtraMetadata creates a new context with incoming md attached. 30 | func WithExtraMetadata(ctx context.Context, md metadata.MD) context.Context { 31 | return context.WithValue(ctx, mdExtraKey{}, md) 32 | } 33 | 34 | // ExtraMetadata returns the incoming metadata in ctx if it exists. The 35 | // returned MD should not be modified. Writing to it may cause races. 36 | // Modification should be made to copies of the returned MD. 37 | func ExtraMetadata(ctx context.Context) (md metadata.MD, ok bool) { 38 | md, ok = ctx.Value(mdExtraKey{}).(metadata.MD) 39 | return 40 | } 41 | -------------------------------------------------------------------------------- /testdata/server1.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDtDCCApygAwIBAgIUbJfTREJ6k6/+oInWhV1O1j3ZT0IwDQYJKoZIhvcNAQEL 3 | BQAwVjELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM 4 | GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGdGVzdGNhMB4XDTIw 5 | MDMxODAzMTA0MloXDTMwMDMxNjAzMTA0MlowZTELMAkGA1UEBhMCVVMxETAPBgNV 6 | BAgMCElsbGlub2lzMRAwDgYDVQQHDAdDaGljYWdvMRUwEwYDVQQKDAxFeGFtcGxl 7 | LCBDby4xGjAYBgNVBAMMESoudGVzdC5nb29nbGUuY29tMIIBIjANBgkqhkiG9w0B 8 | AQEFAAOCAQ8AMIIBCgKCAQEA5xOONxJJ8b8Qauvob5/7dPYZfIcd+uhAWL2ZlTPz 9 | Qvu4oF0QI4iYgP5iGgry9zEtCM+YQS8UhiAlPlqa6ANxgiBSEyMHH/xE8lo/+caY 10 | GeACqy640Jpl/JocFGo3xd1L8DCawjlaj6eu7T7T/tpAV2qq13b5710eNRbCAfFe 11 | 8yALiGQemx0IYhlZXNbIGWLBNhBhvVjJh7UvOqpADk4xtl8o5j0xgMIRg6WJGK6c 12 | 6ffSIg4eP1XmovNYZ9LLEJG68tF0Q/yIN43B4dt1oq4jzSdCbG4F1EiykT2TmwPV 13 | YDi8tml6DfOCDGnit8svnMEmBv/fcPd31GSbXjF8M+KGGQIDAQABo2swaTAJBgNV 14 | HRMEAjAAMAsGA1UdDwQEAwIF4DBPBgNVHREESDBGghAqLnRlc3QuZ29vZ2xlLmZy 15 | ghh3YXRlcnpvb2kudGVzdC5nb29nbGUuYmWCEioudGVzdC55b3V0dWJlLmNvbYcE 16 | wKgBAzANBgkqhkiG9w0BAQsFAAOCAQEAS8hDQA8PSgipgAml7Q3/djwQ644ghWQv 17 | C2Kb+r30RCY1EyKNhnQnIIh/OUbBZvh0M0iYsy6xqXgfDhCB93AA6j0i5cS8fkhH 18 | Jl4RK0tSkGQ3YNY4NzXwQP/vmUgfkw8VBAZ4Y4GKxppdATjffIW+srbAmdDruIRM 19 | wPeikgOoRrXf0LA1fi4TqxARzeRwenQpayNfGHTvVF9aJkl8HoaMunTAdG5pIVcr 20 | 9GKi/gEMpXUJbbVv3U5frX1Wo4CFo+rZWJ/LyCMeb0jciNLxSdMwj/E/ZuExlyeZ 21 | gc9ctPjSMvgSyXEKv6Vwobleeg88V2ZgzenziORoWj4KszG/lbQZvg== 22 | -----END CERTIFICATE----- 23 | -------------------------------------------------------------------------------- /examples/data/data.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | // Package data provides convenience routines to access files in the data 19 | // directory. 20 | package data 21 | 22 | import ( 23 | "path/filepath" 24 | "runtime" 25 | ) 26 | 27 | // basepath is the root directory of this package. 28 | var basepath string 29 | 30 | func init() { 31 | _, currentFile, _, _ := runtime.Caller(0) 32 | basepath = filepath.Dir(currentFile) 33 | } 34 | 35 | // Path returns the absolute path the given relative file or directory path, 36 | // relative to the google.golang.org/grpc/examples/data directory in the 37 | // user's GOPATH. If rel is already absolute, it is returned unmodified. 38 | func Path(rel string) string { 39 | if filepath.IsAbs(rel) { 40 | return rel 41 | } 42 | 43 | return filepath.Join(basepath, rel) 44 | } 45 | -------------------------------------------------------------------------------- /internal/channelz/types_nonlinux.go: -------------------------------------------------------------------------------- 1 | //go:build !linux 2 | // +build !linux 3 | 4 | /* 5 | * 6 | * Copyright 2018 gRPC authors. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | */ 21 | 22 | package channelz 23 | 24 | import ( 25 | "sync" 26 | ) 27 | 28 | var once sync.Once 29 | 30 | // SocketOptionData defines the struct to hold socket option data, and related 31 | // getter function to obtain info from fd. 32 | // Windows OS doesn't support Socket Option 33 | type SocketOptionData struct { 34 | } 35 | 36 | // Getsockopt defines the function to get socket options requested by channelz. 37 | // It is to be passed to syscall.RawConn.Control(). 38 | // Windows OS doesn't support Socket Option 39 | func (s *SocketOptionData) Getsockopt(fd uintptr) { 40 | once.Do(func() { 41 | logger.Warning("Channelz: socket options are not supported on non-linux environments") 42 | }) 43 | } 44 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/testdata.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | // Package testdata contains functionality to find data files in tests. 19 | package testdata 20 | 21 | import ( 22 | "path/filepath" 23 | "runtime" 24 | ) 25 | 26 | // basepath is the root directory of this package. 27 | var basepath string 28 | 29 | func init() { 30 | _, currentFile, _, _ := runtime.Caller(0) 31 | basepath = filepath.Dir(currentFile) 32 | } 33 | 34 | // Path returns the absolute path the given relative file or directory path, 35 | // relative to the google.golang.org/grpc/testdata directory in the user's GOPATH. 36 | // If rel is already absolute, it is returned unmodified. 37 | func Path(rel string) string { 38 | if filepath.IsAbs(rel) { 39 | return rel 40 | } 41 | 42 | return filepath.Join(basepath, rel) 43 | } 44 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | This page lists all active maintainers of this repository. If you were a 2 | maintainer and would like to add your name to the Emeritus list, please send us a 3 | PR. 4 | 5 | See [GOVERNANCE.md](https://github.com/grpc/grpc-community/blob/master/governance.md) 6 | for governance guidelines and how to become a maintainer. 7 | See [CONTRIBUTING.md](https://github.com/grpc/grpc-community/blob/master/CONTRIBUTING.md) 8 | for general contribution guidelines. 9 | 10 | ## Maintainers (in alphabetical order) 11 | 12 | - [cesarghali](https://github.com/cesarghali), Google LLC 13 | - [dfawley](https://github.com/dfawley), Google LLC 14 | - [easwars](https://github.com/easwars), Google LLC 15 | - [menghanl](https://github.com/menghanl), Google LLC 16 | - [srini100](https://github.com/srini100), Google LLC 17 | 18 | ## Emeritus Maintainers (in alphabetical order) 19 | - [adelez](https://github.com/adelez), Google LLC 20 | - [canguler](https://github.com/canguler), Google LLC 21 | - [iamqizhao](https://github.com/iamqizhao), Google LLC 22 | - [jadekler](https://github.com/jadekler), Google LLC 23 | - [jtattermusch](https://github.com/jtattermusch), Google LLC 24 | - [lyuxuan](https://github.com/lyuxuan), Google LLC 25 | - [makmukhi](https://github.com/makmukhi), Google LLC 26 | - [matt-kwong](https://github.com/matt-kwong), Google LLC 27 | - [nicolasnoble](https://github.com/nicolasnoble), Google LLC 28 | - [yongni](https://github.com/yongni), Google LLC 29 | -------------------------------------------------------------------------------- /xds/internal/balancer/balancer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package balancer installs all the xds balancers. 20 | package balancer 21 | 22 | import ( 23 | _ "google.golang.org/grpc/balancer/weightedtarget" // Register the weighted_target balancer 24 | _ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer" // Register the CDS balancer 25 | _ "google.golang.org/grpc/xds/internal/balancer/clusterimpl" // Register the xds_cluster_impl balancer 26 | _ "google.golang.org/grpc/xds/internal/balancer/clustermanager" // Register the xds_cluster_manager balancer 27 | _ "google.golang.org/grpc/xds/internal/balancer/clusterresolver" // Register the xds_cluster_resolver balancer 28 | _ "google.golang.org/grpc/xds/internal/balancer/priority" // Register the priority balancer 29 | ) 30 | -------------------------------------------------------------------------------- /xds/internal/balancer/clustermanager/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package clustermanager 20 | 21 | import ( 22 | "encoding/json" 23 | 24 | internalserviceconfig "google.golang.org/grpc/internal/serviceconfig" 25 | "google.golang.org/grpc/serviceconfig" 26 | ) 27 | 28 | type childConfig struct { 29 | // ChildPolicy is the child policy and it's config. 30 | ChildPolicy *internalserviceconfig.BalancerConfig 31 | } 32 | 33 | // lbConfig is the balancer config for xds routing policy. 34 | type lbConfig struct { 35 | serviceconfig.LoadBalancingConfig 36 | Children map[string]childConfig 37 | } 38 | 39 | func parseConfig(c json.RawMessage) (*lbConfig, error) { 40 | cfg := &lbConfig{} 41 | if err := json.Unmarshal(c, cfg); err != nil { 42 | return nil, err 43 | } 44 | 45 | return cfg, nil 46 | } 47 | -------------------------------------------------------------------------------- /serviceconfig/serviceconfig.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package serviceconfig defines types and methods for operating on gRPC 20 | // service configs. 21 | // 22 | // Experimental 23 | // 24 | // Notice: This package is EXPERIMENTAL and may be changed or removed in a 25 | // later release. 26 | package serviceconfig 27 | 28 | // Config represents an opaque data structure holding a service config. 29 | type Config interface { 30 | isServiceConfig() 31 | } 32 | 33 | // LoadBalancingConfig represents an opaque data structure holding a load 34 | // balancing config. 35 | type LoadBalancingConfig interface { 36 | isLoadBalancingConfig() 37 | } 38 | 39 | // ParseResult contains a service config or an error. Exactly one must be 40 | // non-nil. 41 | type ParseResult struct { 42 | Config Config 43 | Err error 44 | } 45 | -------------------------------------------------------------------------------- /internal/balancerload/load.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Package balancerload defines APIs to parse server loads in trailers. The 18 | // parsed loads are sent to balancers in DoneInfo. 19 | package balancerload 20 | 21 | import ( 22 | "google.golang.org/grpc/metadata" 23 | ) 24 | 25 | // Parser converts loads from metadata into a concrete type. 26 | type Parser interface { 27 | // Parse parses loads from metadata. 28 | Parse(md metadata.MD) interface{} 29 | } 30 | 31 | var parser Parser 32 | 33 | // SetParser sets the load parser. 34 | // 35 | // Not mutex-protected, should be called before any gRPC functions. 36 | func SetParser(lr Parser) { 37 | parser = lr 38 | } 39 | 40 | // Parse calls parser.Read(). 41 | func Parse(md metadata.MD) interface{} { 42 | if parser == nil { 43 | return nil 44 | } 45 | return parser.Parse(md) 46 | } 47 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/server_cert_3.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDyTCCArGgAwIBAgIUeoNdEiqhXVkpcYsmHaKiVS5W/tQwDQYJKoZIhvcNAQEL 3 | BQAwPjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMREwDwYDVQQHDAhTYW4gSm9z 4 | ZTEPMA0GA1UECgwGR29vZ2xlMB4XDTIwMDcxNjE2NTMxOVoXDTQwMDcxMTE2NTMx 5 | OVowgZMxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTERMA8GA1UEBwwIU2FuIEpv 6 | c2UxEjAQBgNVBAoMCUVuZCBQb2ludDEOMAwGA1UECwwFSW5mcmExIjAgBgkqhkiG 7 | 9w0BCQEWE2NpbmR5eHVlQGdvb2dsZS5jb20xHDAaBgNVBAMME2Zvby5iYXIuc2Vy 8 | dmVyMy5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDcIyJEt3ZA 9 | xPn7H5f/IwXS8NcoAXWP8L6rWndcg+EWayx7W+wmUsFKGSFGzrFPPCFmKO8MrQqp 10 | 8LSAxHAVtOC6Uw+INWJJw9BRlx2nvV7hfbqu3OnPkPVkN/siUQCqnEKJQHliNT9X 11 | Dl4/Mav75uQSWb3Vfi3KtG7mzPFNNbbe4yfHyGbC4e9RtKkGimDSJ413s3m4+scD 12 | vtpCcCXj9XXZNdCwD1CL3kNdmOdhgfkDBP+AMLBFKZKqpCo6m0s4JJTiej13dc27 13 | wTrnkFm1CP77SV+kQlWg5DAcVXYJkN9FqNqExqIPS/SxMk7H+3qSQACbttQK9UmC 14 | n3qR3pGbwqzNAgMBAAGjaTBnMB8GA1UdIwQYMBaAFG4bi8k0dOd7jSpPQQ6YUDAU 15 | ARaxMAkGA1UdEwQCMAAwCwYDVR0PBAQDAgTwMCwGA1UdEQQlMCOCCmdvb2dsZS5j 16 | b22CCWFwcGxlLmNvbYIKYW1hem9uLmNvbTANBgkqhkiG9w0BAQsFAAOCAQEAn5aW 17 | HEHNTDmcgC25oEtCj+IkoAslgFze4ZqkSz0HCzx76vj3AfMmIEvqB0W74wKqeZgm 18 | V0D7I0xHkM3ILH4RjoCotpol3nLooIPFflA6Z1ILTRZl8mE5kfBSHzKdPS0egOf6 19 | kgrNYgJjBEtGNsmq8RKxAHVVAPgH88di0JnQDN5LcV9ZBKTQM2R7EM6a8eWD/Jsi 20 | uujbNtdNERssSBV+Oil93MbsEcOT1RSKKxAiVvkHkR+45GRB889xBnqelcDVqcMK 21 | Vcdp6X7aD5/Bu/4fq9AZlcHSEQDixNtjp/pQR0B5FsCGrb5OAz0B2t9jykDiIyj4 22 | 4lxhQz8ykXf7ue0/ag== 23 | -----END CERTIFICATE----- 24 | -------------------------------------------------------------------------------- /trace_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpc 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func (s) TestMethodFamily(t *testing.T) { 26 | cases := []struct { 27 | desc string 28 | method string 29 | wantMethodFamily string 30 | }{ 31 | { 32 | desc: "No leading slash", 33 | method: "pkg.service/method", 34 | wantMethodFamily: "pkg.service", 35 | }, 36 | { 37 | desc: "Leading slash", 38 | method: "/pkg.service/method", 39 | wantMethodFamily: "pkg.service", 40 | }, 41 | } 42 | 43 | for _, ut := range cases { 44 | t.Run(ut.desc, func(t *testing.T) { 45 | if got := methodFamily(ut.method); got != ut.wantMethodFamily { 46 | t.Fatalf("methodFamily(%s) = %s, want %s", ut.method, got, ut.wantMethodFamily) 47 | } 48 | }) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /profiling/profiling.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package profiling exposes methods to manage profiling within gRPC. 20 | // 21 | // Experimental 22 | // 23 | // Notice: This package is EXPERIMENTAL and may be changed or removed in a 24 | // later release. 25 | package profiling 26 | 27 | import ( 28 | internal "google.golang.org/grpc/internal/profiling" 29 | ) 30 | 31 | // Enable turns profiling on and off. This operation is safe for concurrent 32 | // access from different goroutines. 33 | // 34 | // Note that this is the only operation that's accessible through the publicly 35 | // exposed profiling package. Everything else (such as retrieving stats) must 36 | // be done through the profiling service. This is allowed so that users can use 37 | // heuristics to turn profiling on and off automatically. 38 | func Enable(enabled bool) { 39 | internal.Enable(enabled) 40 | } 41 | -------------------------------------------------------------------------------- /profiling/cmd/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Binary cmd is a command-line tool for profiling management. It retrieves and 20 | // processes data from the profiling service. 21 | package main 22 | 23 | import ( 24 | "os" 25 | 26 | "google.golang.org/grpc/grpclog" 27 | ppb "google.golang.org/grpc/profiling/proto" 28 | ) 29 | 30 | var logger = grpclog.Component("profiling") 31 | 32 | type snapshot struct { 33 | StreamStats []*ppb.Stat 34 | } 35 | 36 | func main() { 37 | if err := parseArgs(); err != nil { 38 | logger.Errorf("error parsing flags: %v", err) 39 | os.Exit(1) 40 | } 41 | 42 | if *flagAddress != "" { 43 | if err := remoteCommand(); err != nil { 44 | logger.Errorf("error: %v", err) 45 | os.Exit(1) 46 | } 47 | } else { 48 | if err := localCommand(); err != nil { 49 | logger.Errorf("error: %v", err) 50 | os.Exit(1) 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /examples/features/name_resolving/README.md: -------------------------------------------------------------------------------- 1 | # Name resolving 2 | 3 | This examples shows how `ClientConn` can pick different name resolvers. 4 | 5 | ## What is a name resolver 6 | 7 | A name resolver can be seen as a `map[service-name][]backend-ip`. It takes a 8 | service name, and returns a list of IPs of the backends. A common used name 9 | resolver is DNS. 10 | 11 | In this example, a resolver is created to resolve `resolver.example.grpc.io` to 12 | `localhost:50051`. 13 | 14 | ## Try it 15 | 16 | ``` 17 | go run server/main.go 18 | ``` 19 | 20 | ``` 21 | go run client/main.go 22 | ``` 23 | 24 | ## Explanation 25 | 26 | The echo server is serving on ":50051". Two clients are created, one is dialing 27 | to `passthrough:///localhost:50051`, while the other is dialing to 28 | `example:///resolver.example.grpc.io`. Both of them can connect the server. 29 | 30 | Name resolver is picked based on the `scheme` in the target string. See 31 | https://github.com/grpc/grpc/blob/master/doc/naming.md for the target syntax. 32 | 33 | The first client picks the `passthrough` resolver, which takes the input, and 34 | use it as the backend addresses. 35 | 36 | The second is connecting to service name `resolver.example.grpc.io`. Without a 37 | proper name resolver, this would fail. In the example it picks the `example` 38 | resolver that we installed. The `example` resolver can handle 39 | `resolver.example.grpc.io` correctly by returning the backend address. So even 40 | though the backend IP is not set when ClientConn is created, the connection will 41 | be created to the correct backend. -------------------------------------------------------------------------------- /internal/credentials/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package credentials 20 | 21 | import ( 22 | "crypto/tls" 23 | ) 24 | 25 | const alpnProtoStrH2 = "h2" 26 | 27 | // AppendH2ToNextProtos appends h2 to next protos. 28 | func AppendH2ToNextProtos(ps []string) []string { 29 | for _, p := range ps { 30 | if p == alpnProtoStrH2 { 31 | return ps 32 | } 33 | } 34 | ret := make([]string, 0, len(ps)+1) 35 | ret = append(ret, ps...) 36 | return append(ret, alpnProtoStrH2) 37 | } 38 | 39 | // CloneTLSConfig returns a shallow clone of the exported 40 | // fields of cfg, ignoring the unexported sync.Once, which 41 | // contains a mutex and must not be copied. 42 | // 43 | // If cfg is nil, a new zero tls.Config is returned. 44 | // 45 | // TODO: inline this function if possible. 46 | func CloneTLSConfig(cfg *tls.Config) *tls.Config { 47 | if cfg == nil { 48 | return &tls.Config{} 49 | } 50 | 51 | return cfg.Clone() 52 | } 53 | -------------------------------------------------------------------------------- /internal/grpcutil/encode_duration_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpcutil 20 | 21 | import ( 22 | "testing" 23 | "time" 24 | ) 25 | 26 | func TestEncodeDuration(t *testing.T) { 27 | for _, test := range []struct { 28 | in string 29 | out string 30 | }{ 31 | {"12345678ns", "12345678n"}, 32 | {"123456789ns", "123457u"}, 33 | {"12345678us", "12345678u"}, 34 | {"123456789us", "123457m"}, 35 | {"12345678ms", "12345678m"}, 36 | {"123456789ms", "123457S"}, 37 | {"12345678s", "12345678S"}, 38 | {"123456789s", "2057614M"}, 39 | {"12345678m", "12345678M"}, 40 | {"123456789m", "2057614H"}, 41 | } { 42 | d, err := time.ParseDuration(test.in) 43 | if err != nil { 44 | t.Fatalf("failed to parse duration string %s: %v", test.in, err) 45 | } 46 | out := EncodeDuration(d) 47 | if out != test.out { 48 | t.Fatalf("timeoutEncode(%s) = %s, want %s", test.in, out, test.out) 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/xdsresource/test_utils_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package xdsresource 19 | 20 | import ( 21 | "testing" 22 | "time" 23 | 24 | "github.com/google/go-cmp/cmp" 25 | "github.com/google/go-cmp/cmp/cmpopts" 26 | "google.golang.org/grpc/internal/grpctest" 27 | "google.golang.org/protobuf/testing/protocmp" 28 | ) 29 | 30 | type s struct { 31 | grpctest.Tester 32 | } 33 | 34 | func Test(t *testing.T) { 35 | grpctest.RunSubTests(t, s{}) 36 | } 37 | 38 | var ( 39 | cmpOpts = cmp.Options{ 40 | cmpopts.EquateEmpty(), 41 | cmp.FilterValues(func(x, y error) bool { return true }, cmpopts.EquateErrors()), 42 | cmp.Comparer(func(a, b time.Time) bool { return true }), 43 | protocmp.Transform(), 44 | } 45 | 46 | cmpOptsIgnoreDetails = cmp.Options{ 47 | cmp.Comparer(func(a, b time.Time) bool { return true }), 48 | cmp.Comparer(func(x, y error) bool { 49 | return (x == nil) == (y == nil) 50 | }), 51 | } 52 | ) 53 | -------------------------------------------------------------------------------- /internal/grpctest/example_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpctest_test 20 | 21 | import ( 22 | "testing" 23 | 24 | "google.golang.org/grpc/internal/grpctest" 25 | ) 26 | 27 | type s struct { 28 | i int 29 | } 30 | 31 | func (s *s) Setup(t *testing.T) { 32 | t.Log("Per-test setup code") 33 | s.i = 5 34 | } 35 | 36 | func (s *s) TestSomething(t *testing.T) { 37 | t.Log("TestSomething") 38 | if s.i != 5 { 39 | t.Errorf("s.i = %v; want 5", s.i) 40 | } 41 | s.i = 3 42 | } 43 | 44 | func (s *s) TestSomethingElse(t *testing.T) { 45 | t.Log("TestSomethingElse") 46 | if got, want := s.i%4, 1; got != want { 47 | t.Errorf("s.i %% 4 = %v; want %v", got, want) 48 | } 49 | s.i = 3 50 | } 51 | 52 | func (s *s) Teardown(t *testing.T) { 53 | t.Log("Per-test teardown code") 54 | if s.i != 3 { 55 | t.Fatalf("s.i = %v; want 3", s.i) 56 | } 57 | } 58 | 59 | func TestExample(t *testing.T) { 60 | grpctest.RunSubTests(t, &s{}) 61 | } 62 | -------------------------------------------------------------------------------- /internal/xds_handshake_cluster.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package internal 18 | 19 | import ( 20 | "google.golang.org/grpc/attributes" 21 | "google.golang.org/grpc/resolver" 22 | ) 23 | 24 | // handshakeClusterNameKey is the type used as the key to store cluster name in 25 | // the Attributes field of resolver.Address. 26 | type handshakeClusterNameKey struct{} 27 | 28 | // SetXDSHandshakeClusterName returns a copy of addr in which the Attributes field 29 | // is updated with the cluster name. 30 | func SetXDSHandshakeClusterName(addr resolver.Address, clusterName string) resolver.Address { 31 | addr.Attributes = addr.Attributes.WithValue(handshakeClusterNameKey{}, clusterName) 32 | return addr 33 | } 34 | 35 | // GetXDSHandshakeClusterName returns cluster name stored in attr. 36 | func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) { 37 | v := attr.Value(handshakeClusterNameKey{}) 38 | name, ok := v.(string) 39 | return name, ok 40 | } 41 | -------------------------------------------------------------------------------- /internal/credentials/util_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package credentials 20 | 21 | import ( 22 | "reflect" 23 | "testing" 24 | ) 25 | 26 | func (s) TestAppendH2ToNextProtos(t *testing.T) { 27 | tests := []struct { 28 | name string 29 | ps []string 30 | want []string 31 | }{ 32 | { 33 | name: "empty", 34 | ps: nil, 35 | want: []string{"h2"}, 36 | }, 37 | { 38 | name: "only h2", 39 | ps: []string{"h2"}, 40 | want: []string{"h2"}, 41 | }, 42 | { 43 | name: "with h2", 44 | ps: []string{"alpn", "h2"}, 45 | want: []string{"alpn", "h2"}, 46 | }, 47 | { 48 | name: "no h2", 49 | ps: []string{"alpn"}, 50 | want: []string{"alpn", "h2"}, 51 | }, 52 | } 53 | for _, tt := range tests { 54 | t.Run(tt.name, func(t *testing.T) { 55 | if got := AppendH2ToNextProtos(tt.ps); !reflect.DeepEqual(got, tt.want) { 56 | t.Errorf("AppendH2ToNextProtos() = %v, want %v", got, tt.want) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /interop/xds/client/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright 2021 gRPC authors. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Dockerfile for building the xDS interop client. To build the image, run the 16 | # following command from grpc-go directory: 17 | # docker build -t -f interop/xds/client/Dockerfile . 18 | 19 | FROM golang:1.16-alpine as build 20 | 21 | # Make a grpc-go directory and copy the repo into it. 22 | WORKDIR /go/src/grpc-go 23 | COPY . . 24 | 25 | # Build a static binary without cgo so that we can copy just the binary in the 26 | # final image, and can get rid of Go compiler and gRPC-Go dependencies. 27 | RUN go build -tags osusergo,netgo interop/xds/client/client.go 28 | 29 | # Second stage of the build which copies over only the client binary and skips 30 | # the Go compiler and gRPC repo from the earlier stage. This significantly 31 | # reduces the docker image size. 32 | FROM alpine 33 | COPY --from=build /go/src/grpc-go/client . 34 | ENV GRPC_GO_LOG_VERBOSITY_LEVEL=2 35 | ENV GRPC_GO_LOG_SEVERITY_LEVEL="info" 36 | ENV GRPC_GO_LOG_FORMATTER="json" 37 | ENTRYPOINT ["./client"] 38 | -------------------------------------------------------------------------------- /examples/features/proto/echo/echo.proto: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | syntax = "proto3"; 20 | 21 | option go_package = "google.golang.org/grpc/examples/features/proto/echo"; 22 | 23 | package grpc.examples.echo; 24 | 25 | // EchoRequest is the request for echo. 26 | message EchoRequest { 27 | string message = 1; 28 | } 29 | 30 | // EchoResponse is the response for echo. 31 | message EchoResponse { 32 | string message = 1; 33 | } 34 | 35 | // Echo is the echo service. 36 | service Echo { 37 | // UnaryEcho is unary echo. 38 | rpc UnaryEcho(EchoRequest) returns (EchoResponse) {} 39 | // ServerStreamingEcho is server side streaming. 40 | rpc ServerStreamingEcho(EchoRequest) returns (stream EchoResponse) {} 41 | // ClientStreamingEcho is client side streaming. 42 | rpc ClientStreamingEcho(stream EchoRequest) returns (EchoResponse) {} 43 | // BidirectionalStreamingEcho is bidi streaming. 44 | rpc BidirectionalStreamingEcho(stream EchoRequest) returns (stream EchoResponse) {} 45 | } 46 | -------------------------------------------------------------------------------- /internal/testutils/xds/e2e/logging.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2022 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package e2e 20 | 21 | import ( 22 | "fmt" 23 | 24 | "google.golang.org/grpc/grpclog" 25 | ) 26 | 27 | var logger = grpclog.Component("xds-e2e") 28 | 29 | // serverLogger implements the Logger interface defined at 30 | // envoyproxy/go-control-plane/pkg/log. This is passed to the Snapshot cache. 31 | type serverLogger struct{} 32 | 33 | func (l serverLogger) Debugf(format string, args ...interface{}) { 34 | msg := fmt.Sprintf(format, args...) 35 | logger.InfoDepth(1, msg) 36 | } 37 | func (l serverLogger) Infof(format string, args ...interface{}) { 38 | msg := fmt.Sprintf(format, args...) 39 | logger.InfoDepth(1, msg) 40 | } 41 | func (l serverLogger) Warnf(format string, args ...interface{}) { 42 | msg := fmt.Sprintf(format, args...) 43 | logger.WarningDepth(1, msg) 44 | } 45 | func (l serverLogger) Errorf(format string, args ...interface{}) { 46 | msg := fmt.Sprintf(format, args...) 47 | logger.ErrorDepth(1, msg) 48 | } 49 | -------------------------------------------------------------------------------- /interop/xds/server/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright 2021 gRPC authors. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Dockerfile for building the xDS interop server. To build the image, run the 16 | # following command from grpc-go directory: 17 | # docker build -t -f interop/xds/server/Dockerfile . 18 | 19 | FROM golang:1.16-alpine as build 20 | 21 | # Make a grpc-go directory and copy the repo into it. 22 | WORKDIR /go/src/grpc-go 23 | COPY . . 24 | 25 | # Build a static binary without cgo so that we can copy just the binary in the 26 | # final image, and can get rid of the Go compiler and gRPC-Go dependencies. 27 | RUN go build -tags osusergo,netgo interop/xds/server/server.go 28 | 29 | # Second stage of the build which copies over only the client binary and skips 30 | # the Go compiler and gRPC repo from the earlier stage. This significantly 31 | # reduces the docker image size. 32 | FROM alpine 33 | COPY --from=build /go/src/grpc-go/server . 34 | ENV GRPC_GO_LOG_VERBOSITY_LEVEL=2 35 | ENV GRPC_GO_LOG_SEVERITY_LEVEL="info" 36 | ENV GRPC_GO_LOG_FORMATTER="json" 37 | ENTRYPOINT ["./server"] 38 | -------------------------------------------------------------------------------- /examples/features/name_resolving/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Binary server is an example server. 20 | package main 21 | 22 | import ( 23 | "context" 24 | "fmt" 25 | "log" 26 | "net" 27 | 28 | "google.golang.org/grpc" 29 | 30 | pb "google.golang.org/grpc/examples/features/proto/echo" 31 | ) 32 | 33 | const addr = "localhost:50051" 34 | 35 | type ecServer struct { 36 | pb.UnimplementedEchoServer 37 | addr string 38 | } 39 | 40 | func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) { 41 | return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil 42 | } 43 | 44 | func main() { 45 | lis, err := net.Listen("tcp", addr) 46 | if err != nil { 47 | log.Fatalf("failed to listen: %v", err) 48 | } 49 | s := grpc.NewServer() 50 | pb.RegisterEchoServer(s, &ecServer{addr: addr}) 51 | log.Printf("serving on %s\n", addr) 52 | if err := s.Serve(lis); err != nil { 53 | log.Fatalf("failed to serve: %v", err) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /internal/transport/networktype/networktype.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package networktype declares the network type to be used in the default 20 | // dialer. Attribute of a resolver.Address. 21 | package networktype 22 | 23 | import ( 24 | "google.golang.org/grpc/resolver" 25 | ) 26 | 27 | // keyType is the key to use for storing State in Attributes. 28 | type keyType string 29 | 30 | const key = keyType("grpc.internal.transport.networktype") 31 | 32 | // Set returns a copy of the provided address with attributes containing networkType. 33 | func Set(address resolver.Address, networkType string) resolver.Address { 34 | address.Attributes = address.Attributes.WithValue(key, networkType) 35 | return address 36 | } 37 | 38 | // Get returns the network type in the resolver.Address and true, or "", false 39 | // if not present. 40 | func Get(address resolver.Address) (string, bool) { 41 | v := address.Attributes.Value(key) 42 | if v == nil { 43 | return "", false 44 | } 45 | return v.(string), true 46 | } 47 | -------------------------------------------------------------------------------- /internal/googlecloud/manufacturer_windows.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2022 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package googlecloud 20 | 21 | import ( 22 | "errors" 23 | "os/exec" 24 | "regexp" 25 | "strings" 26 | ) 27 | 28 | const ( 29 | windowsCheckCommand = "powershell.exe" 30 | windowsCheckCommandArgs = "Get-WmiObject -Class Win32_BIOS" 31 | powershellOutputFilter = "Manufacturer" 32 | windowsManufacturerRegex = ":(.*)" 33 | ) 34 | 35 | func manufacturer() ([]byte, error) { 36 | cmd := exec.Command(windowsCheckCommand, windowsCheckCommandArgs) 37 | out, err := cmd.Output() 38 | if err != nil { 39 | return nil, err 40 | } 41 | for _, line := range strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") { 42 | if strings.HasPrefix(line, powershellOutputFilter) { 43 | re := regexp.MustCompile(windowsManufacturerRegex) 44 | name := re.FindString(line) 45 | name = strings.TrimLeft(name, ":") 46 | return []byte(name), nil 47 | } 48 | } 49 | return nil, errors.New("cannot determine the machine's manufacturer") 50 | } 51 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/bootstrap/template.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package bootstrap 19 | 20 | import ( 21 | "net/url" 22 | "strings" 23 | ) 24 | 25 | // PopulateResourceTemplate populates the given template using the target 26 | // string. "%s", if exists in the template, will be replaced with target. 27 | // 28 | // If the template starts with "xdstp:", the replaced string will be %-encoded. 29 | // But note that "/" is not percent encoded. 30 | func PopulateResourceTemplate(template, target string) string { 31 | if !strings.Contains(template, "%s") { 32 | return template 33 | } 34 | if strings.HasPrefix(template, "xdstp:") { 35 | target = percentEncode(target) 36 | } 37 | return strings.Replace(template, "%s", target, -1) 38 | } 39 | 40 | // percentEncode percent encode t, except for "/". See the tests for examples. 41 | func percentEncode(t string) string { 42 | segs := strings.Split(t, "/") 43 | for i := range segs { 44 | segs[i] = url.PathEscape(segs[i]) 45 | } 46 | return strings.Join(segs, "/") 47 | } 48 | -------------------------------------------------------------------------------- /xds/internal/balancer/priority/utils_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package priority 20 | 21 | import "testing" 22 | 23 | func TestCompareStringSlice(t *testing.T) { 24 | tests := []struct { 25 | name string 26 | a []string 27 | b []string 28 | want bool 29 | }{ 30 | { 31 | name: "equal", 32 | a: []string{"a", "b"}, 33 | b: []string{"a", "b"}, 34 | want: true, 35 | }, 36 | { 37 | name: "not equal", 38 | a: []string{"a", "b"}, 39 | b: []string{"a", "b", "c"}, 40 | want: false, 41 | }, 42 | { 43 | name: "both empty", 44 | a: nil, 45 | b: nil, 46 | want: true, 47 | }, 48 | { 49 | name: "one empty", 50 | a: []string{"a", "b"}, 51 | b: nil, 52 | want: false, 53 | }, 54 | } 55 | for _, tt := range tests { 56 | t.Run(tt.name, func(t *testing.T) { 57 | if got := equalStringSlice(tt.a, tt.b); got != tt.want { 58 | t.Errorf("equalStringSlice(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /balancer/weightedtarget/weightedtarget_config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2020 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package weightedtarget 20 | 21 | import ( 22 | "encoding/json" 23 | 24 | internalserviceconfig "google.golang.org/grpc/internal/serviceconfig" 25 | "google.golang.org/grpc/serviceconfig" 26 | ) 27 | 28 | // Target represents one target with the weight and the child policy. 29 | type Target struct { 30 | // Weight is the weight of the child policy. 31 | Weight uint32 `json:"weight,omitempty"` 32 | // ChildPolicy is the child policy and it's config. 33 | ChildPolicy *internalserviceconfig.BalancerConfig `json:"childPolicy,omitempty"` 34 | } 35 | 36 | // LBConfig is the balancer config for weighted_target. 37 | type LBConfig struct { 38 | serviceconfig.LoadBalancingConfig `json:"-"` 39 | 40 | Targets map[string]Target `json:"targets,omitempty"` 41 | } 42 | 43 | func parseConfig(c json.RawMessage) (*LBConfig, error) { 44 | var cfg LBConfig 45 | if err := json.Unmarshal(c, &cfg); err != nil { 46 | return nil, err 47 | } 48 | return &cfg, nil 49 | } 50 | -------------------------------------------------------------------------------- /credentials/alts/internal/conn/counter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package conn 20 | 21 | import ( 22 | "errors" 23 | ) 24 | 25 | const counterLen = 12 26 | 27 | var ( 28 | errInvalidCounter = errors.New("invalid counter") 29 | ) 30 | 31 | // Counter is a 96-bit, little-endian counter. 32 | type Counter struct { 33 | value [counterLen]byte 34 | invalid bool 35 | overflowLen int 36 | } 37 | 38 | // Value returns the current value of the counter as a byte slice. 39 | func (c *Counter) Value() ([]byte, error) { 40 | if c.invalid { 41 | return nil, errInvalidCounter 42 | } 43 | return c.value[:], nil 44 | } 45 | 46 | // Inc increments the counter and checks for overflow. 47 | func (c *Counter) Inc() { 48 | // If the counter is already invalid, there is no need to increase it. 49 | if c.invalid { 50 | return 51 | } 52 | i := 0 53 | for ; i < c.overflowLen; i++ { 54 | c.value[i]++ 55 | if c.value[i] != 0 { 56 | break 57 | } 58 | } 59 | if i == c.overflowLen { 60 | c.invalid = true 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /xds/internal/xdsclient/clientimpl_loadreport.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package xdsclient 19 | 20 | import ( 21 | "google.golang.org/grpc/xds/internal/xdsclient/bootstrap" 22 | "google.golang.org/grpc/xds/internal/xdsclient/load" 23 | ) 24 | 25 | // ReportLoad starts a load reporting stream to the given server. All load 26 | // reports to the same server share the LRS stream. 27 | // 28 | // It returns a Store for the user to report loads, a function to cancel the 29 | // load reporting stream. 30 | func (c *clientImpl) ReportLoad(server *bootstrap.ServerConfig) (*load.Store, func()) { 31 | c.authorityMu.Lock() 32 | a, err := c.newAuthorityLocked(server) 33 | c.authorityMu.Unlock() 34 | if err != nil { 35 | c.logger.Infof("xds: failed to connect to the control plane to do load reporting for authority %q: %v", server, err) 36 | return nil, func() {} 37 | } 38 | // Hold the ref before starting load reporting. 39 | a.ref() 40 | store, cancelF := a.reportLoad() 41 | return store, func() { 42 | cancelF() 43 | c.unrefAuthority(a) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /xds/internal/balancer/ringhash/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package ringhash 20 | 21 | import ( 22 | "encoding/json" 23 | "fmt" 24 | 25 | "google.golang.org/grpc/serviceconfig" 26 | ) 27 | 28 | // LBConfig is the balancer config for ring_hash balancer. 29 | type LBConfig struct { 30 | serviceconfig.LoadBalancingConfig `json:"-"` 31 | 32 | MinRingSize uint64 `json:"minRingSize,omitempty"` 33 | MaxRingSize uint64 `json:"maxRingSize,omitempty"` 34 | } 35 | 36 | const ( 37 | defaultMinSize = 1024 38 | defaultMaxSize = 8 * 1024 * 1024 // 8M 39 | ) 40 | 41 | func parseConfig(c json.RawMessage) (*LBConfig, error) { 42 | var cfg LBConfig 43 | if err := json.Unmarshal(c, &cfg); err != nil { 44 | return nil, err 45 | } 46 | if cfg.MinRingSize == 0 { 47 | cfg.MinRingSize = defaultMinSize 48 | } 49 | if cfg.MaxRingSize == 0 { 50 | cfg.MaxRingSize = defaultMaxSize 51 | } 52 | if cfg.MinRingSize > cfg.MaxRingSize { 53 | return nil, fmt.Errorf("min %v is greater than max %v", cfg.MinRingSize, cfg.MaxRingSize) 54 | } 55 | return &cfg, nil 56 | } 57 | -------------------------------------------------------------------------------- /peer/peer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2014 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package peer defines various peer information associated with RPCs and 20 | // corresponding utils. 21 | package peer 22 | 23 | import ( 24 | "context" 25 | "net" 26 | 27 | "google.golang.org/grpc/credentials" 28 | ) 29 | 30 | // Peer contains the information of the peer for an RPC, such as the address 31 | // and authentication information. 32 | type Peer struct { 33 | // Addr is the peer address. 34 | Addr net.Addr 35 | // AuthInfo is the authentication information of the transport. 36 | // It is nil if there is no transport security being used. 37 | AuthInfo credentials.AuthInfo 38 | } 39 | 40 | type peerKey struct{} 41 | 42 | // NewContext creates a new context with peer information attached. 43 | func NewContext(ctx context.Context, p *Peer) context.Context { 44 | return context.WithValue(ctx, peerKey{}, p) 45 | } 46 | 47 | // FromContext returns the peer information in ctx if it exists. 48 | func FromContext(ctx context.Context) (p *Peer, ok bool) { 49 | p, ok = ctx.Value(peerKey{}).(*Peer) 50 | return 51 | } 52 | -------------------------------------------------------------------------------- /xds/internal/testutils/testutils.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | // Package testutils provides utility types, for use in xds tests. 19 | package testutils 20 | 21 | import ( 22 | "google.golang.org/grpc/xds/internal/xdsclient/xdsresource" 23 | "google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version" 24 | ) 25 | 26 | // BuildResourceName returns the resource name in the format of an xdstp:// 27 | // resource. 28 | func BuildResourceName(typ xdsresource.ResourceType, auth, id string, ctxParams map[string]string) string { 29 | var typS string 30 | switch typ { 31 | case xdsresource.ListenerResource: 32 | typS = version.V3ListenerType 33 | case xdsresource.RouteConfigResource: 34 | typS = version.V3RouteConfigType 35 | case xdsresource.ClusterResource: 36 | typS = version.V3ClusterType 37 | case xdsresource.EndpointsResource: 38 | typS = version.V3EndpointsType 39 | } 40 | return (&xdsresource.Name{ 41 | Scheme: "xdstp", 42 | Authority: auth, 43 | Type: typS, 44 | ID: id, 45 | ContextParams: ctxParams, 46 | }).String() 47 | } 48 | -------------------------------------------------------------------------------- /internal/credentials/syscallconn_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package credentials 20 | 21 | import ( 22 | "net" 23 | "syscall" 24 | "testing" 25 | ) 26 | 27 | func (*syscallConn) SyscallConn() (syscall.RawConn, error) { 28 | return nil, nil 29 | } 30 | 31 | type nonSyscallConn struct { 32 | net.Conn 33 | } 34 | 35 | func (s) TestWrapSyscallConn(t *testing.T) { 36 | sc := &syscallConn{} 37 | nsc := &nonSyscallConn{} 38 | 39 | wrapConn := WrapSyscallConn(sc, nsc) 40 | if _, ok := wrapConn.(syscall.Conn); !ok { 41 | t.Errorf("returned conn (type %T) doesn't implement syscall.Conn, want implement", wrapConn) 42 | } 43 | } 44 | 45 | func (s) TestWrapSyscallConnNoWrap(t *testing.T) { 46 | nscRaw := &nonSyscallConn{} 47 | nsc := &nonSyscallConn{} 48 | 49 | wrapConn := WrapSyscallConn(nscRaw, nsc) 50 | if _, ok := wrapConn.(syscall.Conn); ok { 51 | t.Errorf("returned conn (type %T) implements syscall.Conn, want not implement", wrapConn) 52 | } 53 | if wrapConn != nsc { 54 | t.Errorf("returned conn is %p, want %p (the passed-in newConn)", wrapConn, nsc) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /internal/binarylog/binarylog_testutil.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // This file contains exported variables/functions that are exported for testing 20 | // only. 21 | // 22 | // An ideal way for this would be to put those in a *_test.go but in binarylog 23 | // package. But this doesn't work with staticcheck with go module. Error was: 24 | // "MdToMetadataProto not declared by package binarylog". This could be caused 25 | // by the way staticcheck looks for files for a certain package, which doesn't 26 | // support *_test.go files. 27 | // 28 | // Move those to binary_test.go when staticcheck is fixed. 29 | 30 | package binarylog 31 | 32 | var ( 33 | // AllLogger is a logger that logs all headers/messages for all RPCs. It's 34 | // for testing only. 35 | AllLogger = NewLoggerFromConfigString("*") 36 | // MdToMetadataProto converts metadata to a binary logging proto message. 37 | // It's for testing only. 38 | MdToMetadataProto = mdToMetadataProto 39 | // AddrToProto converts an address to a binary logging proto message. It's 40 | // for testing only. 41 | AddrToProto = addrToProto 42 | ) 43 | -------------------------------------------------------------------------------- /internal/grpctest/grpctest_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpctest 20 | 21 | import ( 22 | "reflect" 23 | "testing" 24 | ) 25 | 26 | type tRunST struct { 27 | setup, test, teardown bool 28 | } 29 | 30 | func (t *tRunST) Setup(*testing.T) { 31 | t.setup = true 32 | } 33 | func (t *tRunST) TestSubTest(*testing.T) { 34 | t.test = true 35 | } 36 | func (t *tRunST) Teardown(*testing.T) { 37 | t.teardown = true 38 | } 39 | 40 | func TestRunSubTests(t *testing.T) { 41 | x := &tRunST{} 42 | RunSubTests(t, x) 43 | if want := (&tRunST{setup: true, test: true, teardown: true}); !reflect.DeepEqual(x, want) { 44 | t.Fatalf("x = %v; want all fields true", x) 45 | } 46 | } 47 | 48 | type tNoST struct { 49 | test bool 50 | } 51 | 52 | func (t *tNoST) TestSubTest(*testing.T) { 53 | t.test = true 54 | } 55 | 56 | func TestNoSetupOrTeardown(t *testing.T) { 57 | // Ensures nothing panics or fails if Setup/Teardown are omitted. 58 | x := &tNoST{} 59 | RunSubTests(t, x) 60 | if want := (&tNoST{test: true}); !reflect.DeepEqual(x, want) { 61 | t.Fatalf("x = %v; want %v", x, want) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /codes/code_string.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package codes 20 | 21 | import "strconv" 22 | 23 | func (c Code) String() string { 24 | switch c { 25 | case OK: 26 | return "OK" 27 | case Canceled: 28 | return "Canceled" 29 | case Unknown: 30 | return "Unknown" 31 | case InvalidArgument: 32 | return "InvalidArgument" 33 | case DeadlineExceeded: 34 | return "DeadlineExceeded" 35 | case NotFound: 36 | return "NotFound" 37 | case AlreadyExists: 38 | return "AlreadyExists" 39 | case PermissionDenied: 40 | return "PermissionDenied" 41 | case ResourceExhausted: 42 | return "ResourceExhausted" 43 | case FailedPrecondition: 44 | return "FailedPrecondition" 45 | case Aborted: 46 | return "Aborted" 47 | case OutOfRange: 48 | return "OutOfRange" 49 | case Unimplemented: 50 | return "Unimplemented" 51 | case Internal: 52 | return "Internal" 53 | case Unavailable: 54 | return "Unavailable" 55 | case DataLoss: 56 | return "DataLoss" 57 | case Unauthenticated: 58 | return "Unauthenticated" 59 | default: 60 | return "Code(" + strconv.FormatInt(int64(c), 10) + ")" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /examples/features/compression/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Binary server is an example server. 20 | package main 21 | 22 | import ( 23 | "context" 24 | "flag" 25 | "fmt" 26 | "log" 27 | "net" 28 | 29 | "google.golang.org/grpc" 30 | _ "google.golang.org/grpc/encoding/gzip" // Install the gzip compressor 31 | 32 | pb "google.golang.org/grpc/examples/features/proto/echo" 33 | ) 34 | 35 | var port = flag.Int("port", 50051, "the port to serve on") 36 | 37 | type server struct { 38 | pb.UnimplementedEchoServer 39 | } 40 | 41 | func (s *server) UnaryEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) { 42 | fmt.Printf("UnaryEcho called with message %q\n", in.GetMessage()) 43 | return &pb.EchoResponse{Message: in.Message}, nil 44 | } 45 | 46 | func main() { 47 | flag.Parse() 48 | 49 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) 50 | if err != nil { 51 | log.Fatalf("failed to listen: %v", err) 52 | } 53 | fmt.Printf("server listening at %v\n", lis.Addr()) 54 | 55 | s := grpc.NewServer() 56 | pb.RegisterEchoServer(s, &server{}) 57 | s.Serve(lis) 58 | } 59 | -------------------------------------------------------------------------------- /encoding/proto/proto.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package proto defines the protobuf codec. Importing this package will 20 | // register the codec. 21 | package proto 22 | 23 | import ( 24 | "fmt" 25 | 26 | "github.com/golang/protobuf/proto" 27 | "google.golang.org/grpc/encoding" 28 | ) 29 | 30 | // Name is the name registered for the proto compressor. 31 | const Name = "proto" 32 | 33 | func init() { 34 | encoding.RegisterCodec(codec{}) 35 | } 36 | 37 | // codec is a Codec implementation with protobuf. It is the default codec for gRPC. 38 | type codec struct{} 39 | 40 | func (codec) Marshal(v interface{}) ([]byte, error) { 41 | vv, ok := v.(proto.Message) 42 | if !ok { 43 | return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v) 44 | } 45 | return proto.Marshal(vv) 46 | } 47 | 48 | func (codec) Unmarshal(data []byte, v interface{}) error { 49 | vv, ok := v.(proto.Message) 50 | if !ok { 51 | return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v) 52 | } 53 | return proto.Unmarshal(data, vv) 54 | } 55 | 56 | func (codec) Name() string { 57 | return Name 58 | } 59 | -------------------------------------------------------------------------------- /reflection/grpc_testing_not_regenerate/testv3.proto: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | syntax = "proto3"; 19 | 20 | option go_package = "google.golang.org/grpc/reflection/grpc_testing_not_regenerate"; 21 | 22 | package grpc.testingv3; 23 | 24 | message SearchResponseV3 { 25 | message Result { 26 | string url = 1; 27 | string title = 2; 28 | repeated string snippets = 3; 29 | message Value { 30 | oneof val { 31 | string str = 1; 32 | int64 int = 2; 33 | double real = 3; 34 | } 35 | } 36 | map metadata = 4; 37 | } 38 | enum State { 39 | UNKNOWN = 0; 40 | FRESH = 1; 41 | STALE = 2; 42 | } 43 | repeated Result results = 1; 44 | State state = 2; 45 | } 46 | 47 | message SearchRequestV3 { 48 | string query = 1; 49 | } 50 | 51 | // SearchServiceV3 is used to test grpc server reflection. 52 | service SearchServiceV3 { 53 | // Search is a unary RPC. 54 | rpc Search(SearchRequestV3) returns (SearchResponseV3); 55 | 56 | // StreamingSearch is a streaming RPC. 57 | rpc StreamingSearch(stream SearchRequestV3) returns (stream SearchResponseV3); 58 | } 59 | -------------------------------------------------------------------------------- /Documentation/log_levels.md: -------------------------------------------------------------------------------- 1 | # Log Levels 2 | 3 | This document describes the different log levels supported by the grpc-go 4 | library, and under what conditions they should be used. 5 | 6 | ### Info 7 | 8 | Info messages are for informational purposes and may aid in the debugging of 9 | applications or the gRPC library. 10 | 11 | Examples: 12 | - The name resolver received an update. 13 | - The balancer updated its picker. 14 | - Significant gRPC state is changing. 15 | 16 | At verbosity of 0 (the default), any single info message should not be output 17 | more than once every 5 minutes under normal operation. 18 | 19 | ### Warning 20 | 21 | Warning messages indicate problems that are non-fatal for the application, but 22 | could lead to unexpected behavior or subsequent errors. 23 | 24 | Examples: 25 | - Resolver could not resolve target name. 26 | - Error received while connecting to a server. 27 | - Lost or corrupt connection with remote endpoint. 28 | 29 | ### Error 30 | 31 | Error messages represent errors in the usage of gRPC that cannot be returned to 32 | the application as errors, or internal gRPC-Go errors that are recoverable. 33 | 34 | Internal errors are detected during gRPC tests and will result in test failures. 35 | 36 | Examples: 37 | - Invalid arguments passed to a function that cannot return an error. 38 | - An internal error that cannot be returned or would be inappropriate to return 39 | to the user. 40 | 41 | ### Fatal 42 | 43 | Fatal errors are severe internal errors that are unrecoverable. These lead 44 | directly to panics, and are avoided as much as possible. 45 | 46 | Example: 47 | - Internal invariant was violated. 48 | - User attempted an action that cannot return an error gracefully, but would 49 | lead to an invalid state if performed. 50 | -------------------------------------------------------------------------------- /stress/grpc_testing/metrics.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2016 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Contains the definitions for a metrics service and the type of metrics 16 | // exposed by the service. 17 | // 18 | // Currently, 'Gauge' (i.e a metric that represents the measured value of 19 | // something at an instant of time) is the only metric type supported by the 20 | // service. 21 | syntax = "proto3"; 22 | 23 | option go_package = "google.golang.org/grpc/stress/grpc_testing"; 24 | 25 | package grpc.testing; 26 | 27 | // Response message containing the gauge name and value 28 | message GaugeResponse { 29 | string name = 1; 30 | oneof value { 31 | int64 long_value = 2; 32 | double double_value = 3; 33 | string string_value = 4; 34 | } 35 | } 36 | 37 | // Request message containing the gauge name 38 | message GaugeRequest { 39 | string name = 1; 40 | } 41 | 42 | message EmptyMessage {} 43 | 44 | service MetricsService { 45 | // Returns the values of all the gauges that are currently being maintained by 46 | // the service 47 | rpc GetAllGauges(EmptyMessage) returns (stream GaugeResponse); 48 | 49 | // Returns the value of one gauge 50 | rpc GetGauge(GaugeRequest) returns (GaugeResponse); 51 | } 52 | -------------------------------------------------------------------------------- /internal/googlecloud/googlecloud_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package googlecloud 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func TestIsRunningOnGCE(t *testing.T) { 26 | for _, tc := range []struct { 27 | description string 28 | testOS string 29 | testManufacturer string 30 | out bool 31 | }{ 32 | // Linux tests. 33 | {"linux: not a GCP platform", "linux", "not GCP", false}, 34 | {"Linux: GCP platform (Google)", "linux", "Google", true}, 35 | {"Linux: GCP platform (Google Compute Engine)", "linux", "Google Compute Engine", true}, 36 | {"Linux: GCP platform (Google Compute Engine) with extra spaces", "linux", " Google Compute Engine ", true}, 37 | // Windows tests. 38 | {"windows: not a GCP platform", "windows", "not GCP", false}, 39 | {"windows: GCP platform (Google)", "windows", "Google", true}, 40 | {"windows: GCP platform (Google) with extra spaces", "windows", " Google ", true}, 41 | } { 42 | if got, want := isRunningOnGCE([]byte(tc.testManufacturer), tc.testOS), tc.out; got != want { 43 | t.Errorf("%v: isRunningOnGCE()=%v, want %v", tc.description, got, want) 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /testdata/server1.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDnE443EknxvxBq 3 | 6+hvn/t09hl8hx366EBYvZmVM/NC+7igXRAjiJiA/mIaCvL3MS0Iz5hBLxSGICU+ 4 | WproA3GCIFITIwcf/ETyWj/5xpgZ4AKrLrjQmmX8mhwUajfF3UvwMJrCOVqPp67t 5 | PtP+2kBXaqrXdvnvXR41FsIB8V7zIAuIZB6bHQhiGVlc1sgZYsE2EGG9WMmHtS86 6 | qkAOTjG2XyjmPTGAwhGDpYkYrpzp99IiDh4/Veai81hn0ssQkbry0XRD/Ig3jcHh 7 | 23WiriPNJ0JsbgXUSLKRPZObA9VgOLy2aXoN84IMaeK3yy+cwSYG/99w93fUZJte 8 | MXwz4oYZAgMBAAECggEBAIVn2Ncai+4xbH0OLWckabwgyJ4IM9rDc0LIU368O1kU 9 | koais8qP9dujAWgfoh3sGh/YGgKn96VnsZjKHlyMgF+r4TaDJn3k2rlAOWcurGlj 10 | 1qaVlsV4HiEzp7pxiDmHhWvp4672Bb6iBG+bsjCUOEk/n9o9KhZzIBluRhtxCmw5 11 | nw4Do7z00PTvN81260uPWSc04IrytvZUiAIx/5qxD72bij2xJ8t/I9GI8g4FtoVB 12 | 8pB6S/hJX1PZhh9VlU6Yk+TOfOVnbebG4W5138LkB835eqk3Zz0qsbc2euoi8Hxi 13 | y1VGwQEmMQ63jXz4c6g+X55ifvUK9Jpn5E8pq+pMd7ECgYEA93lYq+Cr54K4ey5t 14 | sWMa+ye5RqxjzgXj2Kqr55jb54VWG7wp2iGbg8FMlkQwzTJwebzDyCSatguEZLuB 15 | gRGroRnsUOy9vBvhKPOch9bfKIl6qOgzMJB267fBVWx5ybnRbWN/I7RvMQf3k+9y 16 | biCIVnxDLEEYyx7z85/5qxsXg/MCgYEA7wmWKtCTn032Hy9P8OL49T0X6Z8FlkDC 17 | Rk42ygrc/MUbugq9RGUxcCxoImOG9JXUpEtUe31YDm2j+/nbvrjl6/bP2qWs0V7l 18 | dTJl6dABP51pCw8+l4cWgBBX08Lkeen812AAFNrjmDCjX6rHjWHLJcpS18fnRRkP 19 | V1d/AHWX7MMCgYEA6Gsw2guhp0Zf2GCcaNK5DlQab8OL4Hwrpttzo4kuTlwtqNKp 20 | Q9H4al9qfF4Cr1TFya98+EVYf8yFRM3NLNjZpe3gwYf2EerlJj7VLcahw0KKzoN1 21 | QBENfwgPLRk5sDkx9VhSmcfl/diLroZdpAwtv3vo4nEoxeuGFbKTGx3Qkf0CgYEA 22 | xyR+dcb05Ygm3w4klHQTowQ10s1H80iaUcZBgQuR1ghEtDbUPZHsoR5t1xCB02ys 23 | DgAwLv1bChIvxvH/L6KM8ovZ2LekBX4AviWxoBxJnfz/EVau98B0b1auRN6eSC83 24 | FRuGldlSOW1z/nSh8ViizSYE5H5HX1qkXEippvFRE88CgYB3Bfu3YQY60ITWIShv 25 | nNkdcbTT9eoP9suaRJjw92Ln+7ZpALYlQMKUZmJ/5uBmLs4RFwUTQruLOPL4yLTH 26 | awADWUzs3IRr1fwn9E+zM8JVyKCnUEM3w4N5UZskGO2klashAd30hWO+knRv/y0r 27 | uGIYs9Ek7YXlXIRVrzMwcsrt1w== 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/crl/README.md: -------------------------------------------------------------------------------- 1 | # CRL Test Data 2 | 3 | This directory contains cert chains and CRL files for revocation testing. 4 | 5 | To print the chain, use a command like, 6 | 7 | ```shell 8 | openssl crl2pkcs7 -nocrl -certfile security/crl/x509/client/testdata/revokedLeaf.pem | openssl pkcs7 -print_certs -text -noout 9 | ``` 10 | 11 | The crl file symlinks are generated with `openssl rehash` 12 | 13 | ## unrevoked.pem 14 | 15 | A certificate chain with CRL files and unrevoked certs 16 | 17 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 18 | OU=campus-sln, CN=Root CA (2021-02-02T07:30:36-08:00) 19 | * 1.crl 20 | 21 | NOTE: 1.crl file is symlinked with 5.crl to simulate two issuers that hash to 22 | the same value to test that loading multiple files works. 23 | 24 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 25 | OU=campus-sln, CN=node CA (2021-02-02T07:30:36-08:00) 26 | * 2.crl 27 | 28 | ## revokedInt.pem 29 | 30 | Certificate chain where the intermediate is revoked 31 | 32 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 33 | OU=campus-sln, CN=Root CA (2021-02-02T07:31:54-08:00) 34 | * 3.crl 35 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 36 | OU=campus-sln, CN=node CA (2021-02-02T07:31:54-08:00) 37 | * 4.crl 38 | 39 | ## revokedLeaf.pem 40 | 41 | Certificate chain where the leaf is revoked 42 | 43 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 44 | OU=campus-sln, CN=Root CA (2021-02-02T07:32:57-08:00) 45 | * 5.crl 46 | * Subject: C=US, ST=California, L=Mountain View, O=Google LLC, OU=Production, 47 | OU=campus-sln, CN=node CA (2021-02-02T07:32:57-08:00) 48 | * 6.crl 49 | -------------------------------------------------------------------------------- /test/kokoro/xds.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -exu -o pipefail 4 | [[ -f /VERSION ]] && cat /VERSION 5 | 6 | cd github 7 | 8 | export GOPATH="${HOME}/gopath" 9 | pushd grpc-go/interop/xds/client 10 | # Install a version of Go supported by gRPC for the new features, e.g. 11 | # errors.Is() 12 | curl --retry 3 -O -L https://go.dev/dl/go1.17.3.linux-amd64.tar.gz 13 | sudo tar -C /usr/local -xf go1.17.3.linux-amd64.tar.gz 14 | sudo ln -s /usr/local/go/bin/go /usr/bin/go 15 | # Retry go build on errors (e.g. go get connection errors), for at most 3 times 16 | for i in 1 2 3; do go build && break || sleep 5; done 17 | popd 18 | 19 | git clone -b master --single-branch --depth=1 https://github.com/grpc/grpc.git 20 | 21 | grpc/tools/run_tests/helper_scripts/prep_xds.sh 22 | 23 | # Test cases "path_matching" and "header_matching" are not included in "all", 24 | # because not all interop clients in all languages support these new tests. 25 | # 26 | # TODO: remove "path_matching" and "header_matching" from --test_case after 27 | # they are added into "all". 28 | GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info \ 29 | python3 grpc/tools/run_tests/run_xds_tests.py \ 30 | --test_case="ping_pong,circuit_breaking" \ 31 | --project_id=grpc-testing \ 32 | --project_num=830293263384 \ 33 | --source_image=projects/grpc-testing/global/images/xds-test-server-5 \ 34 | --path_to_server_binary=/java_server/grpc-java/interop-testing/build/install/grpc-interop-testing/bin/xds-test-server \ 35 | --gcp_suffix=$(date '+%s') \ 36 | --verbose \ 37 | ${XDS_V3_OPT-} \ 38 | --client_cmd="grpc-go/interop/xds/client/client \ 39 | --server=xds:///{server_uri} \ 40 | --stats_port={stats_port} \ 41 | --qps={qps} \ 42 | {fail_on_failed_rpc} \ 43 | {rpcs_to_send} \ 44 | {metadata_to_send}" 45 | -------------------------------------------------------------------------------- /internal/channelz/types_linux.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package channelz 20 | 21 | import ( 22 | "syscall" 23 | 24 | "golang.org/x/sys/unix" 25 | ) 26 | 27 | // SocketOptionData defines the struct to hold socket option data, and related 28 | // getter function to obtain info from fd. 29 | type SocketOptionData struct { 30 | Linger *unix.Linger 31 | RecvTimeout *unix.Timeval 32 | SendTimeout *unix.Timeval 33 | TCPInfo *unix.TCPInfo 34 | } 35 | 36 | // Getsockopt defines the function to get socket options requested by channelz. 37 | // It is to be passed to syscall.RawConn.Control(). 38 | func (s *SocketOptionData) Getsockopt(fd uintptr) { 39 | if v, err := unix.GetsockoptLinger(int(fd), syscall.SOL_SOCKET, syscall.SO_LINGER); err == nil { 40 | s.Linger = v 41 | } 42 | if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO); err == nil { 43 | s.RecvTimeout = v 44 | } 45 | if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO); err == nil { 46 | s.SendTimeout = v 47 | } 48 | if v, err := unix.GetsockoptTCPInfo(int(fd), syscall.SOL_TCP, syscall.TCP_INFO); err == nil { 49 | s.TCPInfo = v 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /internal/resolver/passthrough/passthrough.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package passthrough implements a pass-through resolver. It sends the target 20 | // name without scheme back to gRPC as resolved address. 21 | package passthrough 22 | 23 | import "google.golang.org/grpc/resolver" 24 | 25 | const scheme = "passthrough" 26 | 27 | type passthroughBuilder struct{} 28 | 29 | func (*passthroughBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { 30 | r := &passthroughResolver{ 31 | target: target, 32 | cc: cc, 33 | } 34 | r.start() 35 | return r, nil 36 | } 37 | 38 | func (*passthroughBuilder) Scheme() string { 39 | return scheme 40 | } 41 | 42 | type passthroughResolver struct { 43 | target resolver.Target 44 | cc resolver.ClientConn 45 | } 46 | 47 | func (r *passthroughResolver) start() { 48 | r.cc.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: r.target.Endpoint}}}) 49 | } 50 | 51 | func (*passthroughResolver) ResolveNow(o resolver.ResolveNowOptions) {} 52 | 53 | func (*passthroughResolver) Close() {} 54 | 55 | func init() { 56 | resolver.Register(&passthroughBuilder{}) 57 | } 58 | -------------------------------------------------------------------------------- /credentials/oauth/oauth_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2021 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package oauth 20 | 21 | import ( 22 | "strings" 23 | "testing" 24 | ) 25 | 26 | func checkErrorMsg(err error, msg string) bool { 27 | if err == nil && msg == "" { 28 | return true 29 | } else if err != nil { 30 | return strings.Contains(err.Error(), msg) 31 | } 32 | return false 33 | } 34 | 35 | func TestRemoveServiceNameFromJWTURI(t *testing.T) { 36 | tests := []struct { 37 | name string 38 | uri string 39 | wantedURI string 40 | wantedErrMsg string 41 | }{ 42 | { 43 | name: "invalid URI", 44 | uri: "ht tp://foo.com", 45 | wantedErrMsg: "first path segment in URL cannot contain colon", 46 | }, 47 | { 48 | name: "valid URI", 49 | uri: "https://foo.com/go/", 50 | wantedURI: "https://foo.com/", 51 | }, 52 | } 53 | for _, tt := range tests { 54 | t.Run(tt.name, func(t *testing.T) { 55 | if got, err := removeServiceNameFromJWTURI(tt.uri); got != tt.wantedURI || !checkErrorMsg(err, tt.wantedErrMsg) { 56 | t.Errorf("RemoveServiceNameFromJWTURI() = %s, %v, want %s, %v", got, err, tt.wantedURI, tt.wantedErrMsg) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /security/advancedtls/testdata/server_key_3.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDcIyJEt3ZAxPn7 3 | H5f/IwXS8NcoAXWP8L6rWndcg+EWayx7W+wmUsFKGSFGzrFPPCFmKO8MrQqp8LSA 4 | xHAVtOC6Uw+INWJJw9BRlx2nvV7hfbqu3OnPkPVkN/siUQCqnEKJQHliNT9XDl4/ 5 | Mav75uQSWb3Vfi3KtG7mzPFNNbbe4yfHyGbC4e9RtKkGimDSJ413s3m4+scDvtpC 6 | cCXj9XXZNdCwD1CL3kNdmOdhgfkDBP+AMLBFKZKqpCo6m0s4JJTiej13dc27wTrn 7 | kFm1CP77SV+kQlWg5DAcVXYJkN9FqNqExqIPS/SxMk7H+3qSQACbttQK9UmCn3qR 8 | 3pGbwqzNAgMBAAECggEAU0Te84tKKdnYjUs4HYRL8ay0VienJpl0JjEEMXSZMfe8 9 | TbVJsH1hK/wxgC0zGLuwDoqxUeQqwnmQbZzgoPVYhGJi360BztFI/XPh/c8+EqGS 10 | eg6KSr+UcyJR1ns5e0+8Q1qmD6YAnZeLwu+xFIoT/3T+v8EI5UI3KQqgxAnrcIdc 11 | RWqIwWLkkPm1QVRYsvRaTvmgFd2LcIT9AdIruP/VsqF3GEzvEQSr0lgmwOe0izLb 12 | HKfZr+2JwOppwTLGhKo1wUyUUsglXCBOcFYAA03xdviQWBuCeKXamNrdB7M0T4zZ 13 | THheNRQq2g7bTYnncCYKcrNa5VY5wmHY767mf6ahAQKBgQD+AGk21CsRgiKCGzUI 14 | wTRynutAMkX55U1bud2+OMpzH9omvnccX5ezq5WTw/jZfz+jrUBF8YKSTtb3InUg 15 | yXcpJ/XjRDFMZp1Avy/44rOlYg1QYMD4JK96f8bbd1yej5NVw45V9synaJHuvoDV 16 | bkbZu00S0X8Pgvlh354MUH3CLQKBgQDd3oQdKMZgsX8gtmcQfQoEZ0VvlOYmTM5W 17 | Kw+24iGrBkfBt+NuKx8qm1CpoFGx4G2+TgttMywjF9RG3R2uGqbJZbCOAXzjRMQJ 18 | L9PuTiGAdYD3fA5cTmnrrNEhPydtRhF2M3p8FFeQtwsEBYreXux25rbmVOYTFMgJ 19 | hVUW9RdZIQKBgQDwEYdgMQw70hm3iuuHSMS/iQCkfl+xH08MYRH6FkcSpIpVkDOX 20 | 96m0QXpwXQs41pJZqwhSkz9r9WQr1L+Lq58aoRBAK1XE9j+u0IUQ4YQVziTzUV9R 21 | qarJRze2eoxpuR3yM5C2IzuvBqDXW+r8zuvcIrFoFeXXzVzTar1AulsCSQKBgQCa 22 | UD+3QDrp2co/6F26vB0RfvpuZzPEA7undv/RBWrBVvblp46Je3iL28a4lAb+Hsh1 23 | ijasVuEl71b3iqcwBt1mSlIIEsTYFWX7tcZDgxgODqwKdcBPN0K4ZlR2OUSk3g0b 24 | Fybj0gotXwJMY8Z4b7Er6b/gZ8A2GUggRxotg34fwQKBgEh6a88SgTyu1KcR2yzN 25 | Zbs8MEfZ8hqfUj+GL0+6y9KQd4uSIngyHWGNETE9dCObNz5HEdJIpUNvB3vfN0Vk 26 | f1EEPDQLQKJjii7jZ9U9XfPVaUhqIVH3Aupmb30H3AQw7XOF23g63k9Yg1FWtDT6 27 | 4CuBsUvjXywL4yHrWK+BuSXF 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /internal/grpcsync/event.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package grpcsync implements additional synchronization primitives built upon 20 | // the sync package. 21 | package grpcsync 22 | 23 | import ( 24 | "sync" 25 | "sync/atomic" 26 | ) 27 | 28 | // Event represents a one-time event that may occur in the future. 29 | type Event struct { 30 | fired int32 31 | c chan struct{} 32 | o sync.Once 33 | } 34 | 35 | // Fire causes e to complete. It is safe to call multiple times, and 36 | // concurrently. It returns true iff this call to Fire caused the signaling 37 | // channel returned by Done to close. 38 | func (e *Event) Fire() bool { 39 | ret := false 40 | e.o.Do(func() { 41 | atomic.StoreInt32(&e.fired, 1) 42 | close(e.c) 43 | ret = true 44 | }) 45 | return ret 46 | } 47 | 48 | // Done returns a channel that will be closed when Fire is called. 49 | func (e *Event) Done() <-chan struct{} { 50 | return e.c 51 | } 52 | 53 | // HasFired returns true if Fire has been called. 54 | func (e *Event) HasFired() bool { 55 | return atomic.LoadInt32(&e.fired) == 1 56 | } 57 | 58 | // NewEvent returns a new, ready-to-use Event. 59 | func NewEvent() *Event { 60 | return &Event{c: make(chan struct{})} 61 | } 62 | -------------------------------------------------------------------------------- /examples/features/encryption/ALTS/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Binary server is an example server. 20 | package main 21 | 22 | import ( 23 | "context" 24 | "flag" 25 | "fmt" 26 | "log" 27 | "net" 28 | 29 | "google.golang.org/grpc" 30 | "google.golang.org/grpc/credentials/alts" 31 | 32 | pb "google.golang.org/grpc/examples/features/proto/echo" 33 | ) 34 | 35 | var port = flag.Int("port", 50051, "the port to serve on") 36 | 37 | type ecServer struct { 38 | pb.UnimplementedEchoServer 39 | } 40 | 41 | func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) { 42 | return &pb.EchoResponse{Message: req.Message}, nil 43 | } 44 | 45 | func main() { 46 | flag.Parse() 47 | 48 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) 49 | if err != nil { 50 | log.Fatalf("failed to listen: %v", err) 51 | } 52 | // Create alts based credential. 53 | altsTC := alts.NewServerCreds(alts.DefaultServerOptions()) 54 | 55 | s := grpc.NewServer(grpc.Creds(altsTC)) 56 | 57 | // Register EchoServer on the server. 58 | pb.RegisterEchoServer(s, &ecServer{}) 59 | 60 | if err := s.Serve(lis); err != nil { 61 | log.Fatalf("failed to serve: %v", err) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /examples/helloworld/greeter_server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package main implements a server for Greeter service. 20 | package main 21 | 22 | import ( 23 | "context" 24 | "flag" 25 | "fmt" 26 | "log" 27 | "net" 28 | 29 | "google.golang.org/grpc" 30 | pb "google.golang.org/grpc/examples/helloworld/helloworld" 31 | ) 32 | 33 | var ( 34 | port = flag.Int("port", 50051, "The server port") 35 | ) 36 | 37 | // server is used to implement helloworld.GreeterServer. 38 | type server struct { 39 | pb.UnimplementedGreeterServer 40 | } 41 | 42 | // SayHello implements helloworld.GreeterServer 43 | func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 44 | log.Printf("Received: %v", in.GetName()) 45 | return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil 46 | } 47 | 48 | func main() { 49 | flag.Parse() 50 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) 51 | if err != nil { 52 | log.Fatalf("failed to listen: %v", err) 53 | } 54 | s := grpc.NewServer() 55 | pb.RegisterGreeterServer(s, &server{}) 56 | log.Printf("server listening at %v", lis.Addr()) 57 | if err := s.Serve(lis); err != nil { 58 | log.Fatalf("failed to serve: %v", err) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /xds/bootstrap/bootstrap_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2022 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | package bootstrap 19 | 20 | import ( 21 | "encoding/json" 22 | "testing" 23 | 24 | "google.golang.org/grpc/credentials" 25 | ) 26 | 27 | const testCredsBuilderName = "test_creds" 28 | 29 | var builder = &testCredsBuilder{} 30 | 31 | func init() { 32 | RegisterCredentials(builder) 33 | } 34 | 35 | type testCredsBuilder struct { 36 | config json.RawMessage 37 | } 38 | 39 | func (t *testCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, error) { 40 | t.config = config 41 | return nil, nil 42 | } 43 | 44 | func (t *testCredsBuilder) Name() string { 45 | return testCredsBuilderName 46 | } 47 | 48 | func TestRegisterNew(t *testing.T) { 49 | c := GetCredentials(testCredsBuilderName) 50 | if c == nil { 51 | t.Fatalf("GetCredentials(%q) credential = nil", testCredsBuilderName) 52 | } 53 | 54 | const sampleConfig = "sample_config" 55 | rawMessage := json.RawMessage(sampleConfig) 56 | if _, err := c.Build(rawMessage); err != nil { 57 | t.Errorf("Build(%v) error = %v, want nil", rawMessage, err) 58 | } 59 | 60 | if got, want := string(builder.config), sampleConfig; got != want { 61 | t.Errorf("Build config = %v, want %v", got, want) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /internal/grpcrand/grpcrand.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package grpcrand implements math/rand functions in a concurrent-safe way 20 | // with a global random source, independent of math/rand's global source. 21 | package grpcrand 22 | 23 | import ( 24 | "math/rand" 25 | "sync" 26 | "time" 27 | ) 28 | 29 | var ( 30 | r = rand.New(rand.NewSource(time.Now().UnixNano())) 31 | mu sync.Mutex 32 | ) 33 | 34 | // Int implements rand.Int on the grpcrand global source. 35 | func Int() int { 36 | mu.Lock() 37 | defer mu.Unlock() 38 | return r.Int() 39 | } 40 | 41 | // Int63n implements rand.Int63n on the grpcrand global source. 42 | func Int63n(n int64) int64 { 43 | mu.Lock() 44 | defer mu.Unlock() 45 | return r.Int63n(n) 46 | } 47 | 48 | // Intn implements rand.Intn on the grpcrand global source. 49 | func Intn(n int) int { 50 | mu.Lock() 51 | defer mu.Unlock() 52 | return r.Intn(n) 53 | } 54 | 55 | // Float64 implements rand.Float64 on the grpcrand global source. 56 | func Float64() float64 { 57 | mu.Lock() 58 | defer mu.Unlock() 59 | return r.Float64() 60 | } 61 | 62 | // Uint64 implements rand.Uint64 on the grpcrand global source. 63 | func Uint64() uint64 { 64 | mu.Lock() 65 | defer mu.Unlock() 66 | return r.Uint64() 67 | } 68 | -------------------------------------------------------------------------------- /codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2014 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package grpc 20 | 21 | import ( 22 | "google.golang.org/grpc/encoding" 23 | _ "google.golang.org/grpc/encoding/proto" // to register the Codec for "proto" 24 | ) 25 | 26 | // baseCodec contains the functionality of both Codec and encoding.Codec, but 27 | // omits the name/string, which vary between the two and are not needed for 28 | // anything besides the registry in the encoding package. 29 | type baseCodec interface { 30 | Marshal(v interface{}) ([]byte, error) 31 | Unmarshal(data []byte, v interface{}) error 32 | } 33 | 34 | var _ baseCodec = Codec(nil) 35 | var _ baseCodec = encoding.Codec(nil) 36 | 37 | // Codec defines the interface gRPC uses to encode and decode messages. 38 | // Note that implementations of this interface must be thread safe; 39 | // a Codec's methods can be called from concurrent goroutines. 40 | // 41 | // Deprecated: use encoding.Codec instead. 42 | type Codec interface { 43 | // Marshal returns the wire format of v. 44 | Marshal(v interface{}) ([]byte, error) 45 | // Unmarshal parses the wire format into v. 46 | Unmarshal(data []byte, v interface{}) error 47 | // String returns the name of the Codec implementation. This is unused by 48 | // gRPC. 49 | String() string 50 | } 51 | -------------------------------------------------------------------------------- /examples/features/cancellation/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2018 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Binary server is an example server. 20 | package main 21 | 22 | import ( 23 | "flag" 24 | "fmt" 25 | "io" 26 | "log" 27 | "net" 28 | 29 | "google.golang.org/grpc" 30 | 31 | pb "google.golang.org/grpc/examples/features/proto/echo" 32 | ) 33 | 34 | var port = flag.Int("port", 50051, "the port to serve on") 35 | 36 | type server struct { 37 | pb.UnimplementedEchoServer 38 | } 39 | 40 | func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error { 41 | for { 42 | in, err := stream.Recv() 43 | if err != nil { 44 | fmt.Printf("server: error receiving from stream: %v\n", err) 45 | if err == io.EOF { 46 | return nil 47 | } 48 | return err 49 | } 50 | fmt.Printf("echoing message %q\n", in.Message) 51 | stream.Send(&pb.EchoResponse{Message: in.Message}) 52 | } 53 | } 54 | 55 | func main() { 56 | flag.Parse() 57 | 58 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) 59 | if err != nil { 60 | log.Fatalf("failed to listen: %v", err) 61 | } 62 | fmt.Printf("server listening at port %v\n", lis.Addr()) 63 | s := grpc.NewServer() 64 | pb.RegisterEchoServer(s, &server{}) 65 | s.Serve(lis) 66 | } 67 | -------------------------------------------------------------------------------- /examples/helloworld/greeter_client/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | // Package main implements a client for Greeter service. 20 | package main 21 | 22 | import ( 23 | "context" 24 | "flag" 25 | "log" 26 | "time" 27 | 28 | "google.golang.org/grpc" 29 | "google.golang.org/grpc/credentials/insecure" 30 | pb "google.golang.org/grpc/examples/helloworld/helloworld" 31 | ) 32 | 33 | const ( 34 | defaultName = "world" 35 | ) 36 | 37 | var ( 38 | addr = flag.String("addr", "localhost:50051", "the address to connect to") 39 | name = flag.String("name", defaultName, "Name to greet") 40 | ) 41 | 42 | func main() { 43 | flag.Parse() 44 | // Set up a connection to the server. 45 | conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials())) 46 | if err != nil { 47 | log.Fatalf("did not connect: %v", err) 48 | } 49 | defer conn.Close() 50 | c := pb.NewGreeterClient(conn) 51 | 52 | // Contact the server and print out its response. 53 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) 54 | defer cancel() 55 | r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name}) 56 | if err != nil { 57 | log.Fatalf("could not greet: %v", err) 58 | } 59 | log.Printf("Greeting: %s", r.GetMessage()) 60 | } 61 | -------------------------------------------------------------------------------- /internal/credentials/credentials.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 gRPC authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package credentials 18 | 19 | import ( 20 | "context" 21 | ) 22 | 23 | // requestInfoKey is a struct to be used as the key to store RequestInfo in a 24 | // context. 25 | type requestInfoKey struct{} 26 | 27 | // NewRequestInfoContext creates a context with ri. 28 | func NewRequestInfoContext(ctx context.Context, ri interface{}) context.Context { 29 | return context.WithValue(ctx, requestInfoKey{}, ri) 30 | } 31 | 32 | // RequestInfoFromContext extracts the RequestInfo from ctx. 33 | func RequestInfoFromContext(ctx context.Context) interface{} { 34 | return ctx.Value(requestInfoKey{}) 35 | } 36 | 37 | // clientHandshakeInfoKey is a struct used as the key to store 38 | // ClientHandshakeInfo in a context. 39 | type clientHandshakeInfoKey struct{} 40 | 41 | // ClientHandshakeInfoFromContext extracts the ClientHandshakeInfo from ctx. 42 | func ClientHandshakeInfoFromContext(ctx context.Context) interface{} { 43 | return ctx.Value(clientHandshakeInfoKey{}) 44 | } 45 | 46 | // NewClientHandshakeInfoContext creates a context with chi. 47 | func NewClientHandshakeInfoContext(ctx context.Context, chi interface{}) context.Context { 48 | return context.WithValue(ctx, clientHandshakeInfoKey{}, chi) 49 | } 50 | --------------------------------------------------------------------------------