예제 #1
0
func NewBackend(cf *config.HttpBackend) (*Backend, error) {
	balancer, servers := NewBalancer(cf)
	proxy := &ReverseProxy{
		Director: func(req *http.Request) {
			req.URL.Scheme = "http"
			req.URL.Host = req.Host
		},
		Transport: balancer,
	}
	ch := &stats.CountersCollectingHandler{
		Handler:     proxy,
		RateLimiter: stats.NewRateLimiter(cf.Maxrate),
		Limiter:     stats.NewLimiter(int(cf.Maxconn)),
	}
	b := &Backend{
		Cf:          cf,
		proxy:       ch,
		balancer:    balancer,
		Counting:    ch,
		RateLimiter: ch.RateLimiter,
		Limiter:     ch.Limiter,
		Servers:     servers,
	}
	return b, nil
}
예제 #2
0
func NewStoppableListener(l *net.TCPListener, maxrate float64, maxallowed int64) *StoppableListener {
	if maxrate == 0 {
		maxrate = 999999
	}
	return &StoppableListener{
		TCPListener: l,
		stop:        make(chan int),
		RateLimiter: stats.NewRateLimiter(maxrate),
		Limiter:     stats.NewLimiter(int(maxallowed)),
	}
}
예제 #3
0
func NewServer(backendName string, cf *config.Server, onStateUpdate func()) *Server {
	t := transportForBackend(cf.Address)
	ct := &stats.CountersCollectingRoundTripper{
		RoundTripper: t,
		RateLimiter:  stats.NewRateLimiter(cf.Maxrate),
		Limiter:      stats.NewLimiter(int(cf.Maxconn)),
		TraceFamily:  "server." + backendName + "." + cf.Address,
	}
	//TODO: insert limiters here
	//TODO: make prober url configurable
	proberUrl := fmt.Sprintf("http://%s/", cf.Address)
	prober := &HttpHealthChecker{Transport: t, Url: proberUrl, onStateUpdate: onStateUpdate}
	prober.Run()
	return &Server{
		Cf:            cf,
		RoundTripper:  ct,
		Counting:      ct,
		RateLimiter:   ct.RateLimiter,
		Limiter:       ct.Limiter,
		HealthChecker: prober,
	}
}