func (r *RouteFetcher) deleteEndpoints(validRoutes []db.Route) { var diff []db.Route for _, curRoute := range r.endpoints { routeFound := false for _, validRoute := range validRoutes { if routeEquals(curRoute, validRoute) { routeFound = true break } } if !routeFound { diff = append(diff, curRoute) r.endpoints = r.endpoints } } for _, aRoute := range diff { r.RouteRegistry.Unregister( route.Uri(aRoute.Route), route.NewEndpoint( aRoute.LogGuid, aRoute.IP, uint16(aRoute.Port), aRoute.LogGuid, nil, aRoute.TTL, aRoute.RouteServiceUrl, )) } }
func BenchmarkRegister(b *testing.B) { c := config.DefaultConfig() mbus := fakeyagnats.New() r := registry.NewCFRegistry(c, mbus) proxy.NewProxy(proxy.ProxyArgs{ EndpointTimeout: c.EndpointTimeout, Ip: c.Ip, TraceKey: c.TraceKey, Registry: r, Reporter: varz.NewVarz(r), Logger: access_log.CreateRunningAccessLogger(c), }) for i := 0; i < b.N; i++ { str := strconv.Itoa(i) r.Register( route.Uri("bench.vcap.me."+str), &route.Endpoint{ Host: "localhost", Port: uint16(i), }, ) } }
func registerAddr(r *registry.RouteRegistry, u string, a net.Addr, instanceId string) { h, p, err := net.SplitHostPort(a.String()) Ω(err).NotTo(HaveOccurred()) x, err := strconv.Atoi(p) Ω(err).NotTo(HaveOccurred()) r.Register(route.Uri(u), route.NewEndpoint("", h, uint16(x), instanceId, nil)) }
func registerAddr(reg *registry.RouteRegistry, path string, routeServiceUrl string, addr net.Addr, instanceId string) { host, portStr, err := net.SplitHostPort(addr.String()) Expect(err).NotTo(HaveOccurred()) port, err := strconv.Atoi(portStr) Expect(err).NotTo(HaveOccurred()) reg.Register(route.Uri(path), route.NewEndpoint("", host, uint16(port), instanceId, nil, -1, routeServiceUrl)) }
func (r *RouteFetcher) HandleEvent(e routing_api.Event) { eventRoute := e.Route uri := route.Uri(eventRoute.Route) endpoint := route.NewEndpoint(eventRoute.LogGuid, eventRoute.IP, uint16(eventRoute.Port), eventRoute.LogGuid, nil, eventRoute.TTL, eventRoute.RouteServiceUrl) switch e.Action { case "Delete": r.RouteRegistry.Unregister(uri, endpoint) case "Upsert": r.RouteRegistry.Register(uri, endpoint) } }
func (p *proxy) lookup(request *http.Request) *route.Pool { var requestPath string if request.URL.IsAbs() { requestPath = request.URL.Path } else { requestPath = request.RequestURI } uri := route.Uri(hostWithoutPort(request) + requestPath) return p.registry.Lookup(uri) }
func (r *RouteFetcher) HandleEvent(e routing_api.Event) error { r.logger.Infof("Handling event: %v", e) eventRoute := e.Route uri := route.Uri(eventRoute.Route) endpoint := route.NewEndpoint(eventRoute.LogGuid, eventRoute.IP, uint16(eventRoute.Port), eventRoute.LogGuid, nil, eventRoute.TTL) switch e.Action { case "Delete": r.RouteRegistry.Unregister(uri, endpoint) case "Upsert": r.RouteRegistry.Register(uri, endpoint) } r.logger.Infof("Successfully handled event: %v", e) return nil }
func (proxy *Proxy) Lookup(request *http.Request) (*route.Endpoint, bool) { uri := route.Uri(hostWithoutPort(request)) // Try choosing a backend using sticky session if _, err := request.Cookie(StickyCookieKey); err == nil { if sticky, err := request.Cookie(VcapCookieId); err == nil { routeEndpoint, ok := proxy.Registry.LookupByPrivateInstanceId(uri, sticky.Value) if ok { return routeEndpoint, ok } } } // Choose backend using host alone return proxy.Registry.Lookup(uri) }
func (r *Trie) toMap(segment string, m map[route.Uri]*route.Pool) map[route.Uri]*route.Pool { if r.Pool != nil { m[route.Uri(segment)] = r.Pool } for _, child := range r.ChildNodes { var newseg string if len(segment) == 0 { newseg = segment + child.Segment } else { newseg = segment + "/" + child.Segment } child.toMap(newseg, m) } return m }
func BenchmarkRegister(b *testing.B) { c := config.DefaultConfig() mbus := fakeyagnats.New() r := registry.NewRegistry(c, mbus) p := proxy.NewProxy(c, r, varz.NewVarz(r)) for i := 0; i < b.N; i++ { str := strconv.Itoa(i) p.Register( route.Uri("bench.vcap.me."+str), &route.Endpoint{ Host: "localhost", Port: uint16(i), }, ) } }
func (r *RouteFetcher) refreshEndpoints(validRoutes []db.Route) { r.deleteEndpoints(validRoutes) r.endpoints = validRoutes for _, aRoute := range r.endpoints { r.RouteRegistry.Register( route.Uri(aRoute.Route), route.NewEndpoint( aRoute.LogGuid, aRoute.IP, uint16(aRoute.Port), aRoute.LogGuid, nil, aRoute.TTL, )) } }
func (s *ProxySuite) registerAddr(u string, a net.Addr) { h, p, err := net.SplitHostPort(a.String()) if err != nil { panic(err) } x, err := strconv.Atoi(p) if err != nil { panic(err) } s.r.Register( route.Uri(u), &route.Endpoint{ Host: h, Port: uint16(x), }, ) }
func (r *RouteFetcher) refreshEndpoints(validRoutes []models.Route) { r.deleteEndpoints(validRoutes) r.endpoints = validRoutes for _, aRoute := range r.endpoints { r.RouteRegistry.Register( route.Uri(aRoute.Route), route.NewEndpoint( aRoute.LogGuid, aRoute.IP, uint16(aRoute.Port), aRoute.LogGuid, nil, aRoute.GetTTL(), aRoute.RouteServiceUrl, aRoute.ModificationTag, )) } }
func (p *proxy) lookup(request *http.Request) *route.Pool { requestPath := request.URL.EscapedPath() uri := route.Uri(hostWithoutPort(request) + requestPath) return p.registry.Lookup(uri) }
conn.Close() }) }) }) Context("when the endpoint is nil", func() { It("responds with a 502 BadGateway", func() { ln := registerHandler(r, "nil-endpoint", func(conn *test_util.HttpConn) { conn.CheckLine("GET / HTTP/1.1") resp := test_util.NewResponse(http.StatusOK) conn.WriteResponse(resp) conn.Close() }) defer ln.Close() pool := r.Lookup(route.Uri("nil-endpoint")) endpoints := make([]*route.Endpoint, 0) pool.Each(func(e *route.Endpoint) { endpoints = append(endpoints, e) }) for _, e := range endpoints { pool.Remove(e) } conn := dialProxy(proxyServer) req := test_util.NewRequest("GET", "nil-endpoint", "/", nil) conn.WriteRequest(req) b := make([]byte, 0, 0) buf := bytes.NewBuffer(b)
} }) It("updates the route registry", func() { client.RoutesReturns(response, nil) err := fetcher.FetchRoutes() Expect(err).ToNot(HaveOccurred()) Expect(registry.RegisterCallCount()).To(Equal(3)) for i := 0; i < 3; i++ { expectedRoute := response[i] uri, endpoint := registry.RegisterArgsForCall(i) Expect(uri).To(Equal(route.Uri(expectedRoute.Route))) Expect(endpoint).To(Equal( route.NewEndpoint(expectedRoute.LogGuid, expectedRoute.IP, uint16(expectedRoute.Port), expectedRoute.LogGuid, nil, expectedRoute.TTL, expectedRoute.RouteServiceUrl, ))) } }) It("removes unregistered routes", func() { secondResponse := []db.Route{ response[0], }
func (p *proxy) lookup(request *http.Request) *route.Pool { uri := route.Uri(hostWithoutPort(request) + request.RequestURI) return p.registry.Lookup(uri) }
var _ = Describe("AccessLogRecord", func() { Measure("Register", func(b Benchmarker) { c := config.DefaultConfig() mbus := fakeyagnats.NewApceraClientWrapper() r := registry.NewRouteRegistry(c, mbus) accesslog, err := access_log.CreateRunningAccessLogger(c) Ω(err).ToNot(HaveOccurred()) proxy.NewProxy(proxy.ProxyArgs{ EndpointTimeout: c.EndpointTimeout, Ip: c.Ip, TraceKey: c.TraceKey, Registry: r, Reporter: varz.NewVarz(r), AccessLogger: accesslog, }) b.Time("RegisterTime", func() { for i := 0; i < 1000; i++ { str := strconv.Itoa(i) r.Register( route.Uri("bench.vcap.me."+str), route.NewEndpoint("", "localhost", uint16(i), "", nil), ) } }) }, 10) })
} }) It("updates the route registry", func() { client.RoutesReturns(response, nil) err := fetcher.FetchRoutes() Expect(err).ToNot(HaveOccurred()) Expect(registry.RegisterCallCount()).To(Equal(3)) for i := 0; i < 3; i++ { expectedRoute := response[i] uri, endpoint := registry.RegisterArgsForCall(i) Expect(uri).To(Equal(route.Uri(expectedRoute.Route))) Expect(endpoint).To(Equal( route.NewEndpoint(expectedRoute.LogGuid, expectedRoute.IP, uint16(expectedRoute.Port), expectedRoute.LogGuid, nil, expectedRoute.TTL, expectedRoute.RouteServiceUrl, ))) } }) It("uses cache when fetching token from UAA", func() { client.RoutesReturns(response, nil) err := fetcher.FetchRoutes()
} }) It("updates the route registry", func() { client.RoutesReturns(response, nil) err := fetcher.FetchRoutes() Expect(err).ToNot(HaveOccurred()) Expect(registry.RegisterCallCount()).To(Equal(3)) for i := 0; i < 3; i++ { response := response[i] uri, endpoint := registry.RegisterArgsForCall(i) Expect(uri).To(Equal(route.Uri(response.Route))) Expect(endpoint).To(Equal(route.NewEndpoint(response.LogGuid, response.IP, uint16(response.Port), response.LogGuid, nil, response.TTL))) } }) It("removes unregistered routes", func() { secondResponse := []db.Route{ response[0], } client.RoutesReturns(response, nil) err := fetcher.FetchRoutes() Expect(err).ToNot(HaveOccurred()) Expect(registry.RegisterCallCount()).To(Equal(3))
func (p *proxy) lookup(request *http.Request) *route.Pool { uri := route.Uri(hostWithoutPort(request)) // Choose backend using host alone return p.registry.Lookup(uri) }
proxyObj = proxy.NewProxy(proxy.ProxyArgs{ EndpointTimeout: conf.EndpointTimeout, Ip: conf.Ip, TraceKey: conf.TraceKey, Registry: r, Reporter: nullVarz{}, AccessLogger: fakeAccessLogger, SecureCookies: conf.SecureCookies, TLSConfig: tlsConfig, RouteServiceEnabled: conf.RouteServiceEnabled, RouteServiceTimeout: conf.RouteServiceTimeout, Crypto: crypto, CryptoPrev: cryptoPrev, }) r.Register(route.Uri("some-app"), &route.Endpoint{}) }) Context("Log response time", func() { It("logs response time for HTTP connections", func() { body := []byte("some body") req := test_util.NewRequest("GET", "some-app", "/", bytes.NewReader(body)) resp := httptest.NewRecorder() proxyObj.ServeHTTP(resp, req) Expect(fakeAccessLogger.LogCallCount()).To(Equal(1)) Expect(fakeAccessLogger.LogArgsForCall(0).FinishedAt).NotTo(Equal(time.Time{})) }) It("logs response time for TCP connections", func() { req := test_util.NewRequest("UPGRADE", "some-app", "/", nil)
package route_test import ( "github.com/cloudfoundry/gorouter/route" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("URIs", func() { Context("RouteKey", func() { var key route.Uri It("creates a route key based on uri", func() { key = route.Uri("dora.app.com").RouteKey() Expect(key.String()).To(Equal("dora.app.com")) key = route.Uri("dora.app.com/").RouteKey() Expect(key.String()).To(Equal("dora.app.com")) key = route.Uri("dora.app.com/v1").RouteKey() Expect(key.String()).To(Equal("dora.app.com/v1")) }) Context("has a context path", func() { It("creates route key with context path", func() { key = route.Uri("dora.app.com/v1").RouteKey() Expect(key.String()).To(Equal("dora.app.com/v1"))