MCPcopy
hub / github.com/grafana/dskit / NewTCPTransport

Function NewTCPTransport

kv/memberlist/tcp_transport.go:135–217  ·  view source on GitHub ↗

NewTCPTransport returns a new tcp-based transport with the given configuration. On success all the network listeners will be created and listening.

(config TCPTransportConfig, logger log.Logger, registerer prometheus.Registerer)

Source from the content-addressed store, hash-verified

133// NewTCPTransport returns a new tcp-based transport with the given configuration. On
134// success all the network listeners will be created and listening.
135func NewTCPTransport(config TCPTransportConfig, logger log.Logger, registerer prometheus.Registerer) (*TCPTransport, error) {
136 if len(config.BindAddrs) == 0 {
137 config.BindAddrs = []string{zeroZeroZeroZero}
138 }
139
140 // Build out the new transport.
141 var ok bool
142 concurrentWrites := config.MaxConcurrentWrites
143 if concurrentWrites <= 0 {
144 concurrentWrites = 1
145 }
146 t := TCPTransport{
147 cfg: config,
148 logger: log.With(logger, "component", "memberlist TCPTransport"),
149 packetCh: make(chan *memberlist.Packet),
150 connCh: make(chan net.Conn),
151 writeCh: make(chan writeRequest),
152 }
153
154 for i := 0; i < concurrentWrites; i++ {
155 t.writeWG.Add(1)
156 go t.writeWorker()
157 }
158
159 var err error
160 if config.TLSEnabled {
161 t.tlsConfig, err = config.TLS.GetTLSConfig()
162 if err != nil {
163 return nil, errors.Wrap(err, "unable to create TLS config")
164 }
165 }
166
167 t.registerMetrics(registerer)
168
169 // Clean up listeners if there's an error.
170 defer func() {
171 if !ok {
172 _ = t.Shutdown()
173 }
174 }()
175
176 // Build all the TCP and UDP listeners.
177 port := config.BindPort
178 for _, addr := range config.BindAddrs {
179 ip := net.ParseIP(addr)
180 if ip == nil {
181 return nil, fmt.Errorf("could not parse bind addr %q as IP address", addr)
182 }
183
184 tcpAddr := &net.TCPAddr{IP: ip, Port: port}
185
186 var tcpLn net.Listener
187 if config.TLSEnabled {
188 tcpLn, err = tls.Listen("tcp", tcpAddr.String(), t.tlsConfig)
189 if err != nil {
190 return nil, errors.Wrapf(err, "failed to start TLS TCP listener on %q port %d", addr, port)
191 }
192 } else {

Calls 10

writeWorkerMethod · 0.95
registerMetricsMethod · 0.95
ShutdownMethod · 0.95
tcpListenMethod · 0.95
WithMethod · 0.80
GetTLSConfigMethod · 0.80
ErrorfMethod · 0.80
AddMethod · 0.65
WrapMethod · 0.65
StringMethod · 0.65