config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package embed
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "net/http"
  20. "net/url"
  21. "path/filepath"
  22. "strings"
  23. "time"
  24. "github.com/coreos/etcd/etcdserver"
  25. "github.com/coreos/etcd/pkg/cors"
  26. "github.com/coreos/etcd/pkg/netutil"
  27. "github.com/coreos/etcd/pkg/srv"
  28. "github.com/coreos/etcd/pkg/transport"
  29. "github.com/coreos/etcd/pkg/types"
  30. "github.com/ghodss/yaml"
  31. "google.golang.org/grpc"
  32. )
  33. const (
  34. ClusterStateFlagNew = "new"
  35. ClusterStateFlagExisting = "existing"
  36. DefaultName = "default"
  37. DefaultMaxSnapshots = 5
  38. DefaultMaxWALs = 5
  39. DefaultMaxTxnOps = uint(128)
  40. DefaultMaxRequestBytes = 1.5 * 1024 * 1024
  41. DefaultGRPCKeepAliveMinTime = 5 * time.Second
  42. DefaultGRPCKeepAliveInterval = 2 * time.Hour
  43. DefaultGRPCKeepAliveTimeout = 20 * time.Second
  44. DefaultListenPeerURLs = "http://localhost:2380"
  45. DefaultListenClientURLs = "http://localhost:2379"
  46. // maxElectionMs specifies the maximum value of election timeout.
  47. // More details are listed in ../Documentation/tuning.md#time-parameters.
  48. maxElectionMs = 50000
  49. )
  50. var (
  51. ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
  52. "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
  53. ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
  54. DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
  55. DefaultAdvertiseClientURLs = "http://localhost:2379"
  56. defaultHostname string
  57. defaultHostStatus error
  58. )
  59. func init() {
  60. defaultHostname, defaultHostStatus = netutil.GetDefaultHost()
  61. }
  62. // Config holds the arguments for configuring an etcd server.
  63. type Config struct {
  64. // member
  65. CorsInfo *cors.CORSInfo
  66. LPUrls, LCUrls []url.URL
  67. Dir string `json:"data-dir"`
  68. WalDir string `json:"wal-dir"`
  69. MaxSnapFiles uint `json:"max-snapshots"`
  70. MaxWalFiles uint `json:"max-wals"`
  71. Name string `json:"name"`
  72. SnapCount uint64 `json:"snapshot-count"`
  73. AutoCompactionRetention string `json:"auto-compaction-retention"`
  74. AutoCompactionMode string `json:"auto-compaction-mode"`
  75. // TickMs is the number of milliseconds between heartbeat ticks.
  76. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
  77. // make ticks a cluster wide configuration.
  78. TickMs uint `json:"heartbeat-interval"`
  79. ElectionMs uint `json:"election-timeout"`
  80. QuotaBackendBytes int64 `json:"quota-backend-bytes"`
  81. MaxTxnOps uint `json:"max-txn-ops"`
  82. MaxRequestBytes uint `json:"max-request-bytes"`
  83. // gRPC server options
  84. // GRPCKeepAliveMinTime is the minimum interval that a client should
  85. // wait before pinging server. When client pings "too fast", server
  86. // sends goaway and closes the connection (errors: too_many_pings,
  87. // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
  88. // Server expects client pings only when there is any active streams
  89. // (PermitWithoutStream is set false).
  90. GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
  91. // GRPCKeepAliveInterval is the frequency of server-to-client ping
  92. // to check if a connection is alive. Close a non-responsive connection
  93. // after an additional duration of Timeout. 0 to disable.
  94. GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
  95. // GRPCKeepAliveTimeout is the additional duration of wait
  96. // before closing a non-responsive connection. 0 to disable.
  97. GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`
  98. // clustering
  99. APUrls, ACUrls []url.URL
  100. ClusterState string `json:"initial-cluster-state"`
  101. DNSCluster string `json:"discovery-srv"`
  102. Dproxy string `json:"discovery-proxy"`
  103. Durl string `json:"discovery"`
  104. InitialCluster string `json:"initial-cluster"`
  105. InitialClusterToken string `json:"initial-cluster-token"`
  106. StrictReconfigCheck bool `json:"strict-reconfig-check"`
  107. EnableV2 bool `json:"enable-v2"`
  108. // security
  109. ClientTLSInfo transport.TLSInfo
  110. ClientAutoTLS bool
  111. PeerTLSInfo transport.TLSInfo
  112. PeerAutoTLS bool
  113. // debug
  114. Debug bool `json:"debug"`
  115. LogPkgLevels string `json:"log-package-levels"`
  116. EnablePprof bool `json:"enable-pprof"`
  117. Metrics string `json:"metrics"`
  118. ListenMetricsUrls []url.URL
  119. ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`
  120. // ForceNewCluster starts a new cluster even if previously started; unsafe.
  121. ForceNewCluster bool `json:"force-new-cluster"`
  122. // UserHandlers is for registering users handlers and only used for
  123. // embedding etcd into other applications.
  124. // The map key is the route path for the handler, and
  125. // you must ensure it can't be conflicted with etcd's.
  126. UserHandlers map[string]http.Handler `json:"-"`
  127. // ServiceRegister is for registering users' gRPC services. A simple usage example:
  128. // cfg := embed.NewConfig()
  129. // cfg.ServerRegister = func(s *grpc.Server) {
  130. // pb.RegisterFooServer(s, &fooServer{})
  131. // pb.RegisterBarServer(s, &barServer{})
  132. // }
  133. // embed.StartEtcd(cfg)
  134. ServiceRegister func(*grpc.Server) `json:"-"`
  135. // auth
  136. AuthToken string `json:"auth-token"`
  137. // Experimental flags
  138. ExperimentalCorruptCheckTime time.Duration `json:"experimental-corrupt-check-time"`
  139. ExperimentalEnableV2V3 string `json:"experimental-enable-v2v3"`
  140. }
  141. // configYAML holds the config suitable for yaml parsing
  142. type configYAML struct {
  143. Config
  144. configJSON
  145. }
  146. // configJSON has file options that are translated into Config options
  147. type configJSON struct {
  148. LPUrlsJSON string `json:"listen-peer-urls"`
  149. LCUrlsJSON string `json:"listen-client-urls"`
  150. CorsJSON string `json:"cors"`
  151. APUrlsJSON string `json:"initial-advertise-peer-urls"`
  152. ACUrlsJSON string `json:"advertise-client-urls"`
  153. ClientSecurityJSON securityConfig `json:"client-transport-security"`
  154. PeerSecurityJSON securityConfig `json:"peer-transport-security"`
  155. }
  156. type securityConfig struct {
  157. CAFile string `json:"ca-file"`
  158. CertFile string `json:"cert-file"`
  159. KeyFile string `json:"key-file"`
  160. CertAuth bool `json:"client-cert-auth"`
  161. TrustedCAFile string `json:"trusted-ca-file"`
  162. AutoTLS bool `json:"auto-tls"`
  163. }
  164. // NewConfig creates a new Config populated with default values.
  165. func NewConfig() *Config {
  166. lpurl, _ := url.Parse(DefaultListenPeerURLs)
  167. apurl, _ := url.Parse(DefaultInitialAdvertisePeerURLs)
  168. lcurl, _ := url.Parse(DefaultListenClientURLs)
  169. acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
  170. cfg := &Config{
  171. CorsInfo: &cors.CORSInfo{},
  172. MaxSnapFiles: DefaultMaxSnapshots,
  173. MaxWalFiles: DefaultMaxWALs,
  174. Name: DefaultName,
  175. SnapCount: etcdserver.DefaultSnapCount,
  176. MaxTxnOps: DefaultMaxTxnOps,
  177. MaxRequestBytes: DefaultMaxRequestBytes,
  178. GRPCKeepAliveMinTime: DefaultGRPCKeepAliveMinTime,
  179. GRPCKeepAliveInterval: DefaultGRPCKeepAliveInterval,
  180. GRPCKeepAliveTimeout: DefaultGRPCKeepAliveTimeout,
  181. TickMs: 100,
  182. ElectionMs: 1000,
  183. LPUrls: []url.URL{*lpurl},
  184. LCUrls: []url.URL{*lcurl},
  185. APUrls: []url.URL{*apurl},
  186. ACUrls: []url.URL{*acurl},
  187. ClusterState: ClusterStateFlagNew,
  188. InitialClusterToken: "etcd-cluster",
  189. StrictReconfigCheck: true,
  190. Metrics: "basic",
  191. EnableV2: true,
  192. AuthToken: "simple",
  193. }
  194. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  195. return cfg
  196. }
  197. func ConfigFromFile(path string) (*Config, error) {
  198. cfg := &configYAML{Config: *NewConfig()}
  199. if err := cfg.configFromFile(path); err != nil {
  200. return nil, err
  201. }
  202. return &cfg.Config, nil
  203. }
  204. func (cfg *configYAML) configFromFile(path string) error {
  205. b, err := ioutil.ReadFile(path)
  206. if err != nil {
  207. return err
  208. }
  209. defaultInitialCluster := cfg.InitialCluster
  210. err = yaml.Unmarshal(b, cfg)
  211. if err != nil {
  212. return err
  213. }
  214. if cfg.LPUrlsJSON != "" {
  215. u, err := types.NewURLs(strings.Split(cfg.LPUrlsJSON, ","))
  216. if err != nil {
  217. plog.Fatalf("unexpected error setting up listen-peer-urls: %v", err)
  218. }
  219. cfg.LPUrls = []url.URL(u)
  220. }
  221. if cfg.LCUrlsJSON != "" {
  222. u, err := types.NewURLs(strings.Split(cfg.LCUrlsJSON, ","))
  223. if err != nil {
  224. plog.Fatalf("unexpected error setting up listen-client-urls: %v", err)
  225. }
  226. cfg.LCUrls = []url.URL(u)
  227. }
  228. if cfg.CorsJSON != "" {
  229. if err := cfg.CorsInfo.Set(cfg.CorsJSON); err != nil {
  230. plog.Panicf("unexpected error setting up cors: %v", err)
  231. }
  232. }
  233. if cfg.APUrlsJSON != "" {
  234. u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
  235. if err != nil {
  236. plog.Fatalf("unexpected error setting up initial-advertise-peer-urls: %v", err)
  237. }
  238. cfg.APUrls = []url.URL(u)
  239. }
  240. if cfg.ACUrlsJSON != "" {
  241. u, err := types.NewURLs(strings.Split(cfg.ACUrlsJSON, ","))
  242. if err != nil {
  243. plog.Fatalf("unexpected error setting up advertise-peer-urls: %v", err)
  244. }
  245. cfg.ACUrls = []url.URL(u)
  246. }
  247. if cfg.ListenMetricsUrlsJSON != "" {
  248. u, err := types.NewURLs(strings.Split(cfg.ListenMetricsUrlsJSON, ","))
  249. if err != nil {
  250. plog.Fatalf("unexpected error setting up listen-metrics-urls: %v", err)
  251. }
  252. cfg.ListenMetricsUrls = []url.URL(u)
  253. }
  254. // If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
  255. if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
  256. cfg.InitialCluster = ""
  257. }
  258. if cfg.ClusterState == "" {
  259. cfg.ClusterState = ClusterStateFlagNew
  260. }
  261. copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
  262. tls.CAFile = ysc.CAFile
  263. tls.CertFile = ysc.CertFile
  264. tls.KeyFile = ysc.KeyFile
  265. tls.ClientCertAuth = ysc.CertAuth
  266. tls.TrustedCAFile = ysc.TrustedCAFile
  267. }
  268. copySecurityDetails(&cfg.ClientTLSInfo, &cfg.ClientSecurityJSON)
  269. copySecurityDetails(&cfg.PeerTLSInfo, &cfg.PeerSecurityJSON)
  270. cfg.ClientAutoTLS = cfg.ClientSecurityJSON.AutoTLS
  271. cfg.PeerAutoTLS = cfg.PeerSecurityJSON.AutoTLS
  272. return cfg.Validate()
  273. }
  274. func (cfg *Config) Validate() error {
  275. if err := checkBindURLs(cfg.LPUrls); err != nil {
  276. return err
  277. }
  278. if err := checkBindURLs(cfg.LCUrls); err != nil {
  279. return err
  280. }
  281. if err := checkBindURLs(cfg.ListenMetricsUrls); err != nil {
  282. return err
  283. }
  284. if err := checkHostURLs(cfg.APUrls); err != nil {
  285. // TODO: return err in v3.4
  286. addrs := make([]string, len(cfg.APUrls))
  287. for i := range cfg.APUrls {
  288. addrs[i] = cfg.APUrls[i].String()
  289. }
  290. plog.Warningf("advertise-peer-urls %q is deprecated (%v)", strings.Join(addrs, ","), err)
  291. }
  292. if err := checkHostURLs(cfg.ACUrls); err != nil {
  293. // TODO: return err in v3.4
  294. addrs := make([]string, len(cfg.ACUrls))
  295. for i := range cfg.ACUrls {
  296. addrs[i] = cfg.ACUrls[i].String()
  297. }
  298. plog.Warningf("advertise-client-urls %q is deprecated (%v)", strings.Join(addrs, ","), err)
  299. }
  300. // Check if conflicting flags are passed.
  301. nSet := 0
  302. for _, v := range []bool{cfg.Durl != "", cfg.InitialCluster != "", cfg.DNSCluster != ""} {
  303. if v {
  304. nSet++
  305. }
  306. }
  307. if cfg.ClusterState != ClusterStateFlagNew && cfg.ClusterState != ClusterStateFlagExisting {
  308. return fmt.Errorf("unexpected clusterState %q", cfg.ClusterState)
  309. }
  310. if nSet > 1 {
  311. return ErrConflictBootstrapFlags
  312. }
  313. if 5*cfg.TickMs > cfg.ElectionMs {
  314. return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
  315. }
  316. if cfg.ElectionMs > maxElectionMs {
  317. return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
  318. }
  319. // check this last since proxying in etcdmain may make this OK
  320. if cfg.LCUrls != nil && cfg.ACUrls == nil {
  321. return ErrUnsetAdvertiseClientURLsFlag
  322. }
  323. return nil
  324. }
  325. // PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
  326. func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error) {
  327. token = cfg.InitialClusterToken
  328. switch {
  329. case cfg.Durl != "":
  330. urlsmap = types.URLsMap{}
  331. // If using discovery, generate a temporary cluster based on
  332. // self's advertised peer URLs
  333. urlsmap[cfg.Name] = cfg.APUrls
  334. token = cfg.Durl
  335. case cfg.DNSCluster != "":
  336. clusterStrs, cerr := srv.GetCluster("etcd-server", cfg.Name, cfg.DNSCluster, cfg.APUrls)
  337. if cerr != nil {
  338. plog.Errorf("couldn't resolve during SRV discovery (%v)", cerr)
  339. return nil, "", cerr
  340. }
  341. for _, s := range clusterStrs {
  342. plog.Noticef("got bootstrap from DNS for etcd-server at %s", s)
  343. }
  344. clusterStr := strings.Join(clusterStrs, ",")
  345. if strings.Contains(clusterStr, "https://") && cfg.PeerTLSInfo.CAFile == "" {
  346. cfg.PeerTLSInfo.ServerName = cfg.DNSCluster
  347. }
  348. urlsmap, err = types.NewURLsMap(clusterStr)
  349. // only etcd member must belong to the discovered cluster.
  350. // proxy does not need to belong to the discovered cluster.
  351. if which == "etcd" {
  352. if _, ok := urlsmap[cfg.Name]; !ok {
  353. return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.Name)
  354. }
  355. }
  356. default:
  357. // We're statically configured, and cluster has appropriately been set.
  358. urlsmap, err = types.NewURLsMap(cfg.InitialCluster)
  359. }
  360. return urlsmap, token, err
  361. }
  362. func (cfg Config) InitialClusterFromName(name string) (ret string) {
  363. if len(cfg.APUrls) == 0 {
  364. return ""
  365. }
  366. n := name
  367. if name == "" {
  368. n = DefaultName
  369. }
  370. for i := range cfg.APUrls {
  371. ret = ret + "," + n + "=" + cfg.APUrls[i].String()
  372. }
  373. return ret[1:]
  374. }
  375. func (cfg Config) IsNewCluster() bool { return cfg.ClusterState == ClusterStateFlagNew }
  376. func (cfg Config) ElectionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }
  377. func (cfg Config) defaultPeerHost() bool {
  378. return len(cfg.APUrls) == 1 && cfg.APUrls[0].String() == DefaultInitialAdvertisePeerURLs
  379. }
  380. func (cfg Config) defaultClientHost() bool {
  381. return len(cfg.ACUrls) == 1 && cfg.ACUrls[0].String() == DefaultAdvertiseClientURLs
  382. }
  383. func (cfg *Config) ClientSelfCert() (err error) {
  384. if cfg.ClientAutoTLS && cfg.ClientTLSInfo.Empty() {
  385. chosts := make([]string, len(cfg.LCUrls))
  386. for i, u := range cfg.LCUrls {
  387. chosts[i] = u.Host
  388. }
  389. cfg.ClientTLSInfo, err = transport.SelfCert(filepath.Join(cfg.Dir, "fixtures", "client"), chosts)
  390. return err
  391. } else if cfg.ClientAutoTLS {
  392. plog.Warningf("ignoring client auto TLS since certs given")
  393. }
  394. return nil
  395. }
  396. func (cfg *Config) PeerSelfCert() (err error) {
  397. if cfg.PeerAutoTLS && cfg.PeerTLSInfo.Empty() {
  398. phosts := make([]string, len(cfg.LPUrls))
  399. for i, u := range cfg.LPUrls {
  400. phosts[i] = u.Host
  401. }
  402. cfg.PeerTLSInfo, err = transport.SelfCert(filepath.Join(cfg.Dir, "fixtures", "peer"), phosts)
  403. return err
  404. } else if cfg.PeerAutoTLS {
  405. plog.Warningf("ignoring peer auto TLS since certs given")
  406. }
  407. return nil
  408. }
  409. // UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host,
  410. // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
  411. // e.g. advertise peer URL localhost:2380 or listen peer URL 0.0.0.0:2380
  412. // then the advertise peer host would be updated with machine's default host,
  413. // while keeping the listen URL's port.
  414. // User can work around this by explicitly setting URL with 127.0.0.1.
  415. // It returns the default hostname, if used, and the error, if any, from getting the machine's default host.
  416. // TODO: check whether fields are set instead of whether fields have default value
  417. func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error) {
  418. if defaultHostname == "" || defaultHostStatus != nil {
  419. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  420. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  421. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  422. }
  423. return "", defaultHostStatus
  424. }
  425. used := false
  426. pip, pport := cfg.LPUrls[0].Hostname(), cfg.LPUrls[0].Port()
  427. if cfg.defaultPeerHost() && pip == "0.0.0.0" {
  428. cfg.APUrls[0] = url.URL{Scheme: cfg.APUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, pport)}
  429. used = true
  430. }
  431. // update 'initial-cluster' when only the name is specified (e.g. 'etcd --name=abc')
  432. if cfg.Name != DefaultName && cfg.InitialCluster == defaultInitialCluster {
  433. cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
  434. }
  435. cip, cport := cfg.LCUrls[0].Hostname(), cfg.LCUrls[0].Port()
  436. if cfg.defaultClientHost() && cip == "0.0.0.0" {
  437. cfg.ACUrls[0] = url.URL{Scheme: cfg.ACUrls[0].Scheme, Host: fmt.Sprintf("%s:%s", defaultHostname, cport)}
  438. used = true
  439. }
  440. dhost := defaultHostname
  441. if !used {
  442. dhost = ""
  443. }
  444. return dhost, defaultHostStatus
  445. }
  446. // checkBindURLs returns an error if any URL uses a domain name.
  447. // TODO: return error in 3.2.0
  448. func checkBindURLs(urls []url.URL) error {
  449. for _, url := range urls {
  450. if url.Scheme == "unix" || url.Scheme == "unixs" {
  451. continue
  452. }
  453. host, _, err := net.SplitHostPort(url.Host)
  454. if err != nil {
  455. return err
  456. }
  457. if host == "localhost" {
  458. // special case for local address
  459. // TODO: support /etc/hosts ?
  460. continue
  461. }
  462. if net.ParseIP(host) == nil {
  463. return fmt.Errorf("expected IP in URL for binding (%s)", url.String())
  464. }
  465. }
  466. return nil
  467. }
  468. func checkHostURLs(urls []url.URL) error {
  469. for _, url := range urls {
  470. host, _, err := net.SplitHostPort(url.Host)
  471. if err != nil {
  472. return err
  473. }
  474. if host == "" {
  475. return fmt.Errorf("unexpected empty host (%s)", url.String())
  476. }
  477. }
  478. return nil
  479. }