github.com/cloudwego/kitex@v0.9.0/pkg/remote/option.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo 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 remote
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"time"
    23  
    24  	"github.com/cloudwego/kitex/pkg/endpoint"
    25  	"github.com/cloudwego/kitex/pkg/profiler"
    26  	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/grpc"
    27  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    28  	"github.com/cloudwego/kitex/pkg/serviceinfo"
    29  	"github.com/cloudwego/kitex/pkg/streaming"
    30  )
    31  
    32  // Option is used to pack the inbound and outbound handlers.
    33  type Option struct {
    34  	Outbounds []OutboundHandler
    35  
    36  	Inbounds []InboundHandler
    37  
    38  	StreamingMetaHandlers []StreamingMetaHandler
    39  }
    40  
    41  // PrependBoundHandler adds a BoundHandler to the head.
    42  func (o *Option) PrependBoundHandler(h BoundHandler) {
    43  	switch v := h.(type) {
    44  	case DuplexBoundHandler:
    45  		o.Inbounds = append([]InboundHandler{v}, o.Inbounds...)
    46  		o.Outbounds = append([]OutboundHandler{v}, o.Outbounds...)
    47  	case InboundHandler:
    48  		o.Inbounds = append([]InboundHandler{v}, o.Inbounds...)
    49  	case OutboundHandler:
    50  		o.Outbounds = append([]OutboundHandler{v}, o.Outbounds...)
    51  	default:
    52  		panic("invalid BoundHandler: must implement InboundHandler or OutboundHandler")
    53  	}
    54  }
    55  
    56  // AppendBoundHandler adds a BoundHandler to the end.
    57  func (o *Option) AppendBoundHandler(h BoundHandler) {
    58  	switch v := h.(type) {
    59  	case DuplexBoundHandler:
    60  		o.Inbounds = append(o.Inbounds, v)
    61  		o.Outbounds = append(o.Outbounds, v)
    62  	case InboundHandler:
    63  		o.Inbounds = append(o.Inbounds, v)
    64  	case OutboundHandler:
    65  		o.Outbounds = append(o.Outbounds, v)
    66  	default:
    67  		panic("invalid BoundHandler: must implement InboundHandler or OutboundHandler")
    68  	}
    69  }
    70  
    71  // ServerOption contains option that is used to init the remote server.
    72  type ServerOption struct {
    73  	TargetSvcInfo *serviceinfo.ServiceInfo
    74  
    75  	SvcSearchMap map[string]*serviceinfo.ServiceInfo
    76  
    77  	TransServerFactory TransServerFactory
    78  
    79  	SvrHandlerFactory ServerTransHandlerFactory
    80  
    81  	Codec Codec
    82  
    83  	PayloadCodec PayloadCodec
    84  
    85  	// Listener is used to specify the server listener, which comes with higher priority than Address below.
    86  	Listener net.Listener
    87  
    88  	// Address is the listener addr
    89  	Address net.Addr
    90  
    91  	ReusePort bool
    92  
    93  	// Duration that server waits for to allow any existing connection to be closed gracefully.
    94  	ExitWaitTime time.Duration
    95  
    96  	// Duration that server waits for after error occurs during connection accepting.
    97  	AcceptFailedDelayTime time.Duration
    98  
    99  	// Duration that the accepted connection waits for to read or write data.
   100  	MaxConnectionIdleTime time.Duration
   101  
   102  	ReadWriteTimeout time.Duration
   103  
   104  	InitOrResetRPCInfoFunc func(rpcinfo.RPCInfo, net.Addr) rpcinfo.RPCInfo
   105  
   106  	TracerCtl *rpcinfo.TraceController
   107  
   108  	Profiler                 profiler.Profiler
   109  	ProfilerTransInfoTagging TransInfoTagging
   110  	ProfilerMessageTagging   MessageTagging
   111  
   112  	GRPCCfg *grpc.ServerConfig
   113  
   114  	GRPCUnknownServiceHandler func(ctx context.Context, method string, stream streaming.Stream) error
   115  
   116  	// RefuseTrafficWithoutServiceName is used for a server with multi services
   117  	RefuseTrafficWithoutServiceName bool
   118  
   119  	Option
   120  
   121  	// invoking chain with recv/send middlewares for streaming APIs
   122  	RecvEndpoint endpoint.RecvEndpoint
   123  	SendEndpoint endpoint.SendEndpoint
   124  
   125  	// for thrift streaming, this is enabled by default
   126  	// for grpc(protobuf) streaming, it's disabled by default, enable with server.WithCompatibleMiddlewareForUnary
   127  	CompatibleMiddlewareForUnary bool
   128  }
   129  
   130  // ClientOption is used to init the remote client.
   131  type ClientOption struct {
   132  	SvcInfo *serviceinfo.ServiceInfo
   133  
   134  	CliHandlerFactory ClientTransHandlerFactory
   135  
   136  	Codec Codec
   137  
   138  	PayloadCodec PayloadCodec
   139  
   140  	ConnPool ConnPool
   141  
   142  	Dialer Dialer
   143  
   144  	Option
   145  
   146  	EnableConnPoolReporter bool
   147  }