Example #1
0
func (c *Client) Get(ctx context.Context, url string) (r *http.Response, err error) {
	ctx = trace.Enter(ctx, "HTTP")

	if maximum := c.MaximumInFlight; maximum != 0 {
		defer atomic.AddInt64(&c.inflight, -1)

		// too many in-flight?
		if n := atomic.AddInt64(&c.inflight, 1); n >= maximum {
			trace.Leave(ctx, "Errors.TooManyInFlight")
			return
		}
	}

	r, err = defaults.Client(c.Client).Get(url)
	if err != nil {
		atomic.AddInt64(&c.count, -1)
		trace.Error(ctx, "Errors.Fail", err)
		return
	}

	if r.StatusCode != http.StatusOK && r.StatusCode != http.StatusNoContent {
		atomic.AddInt64(&c.count, -1)
		trace.Error(ctx, "Errors.Status", fmt.Errorf("%s", r.Status))
		return
	}

	trace.Leave(ctx, "Check")
	return
}
Example #2
0
func (c *Client) HealthCheck(url string) (err error) {
	r, err := defaults.Client(c.Client).Get(url)
	if err != nil {
		atomic.StoreInt64(&c.count, 0)
		//trace.Error(ctx, "Errors.Fail", err)
		log.Println("forensiq:", err)
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		atomic.StoreInt64(&c.count, 0)
		//trace.Error(ctx, "Errors.Down", err)
		log.Println("forensiq: down")
		return
	}

	if result := string(body); r.StatusCode != http.StatusOK || result[0] != '1' {
		atomic.StoreInt64(&c.count, 0)
		err = fmt.Errorf("%s: %s", r.Status, result)
		//trace.Error(ctx, "Errors.Status", err)
		log.Println("forensiq:", err)
		return
	}

	atomic.StoreInt64(&c.count, defaults.Int64(c.FailCount, 10))
	return
}