github.com/kaydxh/golang@v0.0.131/go/net/resolver/register.reslover.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package resolver 23 24 import "sync" 25 26 var ( 27 builderMu sync.RWMutex 28 resolversMu sync.RWMutex 29 // m is a map from scheme to resolver builder. 30 m = make(map[string]Builder) 31 resolverPool = make(map[string]Resolver) 32 // defaultScheme is the default scheme to use. 33 defaultScheme = "passthrough" 34 ) 35 36 // TODO(bar) install dns resolver in init(){}. 37 38 // Register registers the resolver builder to the resolver map. b.Scheme will be 39 // used as the scheme registered with this builder. 40 // 41 func Register(b Builder) { 42 builderMu.Lock() 43 defer builderMu.Unlock() 44 45 if b == nil { 46 panic("register builder is nil") 47 } 48 if _, ok := m[b.Scheme()]; ok { 49 panic("double register scheme " + b.Scheme()) 50 } 51 m[b.Scheme()] = b 52 } 53 54 // Get returns the resolver builder registered with the given scheme. 55 // 56 // If no builder is register with the scheme, nil will be returned. 57 func Get(scheme string) Builder { 58 builderMu.Lock() 59 defer builderMu.Unlock() 60 61 if b, ok := m[scheme]; ok { 62 return b 63 } 64 return nil 65 } 66 67 func GetDefault() Builder { 68 builderMu.Lock() 69 defer builderMu.Unlock() 70 71 if b, ok := m[defaultScheme]; ok { 72 return b 73 } 74 return nil 75 } 76 77 func getResolver(target string) Resolver { 78 resolversMu.Lock() 79 defer resolversMu.Unlock() 80 81 if b, ok := resolverPool[target]; ok { 82 return b 83 } 84 return nil 85 } 86 87 func setResolver(target string, r Resolver) { 88 resolversMu.Lock() 89 defer resolversMu.Unlock() 90 91 if r == nil { 92 panic("register resolver is nil") 93 } 94 if _, ok := resolverPool[target]; ok { 95 panic("double register target " + target) 96 } 97 resolverPool[target] = r 98 } 99 100 // SetDefaultScheme sets the default scheme that will be used. The default 101 // default scheme is "passthrough". 102 // 103 // NOTE: this function must only be called during initialization time (i.e. in 104 // an init() function), and is not thread-safe. The scheme set last overrides 105 // previously set values. 106 func SetDefaultScheme(scheme string) { 107 defaultScheme = scheme 108 } 109 110 // GetDefaultScheme gets the default scheme that will be used. 111 func GetDefaultScheme() string { 112 return defaultScheme 113 }