trpc.group/trpc-go/trpc-go@v1.0.3/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 // StopListening is used to instruct the server transport to stop listening. 48 StopListening <-chan struct{} 49 } 50 51 // ListenServeOption modifies the ListenServeOptions. 52 type ListenServeOption func(*ListenServeOptions) 53 54 // WithServiceName returns a ListenServeOption which sets the service name. 55 func WithServiceName(name string) ListenServeOption { 56 return func(opts *ListenServeOptions) { 57 opts.ServiceName = name 58 } 59 } 60 61 // WithServerFramerBuilder returns a ListenServeOption which sets server frame builder. 62 func WithServerFramerBuilder(fb codec.FramerBuilder) ListenServeOption { 63 return func(opts *ListenServeOptions) { 64 opts.FramerBuilder = fb 65 } 66 } 67 68 // WithListenAddress returns a ListenServerOption which sets listening address. 69 func WithListenAddress(address string) ListenServeOption { 70 return func(opts *ListenServeOptions) { 71 opts.Address = address 72 } 73 } 74 75 // WithListenNetwork returns a ListenServeOption which sets listen network. 76 func WithListenNetwork(network string) ListenServeOption { 77 return func(opts *ListenServeOptions) { 78 opts.Network = network 79 } 80 } 81 82 // WithListener returns a ListenServeOption which allows users to use their customized listener for 83 // specific accept/read/write logics. 84 func WithListener(lis net.Listener) ListenServeOption { 85 return func(opts *ListenServeOptions) { 86 opts.Listener = lis 87 } 88 } 89 90 // WithHandler returns a ListenServeOption which sets business Handler. 91 func WithHandler(handler Handler) ListenServeOption { 92 return func(opts *ListenServeOptions) { 93 opts.Handler = handler 94 } 95 } 96 97 // WithServeTLS returns a ListenServeOption which sets TLS relatives. 98 func WithServeTLS(certFile, keyFile, caFile string) ListenServeOption { 99 return func(opts *ListenServeOptions) { 100 opts.TLSCertFile = certFile 101 opts.TLSKeyFile = keyFile 102 opts.CACertFile = caFile 103 } 104 } 105 106 // WithServerAsync returns a ListenServeOption which enables server async. 107 // When another frameworks call trpc, they may use long connections. tRPC server can not handle 108 // them concurrently, thus timeout. 109 // This option takes effect for each TCP connections. 110 func WithServerAsync(serverAsync bool) ListenServeOption { 111 return func(opts *ListenServeOptions) { 112 opts.ServerAsync = serverAsync 113 } 114 } 115 116 // WithWritev returns a ListenServeOption which enables writev. 117 func WithWritev(writev bool) ListenServeOption { 118 return func(opts *ListenServeOptions) { 119 opts.Writev = writev 120 } 121 } 122 123 // WithMaxRoutines returns a ListenServeOption which sets the max number of async goroutines. 124 // It's recommended to reserve twice of expected goroutines, but no less than MAXPROCS. The default 125 // value is (1<<31 - 1). 126 // This option takes effect only when async mod is enabled. It's ignored on sync mod. 127 func WithMaxRoutines(routines int) ListenServeOption { 128 return func(opts *ListenServeOptions) { 129 opts.Routines = routines 130 } 131 } 132 133 // WithCopyFrame returns a ListenServeOption which sets whether copy frames. 134 // In stream RPC, even server use sync mod, stream is asynchronous, we need to copy frame to avoid 135 // over writing. 136 func WithCopyFrame(copyFrame bool) ListenServeOption { 137 return func(opts *ListenServeOptions) { 138 opts.CopyFrame = copyFrame 139 } 140 } 141 142 // WithDisableKeepAlives returns a ListenServeOption which disables keep-alives. 143 func WithDisableKeepAlives(disable bool) ListenServeOption { 144 return func(options *ListenServeOptions) { 145 options.DisableKeepAlives = disable 146 } 147 } 148 149 // WithServerIdleTimeout returns a ListenServeOption which sets the server idle timeout. 150 func WithServerIdleTimeout(timeout time.Duration) ListenServeOption { 151 return func(options *ListenServeOptions) { 152 options.IdleTimeout = timeout 153 } 154 } 155 156 // WithStopListening returns a ListenServeOption which notifies the transport to stop listening. 157 func WithStopListening(ch <-chan struct{}) ListenServeOption { 158 return func(options *ListenServeOptions) { 159 options.StopListening = ch 160 } 161 }