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 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 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 (rm *RegistryMessage) makeEndpoint() *route.Endpoint { return route.NewEndpoint( rm.App, rm.Host, rm.Port, rm.PrivateInstanceId, rm.Tags, rm.StaleThresholdInSeconds, rm.RouteServiceUrl, models.ModificationTag{}) }
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 (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 (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 (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 CreateAccessLogRecord() *AccessLogRecord { u, err := url.Parse("http://foo.bar:1234/quz?wat") if err != nil { panic(err) } req := &http.Request{ Method: "GET", URL: u, Proto: "HTTP/1.1", Header: make(http.Header), Host: "foo.bar", RemoteAddr: "1.2.3.4:5678", } req.Header.Set("Referer", "referer") req.Header.Set("User-Agent", "user-agent") res := &http.Response{ StatusCode: http.StatusOK, } b := route.NewEndpoint("my_awesome_id", "127.0.0.1", 4567, "", nil) r := AccessLogRecord{ Request: req, StatusCode: res.StatusCode, RouteEndpoint: b, StartedAt: time.Unix(10, 100000000), FirstByteAt: time.Unix(10, 200000000), FinishedAt: time.Unix(10, 300000000), BodyBytesSent: 42, } return &r }
"github.com/cloudfoundry/gorouter/route" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("EndpointIterator", func() { var pool *route.Pool BeforeEach(func() { pool = route.NewPool(2*time.Minute, "") }) Describe("Next", func() { It("performs round-robin through the endpoints", func() { e1 := route.NewEndpoint("", "1.2.3.4", 5678, "", nil, -1, "") e2 := route.NewEndpoint("", "5.6.7.8", 1234, "", nil, -1, "") e3 := route.NewEndpoint("", "1.2.7.8", 1234, "", nil, -1, "") endpoints := []*route.Endpoint{e1, e2, e3} for _, e := range endpoints { pool.Put(e) } counts := make([]int, len(endpoints)) iter := pool.Endpoints("") loops := 50 for i := 0; i < len(endpoints)*loops; i += 1 { n := iter.Next()
var r *RouteRegistry var messageBus *fakeyagnats.FakeNATSConn var fooEndpoint, barEndpoint, bar2Endpoint *route.Endpoint var configObj *config.Config BeforeEach(func() { configObj = config.DefaultConfig() configObj.PruneStaleDropletsInterval = 50 * time.Millisecond configObj.DropletStaleThreshold = 10 * time.Millisecond messageBus = fakeyagnats.Connect() r = NewRouteRegistry(configObj, messageBus) fooEndpoint = route.NewEndpoint("12345", "192.168.1.1", 1234, "id1", map[string]string{ "runtime": "ruby18", "framework": "sinatra", }, -1, "") barEndpoint = route.NewEndpoint("54321", "192.168.1.2", 4321, "id2", map[string]string{ "runtime": "javascript", "framework": "node", }, -1, "https://my-rs.com") bar2Endpoint = route.NewEndpoint("54321", "192.168.1.3", 1234, "id3", map[string]string{ "runtime": "javascript", "framework": "node", }, -1, "") })
go accessLogger.Run() accessLogger.Log(*CreateAccessLogRecord()) Eventually(testEmitter.done).Should(Receive()) Ω(testEmitter.emitted).Should(BeTrue()) Ω(testEmitter.appId).To(Equal("my_awesome_id")) Ω(testEmitter.message).To(MatchRegexp("^.*foo.bar.*\n")) accessLogger.Stop() }) It("a record with no app id is not written", func() { testEmitter := NewMockEmitter() accessLogger := NewFileAndLoggregatorAccessLogger(nil, testEmitter) routeEndpoint := route.NewEndpoint("", "127.0.0.1", 4567, "", nil) accessLogRecord := CreateAccessLogRecord() accessLogRecord.RouteEndpoint = routeEndpoint accessLogger.Log(*accessLogRecord) go accessLogger.Run() Consistently(testEmitter.done).ShouldNot(Receive()) accessLogger.Stop() }) }) Context("with a file", func() { It("writes to the log file", 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() Expect(err).ToNot(HaveOccurred()) Expect(uaaClient.FetchTokenCallCount()).To(Equal(1)) Expect(uaaClient.FetchTokenArgsForCall(0)).To(Equal(false)) })
func (rm *registryMessage) makeEndpoint() *route.Endpoint { return route.NewEndpoint(rm.App, rm.Host, rm.Port, rm.PrivateInstanceId, rm.Tags) }
var r *RouteRegistry var messageBus *fakeyagnats.FakeYagnats var fooEndpoint, barEndpoint, bar2Endpoint *route.Endpoint var configObj *config.Config BeforeEach(func() { configObj = config.DefaultConfig() configObj.PruneStaleDropletsInterval = 50 * time.Millisecond configObj.DropletStaleThreshold = 10 * time.Millisecond messageBus = fakeyagnats.New() r = NewRouteRegistry(configObj, messageBus) fooEndpoint = route.NewEndpoint("12345", "192.168.1.1", 1234, "id1", map[string]string{ "runtime": "ruby18", "framework": "sinatra", }) barEndpoint = route.NewEndpoint("54321", "192.168.1.2", 4321, "id2", map[string]string{ "runtime": "javascript", "framework": "node", }) bar2Endpoint = route.NewEndpoint("54321", "192.168.1.3", 1234, "id3", map[string]string{ "runtime": "javascript", "framework": "node", }) })
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++ { 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)) client.RoutesReturns(secondResponse, nil)
var _ = Describe("AccessLogRecord", func() { Measure("Register", func(b Benchmarker) { logger := lagertest.NewTestLogger("test") c := config.DefaultConfig() r := registry.NewRouteRegistry(logger, c, new(fakes.FakeRouteRegistryReporter)) accesslog, err := access_log.CreateRunningAccessLogger(logger, c) Expect(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, -1, "", models.ModificationTag{}), ) } }) }, 10) })
}) It("reports seconds since last registry update", func() { Registry.Register("foo", &route.Endpoint{}) time.Sleep(10 * time.Millisecond) timeSince := findValue(Varz, "ms_since_last_registry_update").(float64) Expect(timeSince).To(BeNumerically("<", 1000)) Expect(timeSince).To(BeNumerically(">=", 10)) }) It("has urls", func() { Expect(findValue(Varz, "urls")).To(Equal(float64(0))) var fooReg = route.NewEndpoint("12345", "192.168.1.1", 1234, "", map[string]string{}, -1, "", models.ModificationTag{}) // Add a route Registry.Register("foo.vcap.me", fooReg) Registry.Register("fooo.vcap.me", fooReg) Expect(findValue(Varz, "urls")).To(Equal(float64(2))) }) It("updates bad requests", func() { r := http.Request{} Varz.CaptureBadRequest(&r) Expect(findValue(Varz, "bad_requests")).To(Equal(float64(1))) Varz.CaptureBadRequest(&r)
var fakeReporter2 *fakes.FakeProxyReporter var composite reporter.ProxyReporter var req *http.Request var endpoint *route.Endpoint var response *http.Response var responseTime time.Time var responseDuration time.Duration BeforeEach(func() { fakeReporter1 = new(fakes.FakeProxyReporter) fakeReporter2 = new(fakes.FakeProxyReporter) composite = metrics.NewCompositeReporter(fakeReporter1, fakeReporter2) req, _ = http.NewRequest("GET", "https://example.com", nil) endpoint = route.NewEndpoint("someId", "host", 2222, "privateId", map[string]string{}, 30, "", models.ModificationTag{}) response = &http.Response{StatusCode: 200} responseTime = time.Now() responseDuration = time.Second }) It("forwards CaptureBadRequest to both reporters", func() { composite.CaptureBadRequest(req) Expect(fakeReporter1.CaptureBadRequestCallCount()).To(Equal(1)) Expect(fakeReporter2.CaptureBadRequestCallCount()).To(Equal(1)) Expect(fakeReporter1.CaptureBadRequestArgsForCall(0)).To(Equal(req)) Expect(fakeReporter2.CaptureBadRequestArgsForCall(0)).To(Equal(req)) })
func (rm *registryMessage) makeEndpoint() *route.Endpoint { return route.NewEndpoint(rm.App, rm.Host, rm.Port, rm.PrivateInstanceId, rm.Tags, rm.StaleThresholdInSeconds) }
var fakeReporter2 *fakes.FakeReporter var composite metrics.ProxyReporter var req *http.Request var endpoint *route.Endpoint var response *http.Response var responseTime time.Time var responseDuration time.Duration BeforeEach(func() { fakeReporter1 = new(fakes.FakeReporter) fakeReporter2 = new(fakes.FakeReporter) composite = metrics.NewCompositeReporter(fakeReporter1, fakeReporter2) req, _ = http.NewRequest("GET", "https://example.com", nil) endpoint = route.NewEndpoint("someId", "host", 2222, "privateId", map[string]string{}, 30, "") response = &http.Response{StatusCode: 200} responseTime = time.Now() responseDuration = time.Second }) It("forwards CaptureBadRequest to both reporters", func() { composite.CaptureBadRequest(req) Expect(fakeReporter1.CaptureBadRequestCallCount()).To(Equal(1)) Expect(fakeReporter2.CaptureBadRequestCallCount()).To(Equal(1)) Expect(fakeReporter1.CaptureBadRequestArgsForCall(0)).To(Equal(req)) Expect(fakeReporter2.CaptureBadRequestArgsForCall(0)).To(Equal(req)) })
endpoint := &route.Endpoint{} b := pool.Put(endpoint) Expect(b).To(BeTrue()) }) It("handles duplicate endpoints", func() { endpoint := &route.Endpoint{} pool.Put(endpoint) b := pool.Put(endpoint) Expect(b).To(BeFalse()) }) It("handles equivalent (duplicate) endpoints", func() { endpoint1 := route.NewEndpoint("", "1.2.3.4", 5678, "", nil, -1, "", modTag) endpoint2 := route.NewEndpoint("", "1.2.3.4", 5678, "", nil, -1, "", modTag) pool.Put(endpoint1) Expect(pool.Put(endpoint2)).To(BeTrue()) }) Context("with modification tags", func() { var modTag2 models.ModificationTag BeforeEach(func() { modTag2 = models.ModificationTag{Guid: "abc"} endpoint1 := route.NewEndpoint("", "1.2.3.4", 5678, "", nil, -1, "", modTag) Expect(pool.Put(endpoint1)).To(BeTrue()) })
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], } client.RoutesReturns(response, nil) err := fetcher.FetchRoutes() Expect(err).ToNot(HaveOccurred())
r.Insert("/foo/bar/baz", p1) r.Insert("/foo/bar", p2) Expect(r.PoolCount()).To(Equal(2)) }) Describe(".PruneDeadLeaves", func() { It("removes dead leaves", func() { segments := make([]string, 0) count := 0 f := func(r *Trie) { segments = append(segments, r.Segment) count += 1 } e1 := route.NewEndpoint("", "192.168.1.1", 1234, "", nil, -1) e2 := route.NewEndpoint("", "192.168.1.1", 4321, "", nil, -1) p1 := route.NewPool(42) p2 := route.NewPool(42) p3 := route.NewPool(42) p4 := route.NewPool(42) p1.Put(e1) p2.Put(e2) r.Insert("/foo", p1) r.Insert("/foo/bar/baz", p2) r.Insert("/zoo", p3) r.Insert("/foo/bar/zak", p4) r.EachNodeWithPool(f) Expect(segments).To(ConsistOf("foo", "baz", "zak", "zoo")) Expect(count).To(Equal(4))
}) It("reports seconds since last registry update", func() { Registry.Register("foo", &route.Endpoint{}) time.Sleep(10 * time.Millisecond) timeSince := findValue(Varz, "ms_since_last_registry_update").(float64) Expect(timeSince).To(BeNumerically("<", 1000)) Expect(timeSince).To(BeNumerically(">=", 10)) }) It("has urls", func() { Expect(findValue(Varz, "urls")).To(Equal(float64(0))) var fooReg = route.NewEndpoint("12345", "192.168.1.1", 1234, "", map[string]string{}, -1, "") // Add a route Registry.Register("foo.vcap.me", fooReg) Registry.Register("fooo.vcap.me", fooReg) Expect(findValue(Varz, "urls")).To(Equal(float64(2))) }) It("updates bad requests", func() { r := http.Request{} Varz.CaptureBadRequest(&r) Expect(findValue(Varz, "bad_requests")).To(Equal(float64(1))) Varz.CaptureBadRequest(&r)
Expect(fakeLogSender.GetLogs()[0].Message).To(MatchRegexp("^.*foo.bar.*\n")) Expect(fakeLogSender.GetLogs()[0].SourceType).To(Equal("RTR")) Expect(fakeLogSender.GetLogs()[0].SourceInstance).To(Equal("42")) Expect(fakeLogSender.GetLogs()[0].MessageType).To(Equal("OUT")) accessLogger.Stop() }) It("a record with no app id is not logged to dropsonde", func() { fakeLogSender := fake.NewFakeLogSender() logs.Initialize(fakeLogSender) accessLogger := NewFileAndLoggregatorAccessLogger(logger, "43") routeEndpoint := route.NewEndpoint("", "127.0.0.1", 4567, "", nil, -1, "", models.ModificationTag{}) accessLogRecord := CreateAccessLogRecord() accessLogRecord.RouteEndpoint = routeEndpoint accessLogger.Log(*accessLogRecord) go accessLogger.Run() Consistently(fakeLogSender.GetLogs).Should(HaveLen(0)) accessLogger.Stop() }) }) Context("created with access log file", func() { It("writes to the log file and Stdout", func() {