internal.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. *
  3. * Copyright 2023 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package internal contains functionality internal to the dns resolver package.
  19. package internal
  20. import (
  21. "context"
  22. "errors"
  23. "net"
  24. "time"
  25. )
  26. // NetResolver groups the methods on net.Resolver that are used by the DNS
  27. // resolver implementation. This allows the default net.Resolver instance to be
  28. // overidden from tests.
  29. type NetResolver interface {
  30. LookupHost(ctx context.Context, host string) (addrs []string, err error)
  31. LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error)
  32. LookupTXT(ctx context.Context, name string) (txts []string, err error)
  33. }
  34. var (
  35. // ErrMissingAddr is the error returned when building a DNS resolver when
  36. // the provided target name is empty.
  37. ErrMissingAddr = errors.New("dns resolver: missing address")
  38. // ErrEndsWithColon is the error returned when building a DNS resolver when
  39. // the provided target name ends with a colon that is supposed to be the
  40. // separator between host and port. E.g. "::" is a valid address as it is
  41. // an IPv6 address (host only) and "[::]:" is invalid as it ends with a
  42. // colon as the host and port separator
  43. ErrEndsWithColon = errors.New("dns resolver: missing port after port-separator colon")
  44. )
  45. // The following vars are overridden from tests.
  46. var (
  47. // MinResolutionRate is the minimum rate at which re-resolutions are
  48. // allowed. This helps to prevent excessive re-resolution.
  49. MinResolutionRate = 30 * time.Second
  50. // TimeAfterFunc is used by the DNS resolver to wait for the given duration
  51. // to elapse. In non-test code, this is implemented by time.After. In test
  52. // code, this can be used to control the amount of time the resolver is
  53. // blocked waiting for the duration to elapse.
  54. TimeAfterFunc func(time.Duration) <-chan time.Time
  55. // NewNetResolver returns the net.Resolver instance for the given target.
  56. NewNetResolver func(string) (NetResolver, error)
  57. // AddressDialer is the dialer used to dial the DNS server. It accepts the
  58. // Host portion of the URL corresponding to the user's dial target and
  59. // returns a dial function.
  60. AddressDialer func(address string) func(context.Context, string, string) (net.Conn, error)
  61. )