server.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Binary server is an interop server.
  19. //
  20. // See interop test case descriptions [here].
  21. //
  22. // [here]: https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md
  23. package main
  24. import (
  25. "flag"
  26. "net"
  27. "strconv"
  28. "time"
  29. "google.golang.org/grpc"
  30. "google.golang.org/grpc/credentials"
  31. "google.golang.org/grpc/credentials/alts"
  32. "google.golang.org/grpc/grpclog"
  33. "google.golang.org/grpc/internal"
  34. "google.golang.org/grpc/interop"
  35. "google.golang.org/grpc/orca"
  36. "google.golang.org/grpc/testdata"
  37. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  38. )
  39. var (
  40. useTLS = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
  41. useALTS = flag.Bool("use_alts", false, "Connection uses ALTS if true (this option can only be used on GCP)")
  42. altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
  43. certFile = flag.String("tls_cert_file", "", "The TLS cert file")
  44. keyFile = flag.String("tls_key_file", "", "The TLS key file")
  45. port = flag.Int("port", 10000, "The server port")
  46. logger = grpclog.Component("interop")
  47. )
  48. func main() {
  49. flag.Parse()
  50. if *useTLS && *useALTS {
  51. logger.Fatal("-use_tls and -use_alts cannot be both set to true")
  52. }
  53. p := strconv.Itoa(*port)
  54. lis, err := net.Listen("tcp", ":"+p)
  55. if err != nil {
  56. logger.Fatalf("failed to listen: %v", err)
  57. }
  58. logger.Infof("interop server listening on %v", lis.Addr())
  59. opts := []grpc.ServerOption{orca.CallMetricsServerOption(nil)}
  60. if *useTLS {
  61. if *certFile == "" {
  62. *certFile = testdata.Path("server1.pem")
  63. }
  64. if *keyFile == "" {
  65. *keyFile = testdata.Path("server1.key")
  66. }
  67. creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
  68. if err != nil {
  69. logger.Fatalf("Failed to generate credentials: %v", err)
  70. }
  71. opts = append(opts, grpc.Creds(creds))
  72. } else if *useALTS {
  73. altsOpts := alts.DefaultServerOptions()
  74. if *altsHSAddr != "" {
  75. altsOpts.HandshakerServiceAddress = *altsHSAddr
  76. }
  77. altsTC := alts.NewServerCreds(altsOpts)
  78. opts = append(opts, grpc.Creds(altsTC))
  79. }
  80. server := grpc.NewServer(opts...)
  81. metricsRecorder := orca.NewServerMetricsRecorder()
  82. sopts := orca.ServiceOptions{
  83. MinReportingInterval: time.Second,
  84. ServerMetricsProvider: metricsRecorder,
  85. }
  86. internal.ORCAAllowAnyMinReportingInterval.(func(*orca.ServiceOptions))(&sopts)
  87. orca.Register(server, sopts)
  88. testgrpc.RegisterTestServiceServer(server, interop.NewTestServer(interop.NewTestServerOptions{MetricsRecorder: metricsRecorder}))
  89. server.Serve(lis)
  90. }