示例#1
0
func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
	t := time.Now()
	data := lager.Data{"uri": uri, "backend": endpoint.CanonicalAddr(), "modification_tag": endpoint.ModificationTag}

	r.reporter.CaptureRegistryMessage(endpoint)

	r.Lock()

	uri = uri.RouteKey()

	pool, found := r.byUri.Find(uri)
	if !found {
		contextPath := parseContextPath(uri)
		pool = route.NewPool(r.dropletStaleThreshold/4, contextPath)
		r.byUri.Insert(uri, pool)
		r.logger.Debug("uri-added", lager.Data{"uri": uri})
	}

	endpointAdded := pool.Put(endpoint)

	r.timeOfLastUpdate = t
	r.Unlock()

	if endpointAdded {
		r.logger.Debug("endpoint-registered", data)
	} else {
		r.logger.Debug("endpoint-not-registered", data)
	}
}
示例#2
0
func (p *proxyRoundTripper) processBackend(request *http.Request, endpoint *route.Endpoint) {
	p.handler.Logger().Debug("proxy.backend")

	request.URL.Host = endpoint.CanonicalAddr()
	request.Header.Set("X-CF-ApplicationID", endpoint.ApplicationId)
	setRequestXCfInstanceId(request, endpoint)
}
示例#3
0
func (registry *Registry) Register(uri route.Uri, endpoint *route.Endpoint) {
	registry.Lock()
	defer registry.Unlock()

	uri = uri.ToLower()

	key := tableKey{
		addr: endpoint.CanonicalAddr(),
		uri:  uri,
	}

	var endpointToRegister *route.Endpoint

	entry, found := registry.table[key]
	if found {
		endpointToRegister = entry.endpoint
	} else {
		endpointToRegister = endpoint
		entry = &tableEntry{endpoint: endpoint}

		registry.table[key] = entry
	}

	pool, found := registry.byUri[uri]
	if !found {
		pool = route.NewPool()
		registry.byUri[uri] = pool
	}

	pool.Add(endpointToRegister)

	entry.updatedAt = time.Now()

	registry.timeOfLastUpdate = time.Now()
}
示例#4
0
func (h *RequestHandler) serveWebSocket(endpoint *route.Endpoint) error {
	var err error

	client, _, err := h.hijack()
	if err != nil {
		return err
	}

	connection, err := net.Dial("tcp", endpoint.CanonicalAddr())
	if err != nil {
		return err
	}

	defer client.Close()
	defer connection.Close()

	err = h.request.Write(connection)
	if err != nil {
		return err
	}

	forwardIO(client, connection)

	return nil
}
示例#5
0
func SetRequestXCfInstanceId(request *http.Request, endpoint *route.Endpoint) {
	value := endpoint.PrivateInstanceId
	if value == "" {
		value = endpoint.CanonicalAddr()
	}

	request.Header.Set(router_http.CfInstanceIdHeader, value)
}
示例#6
0
func (registry *Registry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
	registry.Lock()
	defer registry.Unlock()

	uri = uri.ToLower()

	key := tableKey{
		addr: endpoint.CanonicalAddr(),
		uri:  uri,
	}

	registry.unregisterUri(key)
}
示例#7
0
func (p *proxyRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
	var err error
	var res *http.Response
	var endpoint *route.Endpoint
	retry := 0
	for {
		endpoint = p.iter.Next()

		if endpoint == nil {
			p.handler.reporter.CaptureBadGateway(request)
			err = noEndpointsAvailable
			p.handler.HandleBadGateway(err)
			return nil, err
		}

		request.URL.Host = endpoint.CanonicalAddr()
		request.Header.Set("X-CF-ApplicationID", endpoint.ApplicationId)
		setRequestXCfInstanceId(request, endpoint)

		res, err = p.transport.RoundTrip(request)
		if err == nil {
			break
		}

		if ne, netErr := err.(*net.OpError); !netErr || ne.Op != "dial" {
			break
		}

		p.iter.EndpointFailed()

		p.handler.Logger().Set("Error", err.Error())
		p.handler.Logger().Warnf("proxy.endpoint.failed")

		retry++
		if retry == retries {
			break
		}
	}

	if p.after != nil {
		p.after(res, endpoint, err)
	}

	p.response = res
	p.err = err

	return res, err
}
示例#8
0
func (s *RegistrySuite) TestLookup(c *C) {
	m := &route.Endpoint{
		Host: "192.168.1.1",
		Port: 1234,
	}

	s.Register("foo", m)

	var b *route.Endpoint
	var ok bool

	b, ok = s.Lookup("foo")
	c.Assert(ok, Equals, true)
	c.Check(b.CanonicalAddr(), Equals, "192.168.1.1:1234")

	b, ok = s.Lookup("FOO")
	c.Assert(ok, Equals, true)
	c.Check(b.CanonicalAddr(), Equals, "192.168.1.1:1234")
}
示例#9
0
func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
	data := lager.Data{"uri": uri, "backend": endpoint.CanonicalAddr(), "modification_tag": endpoint.ModificationTag}
	r.reporter.CaptureRegistryMessage(endpoint)

	r.Lock()

	uri = uri.RouteKey()

	pool, found := r.byUri.Find(uri)
	if found {
		endpointRemoved := pool.Remove(endpoint)
		if endpointRemoved {
			r.logger.Debug("endpoint-unregistered", data)
		} else {
			r.logger.Debug("endpoint-not-unregistered", data)
		}

		if pool.IsEmpty() {
			r.byUri.Delete(uri)
		}
	}

	r.Unlock()
}
示例#10
0
func (h *RequestHandler) setupRequest(endpoint *route.Endpoint) {
	h.setRequestURL(endpoint.CanonicalAddr())
	h.setRequestXForwardedFor()
	SetRequestXRequestStart(h.request)
}
示例#11
0
func (rt *BackendRoundTripper) setupRequest(request *http.Request, endpoint *route.Endpoint) {
	rt.handler.Logger().Debug("backend")
	request.URL.Host = endpoint.CanonicalAddr()
	request.Header.Set("X-CF-ApplicationID", endpoint.ApplicationId)
	handler.SetRequestXCfInstanceId(request, endpoint)
}
示例#12
0
func (h *RequestHandler) setupRequest(endpoint *route.Endpoint) {
	h.setRequestURL(endpoint.CanonicalAddr())
	h.setRequestXForwardedFor()
	setRequestXRequestStart(h.request)
	setRequestXVcapRequestId(h.request, h.StenoLogger)
}
	dropsondeMetrics "github.com/cloudfoundry/dropsonde/metrics"
	"github.com/cloudfoundry/gorouter/metrics"

	"net/http"
	"time"

	"github.com/cloudfoundry/dropsonde/metricbatcher"
	"github.com/cloudfoundry/gorouter/route"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("MetricsReporter", func() {
	var metricsReporter *metrics.MetricsReporter
	var req *http.Request
	var endpoint *route.Endpoint
	var sender *fake.FakeMetricSender

	BeforeEach(func() {
		metricsReporter = metrics.NewMetricsReporter()
		req, _ = http.NewRequest("GET", "https://example.com", nil)
		endpoint = route.NewEndpoint("someId", "host", 2222, "privateId", map[string]string{}, 30, "", models.ModificationTag{})
		sender = fake.NewFakeMetricSender()
		batcher := metricbatcher.New(sender, time.Millisecond)
		dropsondeMetrics.Initialize(sender, batcher)
	})

	It("increments the bad_requests metric", func() {
		metricsReporter.CaptureBadRequest(req)
		Eventually(func() uint64 { return sender.GetCounter("rejected_requests") }).Should(BeEquivalentTo(1))