Пример #1
0
func (d *daemon) LoadConfig(newConfig config.Config) error {
	// We don't support changing the Proxy listen port because it would mean closing and
	// restarting the server. If users want to do this they should restart the process.
	if d.proxy.Listen != "" && d.proxy.Listen != newConfig.Proxy.Listen {
		return fmt.Errorf("SPHINX_LOAD_CONFIG_FAILED. Can't change listen port")
	}

	d.proxy = newConfig.Proxy
	d.healthCheck = newConfig.HealthCheck
	target, _ := url.Parse(d.proxy.Host) // already tested for invalid Host
	proxy := httputil.NewSingleHostReverseProxy(target)
	rateLimiter, err := ratelimiter.New(newConfig)
	if err != nil {
		return fmt.Errorf("SPHINX_LOAD_CONFIG_FAILED: %s", err.Error())
	}

	// Set the proxy and handler daemon fields
	var handler http.Handler
	switch d.proxy.Handler {
	case "http":
		handler = handlers.NewHTTPLimiter(rateLimiter, proxy, d.proxy.AllowOnError)
	case "httplogger":
		handler = handlers.NewHTTPLogger(rateLimiter, proxy)
	default:
		return fmt.Errorf("unrecognized handler %s", d.proxy.Handler)
	}

	d.handler = middleware.New(handler, common.Log, func(req *http.Request) map[string]interface{} {
		return map[string]interface{}{"guid": req.Header.Get("X-Request-Id")}
	})
	return nil
}
Пример #2
0
func setUpHTTPLimiter(b *testing.B) {
	config, err := config.New("example.yaml")
	if err != nil {
		b.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
	}
	rateLimiter, err := ratelimiter.New(config)
	if err != nil {
		b.Fatalf("SPHINX_INIT_FAILED: %s", err.Error())
	}

	// if configuration says that use http
	if config.Proxy.Handler != "http" {
		b.Fatalf("sphinx only supports the http handler")
	}

	// ignore the url in the config and use localhost
	target, _ := url.Parse(host)
	proxy := httputil.NewSingleHostReverseProxy(target)
	httpLimiter := handlers.NewHTTPLimiter(rateLimiter, proxy, false)

	go http.ListenAndServe(":8082", httpLimiter)
}