MCPcopy
hub / github.com/cloudflare/cloudflared / NewProtocolSelector

Function NewProtocolSelector

connection/protocol.go:223–260  ·  view source on GitHub ↗
(
	protocolFlag string,
	accountTag string,
	tunnelTokenProvided bool,
	protocolFetcher edgediscovery.PercentageFetcher,
	resolveTTL time.Duration,
	log *zerolog.Logger,
)

Source from the content-addressed store, hash-verified

221}
222
223func NewProtocolSelector(
224 protocolFlag string,
225 accountTag string,
226 tunnelTokenProvided bool,
227 protocolFetcher edgediscovery.PercentageFetcher,
228 resolveTTL time.Duration,
229 log *zerolog.Logger,
230) (ProtocolSelector, error) {
231 threshold := switchThreshold(accountTag)
232 fetchedProtocol, err := getProtocol(ProtocolList, protocolFetcher, threshold)
233 log.Debug().Msgf("Fetched protocol: %s", fetchedProtocol)
234 if err != nil {
235 log.Warn().Msg("Unable to lookup protocol percentage.")
236 // Falling through here since 'auto' is handled in the switch and failing
237 // to do the protocol lookup isn't a failure since it can be triggered again
238 // after the TTL.
239 }
240
241 // If the user picks a protocol, then we stick to it no matter what.
242 switch protocolFlag {
243 case "h2mux":
244 // Any users still requesting h2mux will be upgraded to http2 instead
245 log.Warn().Msg("h2mux is no longer a supported protocol: upgrading edge connection to http2. Please remove '--protocol h2mux' from runtime arguments to remove this warning.")
246 return &staticProtocolSelector{current: HTTP2}, nil
247 case QUIC.String():
248 return &staticProtocolSelector{current: QUIC}, nil
249 case HTTP2.String():
250 return &staticProtocolSelector{current: HTTP2}, nil
251 case AutoSelectFlag:
252 // When a --token is provided, we want to start with QUIC but have fallback to HTTP2
253 if tunnelTokenProvided {
254 return newDefaultProtocolSelector(QUIC), nil
255 }
256 return newRemoteProtocolSelector(fetchedProtocol, ProtocolList, threshold, protocolFetcher, resolveTTL, log), nil
257 }
258
259 return nil, fmt.Errorf("unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
260}
261
262func switchThreshold(accountTag string) int32 {
263 h := fnv.New32a()

Calls 6

getProtocolFunction · 0.85
ErrorfMethod · 0.80
switchThresholdFunction · 0.70
StringMethod · 0.65