Example #1
0
func NewClient(uri, key string) (*Client, error) {
	if uri == "" {
		uri = "discoverd+http://flynn-controller"
	}
	u, err := url.Parse(uri)
	if err != nil {
		return nil, err
	}
	c := &Client{
		url:  uri,
		addr: u.Host,
		http: http.DefaultClient,
		key:  key,
	}
	if u.Scheme == "discoverd+http" {
		if err := discoverd.Connect(""); err != nil {
			return nil, err
		}
		dialer := dialer.New(discoverd.DefaultClient, nil)
		c.dial = dialer.Dial
		c.dialClose = dialer
		c.http = &http.Client{Transport: &http.Transport{Dial: c.dial}}
		u.Scheme = "http"
		c.url = u.String()
	}
	return c, nil
}
Example #2
0
func NewWithDiscoverd(name string, dc dialer.DiscoverdClient) Client {
	if name == "" {
		name = "strowger"
	}
	c := &client{
		dialer: dialer.New(dc, nil),
		url:    fmt.Sprintf("http://%s-api", name),
	}
	c.http = &http.Client{Transport: &http.Transport{Dial: c.dialer.Dial}}
	return c
}
func (a *WaitAction) Run(s *State) error {
	const waitMax = time.Minute
	const waitInterval = 500 * time.Millisecond

	if a.Status == 0 {
		a.Status = 200
	}

	u, err := url.Parse(a.URL)
	if err != nil {
		return err
	}
	httpc := http.DefaultClient
	if u.Scheme == "discoverd+http" {
		if err := discoverd.Connect(""); err != nil {
			return err
		}
		d := dialer.New(discoverd.DefaultClient, nil)
		defer d.Close()
		httpc = &http.Client{Transport: &http.Transport{Dial: d.Dial}}
		u.Scheme = "http"
	}

	start := time.Now()
	for {
		var result string
		res, err := httpc.Get(u.String())
		if err != nil {
			result = fmt.Sprintf("%q", err)
			goto fail
		}
		res.Body.Close()
		if res.StatusCode == a.Status {
			return nil
		}
		result = strconv.Itoa(res.StatusCode)

	fail:
		if time.Now().Sub(start) >= waitMax {
			return fmt.Errorf("bootstrap: timed out waiting for %s, last response %s", a.URL, result)
		}
		time.Sleep(waitInterval)
	}
}