main.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Binary server is an example server.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "net"
  26. "time"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. pb "google.golang.org/grpc/examples/features/proto/echo"
  30. "google.golang.org/grpc/keepalive"
  31. "google.golang.org/grpc/status"
  32. )
  33. var port = flag.Int("port", 50052, "port number")
  34. var kaep = keepalive.EnforcementPolicy{
  35. MinTime: 5 * time.Second, // If a client pings more than once every 5 seconds, terminate the connection
  36. PermitWithoutStream: true, // Allow pings even when there are no active streams
  37. }
  38. var kasp = keepalive.ServerParameters{
  39. MaxConnectionIdle: 15 * time.Second, // If a client is idle for 15 seconds, send a GOAWAY
  40. MaxConnectionAge: 30 * time.Second, // If any connection is alive for more than 30 seconds, send a GOAWAY
  41. MaxConnectionAgeGrace: 5 * time.Second, // Allow 5 seconds for pending RPCs to complete before forcibly closing connections
  42. Time: 5 * time.Second, // Ping the client if it is idle for 5 seconds to ensure the connection is still active
  43. Timeout: 1 * time.Second, // Wait 1 second for the ping ack before assuming the connection is dead
  44. }
  45. // server implements EchoServer.
  46. type server struct{}
  47. func (s *server) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
  48. return &pb.EchoResponse{Message: req.Message}, nil
  49. }
  50. func (s *server) ServerStreamingEcho(req *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
  51. return status.Error(codes.Unimplemented, "RPC unimplemented")
  52. }
  53. func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
  54. return status.Error(codes.Unimplemented, "RPC unimplemented")
  55. }
  56. func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
  57. return status.Error(codes.Unimplemented, "RPC unimplemented")
  58. }
  59. func main() {
  60. flag.Parse()
  61. address := fmt.Sprintf(":%v", *port)
  62. lis, err := net.Listen("tcp", address)
  63. if err != nil {
  64. log.Fatalf("failed to listen: %v", err)
  65. }
  66. s := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp))
  67. pb.RegisterEchoServer(s, &server{})
  68. if err := s.Serve(lis); err != nil {
  69. log.Fatalf("failed to serve: %v", err)
  70. }
  71. }