Exemple #1
0
func NewProxy(args ProxyArgs) Proxy {
	routeServiceConfig := routeservice.NewRouteServiceConfig(args.Logger, args.RouteServiceEnabled, args.RouteServiceTimeout, args.Crypto, args.CryptoPrev, args.RouteServiceRecommendHttps)

	p := &proxy{
		accessLogger: args.AccessLogger,
		traceKey:     args.TraceKey,
		ip:           args.Ip,
		logger:       args.Logger,
		registry:     args.Registry,
		reporter:     args.Reporter,
		transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				conn, err := net.DialTimeout(network, addr, 5*time.Second)
				if err != nil {
					return conn, err
				}
				if args.EndpointTimeout > 0 {
					err = conn.SetDeadline(time.Now().Add(args.EndpointTimeout))
				}
				return conn, err
			},
			DisableKeepAlives:  true,
			DisableCompression: true,
			TLSClientConfig:    args.TLSConfig,
		},
		secureCookies:              args.SecureCookies,
		heartbeatOK:                args.HeartbeatOK, // 1->true, 0->false
		routeServiceConfig:         routeServiceConfig,
		extraHeadersToLog:          args.ExtraHeadersToLog,
		routeServiceRecommendHttps: args.RouteServiceRecommendHttps,
		healthCheckUserAgent:       args.HealthCheckUserAgent,
		forceForwardedProtoHttps:   args.ForceForwardedProtoHttps,
		defaultLoadBalance:         args.DefaultLoadBalance,
	}

	n := negroni.New()
	n.Use(&proxyWriterHandler{})
	n.Use(handlers.NewAccessLog(args.AccessLogger, args.ExtraHeadersToLog))
	n.Use(handlers.NewHealthcheck(args.HealthCheckUserAgent, p.heartbeatOK, args.Logger))
	n.Use(handlers.NewZipkin(args.EnableZipkin, args.ExtraHeadersToLog, args.Logger))

	n.UseHandler(p)
	handlers := &proxyHandler{
		handlers: n,
		proxy:    p,
	}

	return handlers
}
Exemple #2
0
func NewRouter(logger lager.Logger, cfg *config.Config, p proxy.Proxy, mbusClient *nats.Conn, r *registry.RouteRegistry,
	v varz.Varz, heartbeatOK *int32, logCounter *schema.LogCounter, errChan chan error) (*Router, error) {

	var host string
	if cfg.Status.Port != 0 {
		host = fmt.Sprintf("%s:%d", cfg.Status.Host, cfg.Status.Port)
	}

	varz := &health.Varz{
		UniqueVarz: v,
		GenericVarz: health.GenericVarz{
			Type:        "Router",
			Index:       cfg.Index,
			Host:        host,
			Credentials: []string{cfg.Status.User, cfg.Status.Pass},
			LogCounts:   logCounter,
		},
	}

	healthz := &health.Healthz{}
	health := handlers.NewHealthcheck("", heartbeatOK, logger)
	component := &common.VcapComponent{
		Config:  cfg,
		Varz:    varz,
		Healthz: healthz,
		Health:  health,
		InfoRoutes: map[string]json.Marshaler{
			"/routes": r,
		},
		Logger: logger,
	}

	routerErrChan := errChan
	if routerErrChan == nil {
		routerErrChan = make(chan error, 2)
	}

	router := &Router{
		config:       cfg,
		proxy:        p,
		mbusClient:   mbusClient,
		registry:     r,
		varz:         v,
		component:    component,
		serveDone:    make(chan struct{}),
		tlsServeDone: make(chan struct{}),
		idleConns:    make(map[net.Conn]struct{}),
		activeConns:  make(map[net.Conn]struct{}),
		logger:       logger,
		errChan:      routerErrChan,
		HeartbeatOK:  heartbeatOK,
		stopping:     false,
	}

	if err := router.component.Start(); err != nil {
		return nil, err
	}

	router.uptimeMonitor = monitor.NewUptime(emitInterval)
	return router, nil
}
	BeforeEach(func() {
		logger = lagertest.NewTestLogger("zipkin")
		req = test_util.NewRequest("GET", "example.com", "/", nil)
		resp = httptest.NewRecorder()
		proxyWriter = utils.NewProxyResponseWriter(resp)
		alr = &schema.AccessLogRecord{
			Request: req,
		}
		proxyWriter.AddToContext("AccessLogRecord", alr)
		nextCalled = false
		heartbeatOK = 1
	})

	Context("with User-Agent checking", func() {
		BeforeEach(func() {
			handler = handlers.NewHealthcheck("HTTP-Monitor/1.1", &heartbeatOK, logger)
		})

		Context("when User-Agent is set to the healthcheck User-Agent", func() {
			BeforeEach(func() {
				req.Header.Set("User-Agent", "HTTP-Monitor/1.1")
			})

			TestHealthcheckOK()

			Context("when draining is in progress", func() {
				BeforeEach(func() {
					heartbeatOK = 0
				})

				TestHealthcheckServiceUnavailable()