github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/csot_util.go (about) 1 // Copyright (C) MongoDB, Inc. 2022-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package internal 8 9 import ( 10 "context" 11 "time" 12 ) 13 14 type timeoutKey struct{} 15 16 // MakeTimeoutContext returns a new context with Client-Side Operation Timeout (CSOT) feature-gated behavior 17 // and a Timeout set to the passed in Duration. Setting a Timeout on a single operation is not supported in 18 // public API. 19 // 20 // TODO(GODRIVER-2348) We may be able to remove this function once CSOT feature-gated behavior becomes the 21 // TODO default behavior. 22 func MakeTimeoutContext(ctx context.Context, to time.Duration) (context.Context, context.CancelFunc) { 23 // Only use the passed in Duration as a timeout on the Context if it 24 // is non-zero. 25 cancelFunc := func() {} 26 if to != 0 { 27 ctx, cancelFunc = context.WithTimeout(ctx, to) 28 } 29 return context.WithValue(ctx, timeoutKey{}, true), cancelFunc 30 } 31 32 func IsTimeoutContext(ctx context.Context) bool { 33 return ctx.Value(timeoutKey{}) != nil 34 } 35 36 // ZeroRTTMonitor implements the RTTMonitor interface and is used internally for testing. It returns 0 for all 37 // RTT calculations and an empty string for RTT statistics. 38 type ZeroRTTMonitor struct{} 39 40 // EWMA implements the RTT monitor interface. 41 func (zrm *ZeroRTTMonitor) EWMA() time.Duration { 42 return 0 43 } 44 45 // Min implements the RTT monitor interface. 46 func (zrm *ZeroRTTMonitor) Min() time.Duration { 47 return 0 48 } 49 50 // P90 implements the RTT monitor interface. 51 func (zrm *ZeroRTTMonitor) P90() time.Duration { 52 return 0 53 } 54 55 // Stats implements the RTT monitor interface. 56 func (zrm *ZeroRTTMonitor) Stats() string { 57 return "" 58 }