github.com/ovsinc/prototool@v1.3.0/internal/grpc/grpc.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package grpc
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"time"
    27  
    28  	"github.com/golang/protobuf/protoc-gen-go/descriptor"
    29  	"go.uber.org/zap"
    30  )
    31  
    32  const (
    33  	// DefaultCallTimeout is the default call timeout.
    34  	DefaultCallTimeout = 60 * time.Second
    35  	// DefaultConnectTimeout is the default connect timeout.
    36  	DefaultConnectTimeout = 10 * time.Second
    37  )
    38  
    39  // Handler handles gRPC calls.
    40  type Handler interface {
    41  	Invoke(fileDescriptorSets []*descriptor.FileDescriptorSet, address string, method string, inputReader io.Reader, outputWriter io.Writer) error
    42  }
    43  
    44  // HandlerOption is an option for a new Handler.
    45  type HandlerOption func(*handler)
    46  
    47  // HandlerWithLogger returns a HandlerOption that uses the given logger.
    48  //
    49  // The default is to use zap.NewNop().
    50  func HandlerWithLogger(logger *zap.Logger) HandlerOption {
    51  	return func(handler *handler) {
    52  		handler.logger = logger
    53  	}
    54  }
    55  
    56  // HandlerWithCallTimeout returns a HandlerOption that has the given call timeout.
    57  //
    58  // Each invocation must be completed within this time.
    59  //
    60  // The default is to use DefaultCallTimeout.
    61  func HandlerWithCallTimeout(callTimeout time.Duration) HandlerOption {
    62  	return func(handler *handler) {
    63  		handler.callTimeout = callTimeout
    64  	}
    65  }
    66  
    67  // HandlerWithConnectTimeout returns a HandlerOption that has the given connect timeout.
    68  //
    69  // The default is to use DefaultConnectTimeout.
    70  func HandlerWithConnectTimeout(connectTimeout time.Duration) HandlerOption {
    71  	return func(handler *handler) {
    72  		handler.connectTimeout = connectTimeout
    73  	}
    74  }
    75  
    76  // HandlerWithKeepaliveTime returns a HandlerOption that has the given keepalive time.
    77  //
    78  // The default is to have no keepalive time.
    79  func HandlerWithKeepaliveTime(keepaliveTime time.Duration) HandlerOption {
    80  	return func(handler *handler) {
    81  		handler.keepaliveTime = keepaliveTime
    82  	}
    83  }
    84  
    85  // HandlerWithHeader returns a HandlerOption that adds the given key/value header.
    86  func HandlerWithHeader(key string, value string) HandlerOption {
    87  	return func(handler *handler) {
    88  		// this is for grpcurl
    89  		// it takes care of validation
    90  		// if we switch out grpcurl we will probably change to a metadata.MD object
    91  		handler.headers = append(handler.headers, fmt.Sprintf("%s:%s", key, value))
    92  	}
    93  }
    94  
    95  // NewHandler returns a new Handler.
    96  func NewHandler(options ...HandlerOption) Handler {
    97  	return newHandler(options...)
    98  }