github.com/qiniu/go-sdk/v7@v7.27.0/storagev2/chooser/chooser.go (about) 1 package chooser 2 3 import ( 4 "context" 5 "net" 6 ) 7 8 type ( 9 // ChooseOptions 选择器的选项 10 ChooseOptions struct { 11 // Domain 是待选择的域名 12 Domain string 13 14 // 如果找不到合适的域名就直接返回空 15 FailFast bool 16 } 17 18 // FeedbackOptions 反馈的选项 19 FeedbackOptions struct { 20 // Domain 是待反馈的域名 21 Domain string 22 } 23 24 // Chooser 选择器接口 25 Chooser interface { 26 // Choose 从给定的 IP 地址列表中选择一批 IP 地址用于发送请求 27 Choose(context.Context, []net.IP, *ChooseOptions) []net.IP 28 29 // FeedbackGood 反馈一批 IP 地址请求成功 30 FeedbackGood(context.Context, []net.IP, *FeedbackOptions) 31 32 // FeedbackBad 反馈一批 IP 地址请求失败 33 FeedbackBad(context.Context, []net.IP, *FeedbackOptions) 34 } 35 36 directChooser struct{} 37 ) 38 39 // NewDirectChooser 创建直接选择器 40 func NewDirectChooser() Chooser { 41 return &directChooser{} 42 } 43 44 func (chooser *directChooser) Choose(_ context.Context, ips []net.IP, _ *ChooseOptions) []net.IP { 45 return ips 46 } 47 48 func (chooser *directChooser) FeedbackGood(_ context.Context, _ []net.IP, _ *FeedbackOptions) { 49 // do nothing 50 } 51 52 func (chooser *directChooser) FeedbackBad(_ context.Context, _ []net.IP, _ *FeedbackOptions) { 53 // do nothing 54 } 55 56 func makeSet(ips []net.IP, domain string, makeKey func(string, net.IP) string) map[string]struct{} { 57 m := make(map[string]struct{}, len(ips)) 58 for _, ip := range ips { 59 m[makeKey(domain, ip)] = struct{}{} 60 } 61 return m 62 }