You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
830 B

  1. package util
  2. import (
  3. "time"
  4. "google.golang.org/grpc"
  5. "google.golang.org/grpc/keepalive"
  6. )
  7. func NewGrpcServer() *grpc.Server {
  8. return grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{
  9. Time: 10 * time.Second, // wait time before ping if no activity
  10. Timeout: 20 * time.Second, // ping timeout
  11. }), grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
  12. MinTime: 60 * time.Second, // min time a client should wait before sending a ping
  13. }))
  14. }
  15. func GrpcDial(address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
  16. opts = append(opts, grpc.WithInsecure())
  17. opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
  18. Time: 30 * time.Second, // client ping server if no activity for this long
  19. Timeout: 20 * time.Second,
  20. }))
  21. return grpc.Dial(address, opts...)
  22. }