@@ -11,6 +11,7 @@ import (
1111 "net/http"
1212 "os"
1313 "strconv"
14+ "time"
1415
1516 "golang.org/x/oauth2"
1617 "golang.org/x/oauth2/google"
@@ -187,3 +188,41 @@ func (ds *DialSettings) GetUniverseDomain() string {
187188func (ds * DialSettings ) IsUniverseDomainGDU () bool {
188189 return ds .GetUniverseDomain () == ds .GetDefaultUniverseDomain ()
189190}
191+
192+ // GetUniverseDomain returns the default service domain for a given Cloud
193+ // universe, from google.Credentials, for comparison with the value returned by
194+ // (*DialSettings).GetUniverseDomain. This wrapper function should be removed
195+ // to close [TODO(chrisdsmith): issue link here]. See details below.
196+ func GetUniverseDomain (creds * google.Credentials ) (string , error ) {
197+ timer := time .NewTimer (time .Second )
198+ defer timer .Stop ()
199+ errors := make (chan error )
200+ results := make (chan string )
201+
202+ go func () {
203+ result , err := creds .GetUniverseDomain ()
204+ if err != nil {
205+ errors <- err
206+ return
207+ }
208+ results <- result
209+ }()
210+
211+ select {
212+ case err := <- errors :
213+ // An error that is returned before the timer expires is legitimate.
214+ return "" , err
215+ case res := <- results :
216+ return res , nil
217+ case <- timer .C : // Timer is expired.
218+ // If err or res was not returned, it means that creds.GetUniverseDomain()
219+ // did not complete in 1s. Assume that MDS is likely never responding to
220+ // the endpoint and will timeout. This is the source of issues such as
221+ // https://github.com/googleapis/google-cloud-go/issues/9350.
222+ // Temporarily (2024-02-02) return the GDU domain. Restore the original
223+ // calls to creds.GetUniverseDomain() in grpc/dial.go and http/dial.go
224+ // and remove this method to close
225+ // https://github.com/googleapis/google-api-go-client/issues/2399.
226+ return universeDomainDefault , nil
227+ }
228+ }
0 commit comments