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 = "router"
	}
	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
}
Example #3
0
func newDiscoverdClient(u *url.URL, key string) (*Client, error) {
	if err := discoverd.Connect(""); err != nil {
		return nil, err
	}
	u.Scheme = "http"
	d := dialer.New(discoverd.DefaultClient, nil)
	httpClient := &http.Client{Transport: &http.Transport{Dial: d.Dial}}
	c := newClient(u.Host, key, u.String(), httpClient)
	c.Dial = d.Dial
	c.DialClose = d
	return c, nil
}
Example #4
0
// NewWithDiscoverd uses the provided discoverd client and returns a client.
func NewWithDiscoverd(name string, dc dialer.DiscoverdClient) Client {
	if name == "" {
		name = "router"
	}
	dialer := dialer.New(dc, nil)
	c := newRouterClient()
	c.ErrPrefix = name
	c.Dial = dialer.Dial
	c.DialClose = dialer
	c.URL = fmt.Sprintf("http://%s-api", name)
	c.HTTP = &http.Client{Transport: &http.Transport{Dial: c.Dial}}
	return c
}
Example #5
0
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(interpolate(s, 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
		req, err := http.NewRequest("GET", u.String(), nil)
		if err != nil {
			return err
		}
		if a.Host != "" {
			req.Host = interpolate(s, a.Host)
		}
		res, err := httpc.Do(req)
		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)
	}
}
Example #6
0
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(interpolate(s, a.URL))
	if err != nil {
		return err
	}
	httpc := http.DefaultClient
	var dial dialer.DialFunc
	if u.Scheme == "tcp" {
		dial = net.Dial
	}
	if strings.HasPrefix(u.Scheme, "discoverd+") {
		if err := discoverd.Connect(""); err != nil {
			return err
		}
		d := dialer.New(discoverd.DefaultClient, nil)
		defer d.Close()
		dial = d.Dial

		switch u.Scheme {
		case "discoverd+http":
			httpc = &http.Client{Transport: &http.Transport{Dial: d.Dial}}
			u.Scheme = "http"
		case "discoverd+tcp":
			u.Scheme = "tcp"
		default:
			return fmt.Errorf("bootstrap: unknown protocol")
		}
	}

	start := time.Now()
	for {
		var result string

		switch u.Scheme {
		case "http":
			req, err := http.NewRequest("GET", u.String(), nil)
			if err != nil {
				return err
			}
			if a.Host != "" {
				req.Host = interpolate(s, a.Host)
			}
			res, err := httpc.Do(req)
			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)
		case "tcp":
			conn, err := dial("tcp", u.Host)
			if err != nil {
				result = fmt.Sprintf("%q", err)
				goto fail
			}
			conn.Close()
			return nil
		default:
			return fmt.Errorf("bootstrap: unknown protocol")
		}
	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)
	}
}