trpc.group/trpc-go/trpc-go@v1.0.2/transport/server_listenserve_options.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package transport
    15  
    16  import (
    17  	"net"
    18  	"time"
    19  
    20  	"trpc.group/trpc-go/trpc-go/codec"
    21  )
    22  
    23  // ListenServeOptions is the server options on start.
    24  type ListenServeOptions struct {
    25  	ServiceName   string
    26  	Address       string
    27  	Network       string
    28  	Handler       Handler
    29  	FramerBuilder codec.FramerBuilder
    30  	Listener      net.Listener
    31  
    32  	CACertFile  string        // ca certification file
    33  	TLSCertFile string        // server certification file
    34  	TLSKeyFile  string        // server key file
    35  	Routines    int           // size of goroutine pool
    36  	ServerAsync bool          // whether enable server async
    37  	Writev      bool          // whether enable writev in server
    38  	CopyFrame   bool          // whether copy frame
    39  	IdleTimeout time.Duration // idle timeout of connection
    40  
    41  	// DisableKeepAlives, if true, disables keep-alives and only use the
    42  	// connection for a single request.
    43  	// This used for rpc transport layer like http, it's unrelated to
    44  	// the TCP keep-alives.
    45  	DisableKeepAlives bool
    46  }
    47  
    48  // ListenServeOption modifies the ListenServeOptions.
    49  type ListenServeOption func(*ListenServeOptions)
    50  
    51  // WithServiceName returns a ListenServeOption which sets the service name.
    52  func WithServiceName(name string) ListenServeOption {
    53  	return func(opts *ListenServeOptions) {
    54  		opts.ServiceName = name
    55  	}
    56  }
    57  
    58  // WithServerFramerBuilder returns a ListenServeOption which sets server frame builder.
    59  func WithServerFramerBuilder(fb codec.FramerBuilder) ListenServeOption {
    60  	return func(opts *ListenServeOptions) {
    61  		opts.FramerBuilder = fb
    62  	}
    63  }
    64  
    65  // WithListenAddress returns a ListenServerOption which sets listening address.
    66  func WithListenAddress(address string) ListenServeOption {
    67  	return func(opts *ListenServeOptions) {
    68  		opts.Address = address
    69  	}
    70  }
    71  
    72  // WithListenNetwork returns a ListenServeOption which sets listen network.
    73  func WithListenNetwork(network string) ListenServeOption {
    74  	return func(opts *ListenServeOptions) {
    75  		opts.Network = network
    76  	}
    77  }
    78  
    79  // WithListener returns a ListenServeOption which allows users to use their customized listener for
    80  // specific accept/read/write logics.
    81  func WithListener(lis net.Listener) ListenServeOption {
    82  	return func(opts *ListenServeOptions) {
    83  		opts.Listener = lis
    84  	}
    85  }
    86  
    87  // WithHandler returns a ListenServeOption which sets business Handler.
    88  func WithHandler(handler Handler) ListenServeOption {
    89  	return func(opts *ListenServeOptions) {
    90  		opts.Handler = handler
    91  	}
    92  }
    93  
    94  // WithServeTLS returns a ListenServeOption which sets TLS relatives.
    95  func WithServeTLS(certFile, keyFile, caFile string) ListenServeOption {
    96  	return func(opts *ListenServeOptions) {
    97  		opts.TLSCertFile = certFile
    98  		opts.TLSKeyFile = keyFile
    99  		opts.CACertFile = caFile
   100  	}
   101  }
   102  
   103  // WithServerAsync returns a ListenServeOption which enables server async.
   104  // When another frameworks call trpc, they may use long connections. tRPC server can not handle
   105  // them concurrently, thus timeout.
   106  // This option takes effect for each TCP connections.
   107  func WithServerAsync(serverAsync bool) ListenServeOption {
   108  	return func(opts *ListenServeOptions) {
   109  		opts.ServerAsync = serverAsync
   110  	}
   111  }
   112  
   113  // WithWritev returns a ListenServeOption which enables writev.
   114  func WithWritev(writev bool) ListenServeOption {
   115  	return func(opts *ListenServeOptions) {
   116  		opts.Writev = writev
   117  	}
   118  }
   119  
   120  // WithMaxRoutines returns a ListenServeOption which sets the max number of async goroutines.
   121  // It's recommended to reserve twice of expected goroutines, but no less than MAXPROCS. The default
   122  // value is (1<<31 - 1).
   123  // This option takes effect only when async mod is enabled. It's ignored on sync mod.
   124  func WithMaxRoutines(routines int) ListenServeOption {
   125  	return func(opts *ListenServeOptions) {
   126  		opts.Routines = routines
   127  	}
   128  }
   129  
   130  // WithCopyFrame returns a ListenServeOption which sets whether copy frames.
   131  // In stream RPC, even server use sync mod, stream is asynchronous, we need to copy frame to avoid
   132  // over writing.
   133  func WithCopyFrame(copyFrame bool) ListenServeOption {
   134  	return func(opts *ListenServeOptions) {
   135  		opts.CopyFrame = copyFrame
   136  	}
   137  }
   138  
   139  // WithDisableKeepAlives returns a ListenServeOption which disables keep-alives.
   140  func WithDisableKeepAlives(disable bool) ListenServeOption {
   141  	return func(options *ListenServeOptions) {
   142  		options.DisableKeepAlives = disable
   143  	}
   144  }
   145  
   146  // WithServerIdleTimeout returns a ListenServeOption which sets the server idle timeout.
   147  func WithServerIdleTimeout(timeout time.Duration) ListenServeOption {
   148  	return func(options *ListenServeOptions) {
   149  		options.IdleTimeout = timeout
   150  	}
   151  }