コード例 #1
0
ファイル: main.go プロジェクト: jungle0755/gorouter
func connectToNatsServer(c *config.Config, logger *steno.Logger) yagnats.NATSConn {
	var natsClient yagnats.NATSConn
	var err error

	natsServers := c.NatsServers()
	attempts := 3
	for attempts > 0 {
		natsClient, err = yagnats.Connect(natsServers)
		if err == nil {
			break
		} else {
			attempts--
			time.Sleep(100 * time.Millisecond)
		}
	}

	if err != nil {
		logger.Errorf("Error connecting to NATS: %s\n", err)
		os.Exit(1)
	}

	natsClient.AddClosedCB(func(conn *nats.Conn) {
		logger.Errorf("Close on NATS client. nats.Conn: %+v", *conn)
		os.Exit(1)
	})

	return natsClient
}
コード例 #2
0
ファイル: main.go プロジェクト: rakutentech/gorouter
func connectToNatsServer(logger lager.Logger, c *config.Config) *nats.Conn {
	var natsClient *nats.Conn
	var err error

	natsServers := c.NatsServers()
	attempts := 3
	for attempts > 0 {
		options := nats.DefaultOptions
		options.Servers = natsServers
		options.PingInterval = c.NatsClientPingInterval
		options.ClosedCB = func(conn *nats.Conn) {
			logger.Fatal("nats-connection-closed", errors.New("unexpected close"), lager.Data{"last_error": conn.LastError()})
		}
		natsClient, err = options.Connect()
		if err == nil {
			break
		} else {
			attempts--
			time.Sleep(100 * time.Millisecond)
		}
	}

	if err != nil {
		logger.Fatal("nats-connection-error", err)
	}

	return natsClient
}
コード例 #3
0
ファイル: main.go プロジェクト: sunatthegilddotcom/gorouter
func connectToNatsServer(logger lager.Logger, c *config.Config) yagnats.NATSConn {
	var natsClient yagnats.NATSConn
	var err error

	natsServers := c.NatsServers()
	attempts := 3
	for attempts > 0 {
		natsClient, err = yagnats.Connect(natsServers)
		if err == nil {
			break
		} else {
			attempts--
			time.Sleep(100 * time.Millisecond)
		}
	}

	if err != nil {
		logger.Fatal("nats-connection-error", err)
	}

	natsClient.AddClosedCB(func(conn *nats.Conn) {
		logger.Fatal("nats-connection-closed", errors.New("unexpected close"), lager.Data{"connection": *conn})
	})

	return natsClient
}
コード例 #4
0
ファイル: main.go プロジェクト: jungle0755/gorouter
func setupRouteFetcher(c *config.Config, registry rregistry.RegistryInterface) {
	if c.RoutingApiEnabled() {
		tokenFetcher := token_fetcher.NewTokenFetcher(&c.OAuth)
		routingApiUri := fmt.Sprintf("%s:%d", c.RoutingApi.Uri, c.RoutingApi.Port)
		routingApiClient := routing_api.NewClient(routingApiUri)
		routeFetcher := route_fetcher.NewRouteFetcher(steno.NewLogger("router.route_fetcher"), tokenFetcher, registry, c, routingApiClient, 1)
		routeFetcher.StartFetchCycle()
		routeFetcher.StartEventCycle()
	}
}
コード例 #5
0
ファイル: registry.go プロジェクト: shashankmjain/gorouter
func NewRouteRegistry(c *config.Config, mbus yagnats.NATSConn, reporter metrics.RouteReporter) *RouteRegistry {
	r := &RouteRegistry{}
	r.logger = c.Logger()
	r.byUri = NewTrie()

	r.pruneStaleDropletsInterval = c.PruneStaleDropletsInterval
	r.dropletStaleThreshold = c.DropletStaleThreshold

	r.messageBus = mbus
	r.reporter = reporter
	return r
}
コード例 #6
0
ファイル: registry_test.go プロジェクト: nakaji-s/gorouter
func (s *RegistrySuite) SetUpTest(c *C) {
	var configObj *config.Config

	configObj = config.DefaultConfig()
	configObj.DropletStaleThreshold = 10 * time.Millisecond

	s.messageBus = fakeyagnats.New()
	s.Registry = NewRegistry(configObj, s.messageBus)

	fooEndpoint = &route.Endpoint{
		Host: "192.168.1.1",
		Port: 1234,

		ApplicationId: "12345",
		Tags: map[string]string{
			"runtime":   "ruby18",
			"framework": "sinatra",
		},
	}

	barEndpoint = &route.Endpoint{
		Host: "192.168.1.2",
		Port: 4321,

		ApplicationId: "54321",
		Tags: map[string]string{
			"runtime":   "javascript",
			"framework": "node",
		},
	}

	bar2Endpoint = &route.Endpoint{
		Host: "192.168.1.3",
		Port: 1234,

		ApplicationId: "54321",
		Tags: map[string]string{
			"runtime":   "javascript",
			"framework": "node",
		},
	}
}
コード例 #7
0
	. "github.com/cloudfoundry/gorouter/router"
	"github.com/cloudfoundry/gorouter/test"
	"github.com/cloudfoundry/gorouter/test_util"
	vvarz "github.com/cloudfoundry/gorouter/varz"
	"github.com/cloudfoundry/gunk/natsrunner"
	"github.com/cloudfoundry/yagnats"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"io/ioutil"
	"net/http"
	"time"
)

var _ = Describe("Router", func() {
	var natsRunner *natsrunner.NATSRunner
	var config *cfg.Config

	var mbusClient *yagnats.Client
	var registry *rregistry.RouteRegistry
	var varz vvarz.Varz
	var router *Router
	var natsPort uint16

	BeforeEach(func() {
		natsPort = test_util.NextAvailPort()
		natsRunner = natsrunner.NewNATSRunner(int(natsPort))
		natsRunner.Start()

		proxyPort := test_util.NextAvailPort()
		statusPort := test_util.NextAvailPort()
コード例 #8
0
func init() {
	sender = metrics_fakes.NewFakeMetricSender()
	metrics.Initialize(sender, nil)
}

var _ = Describe("RouteFetcher", func() {
	var (
		cfg       *config.Config
		uaaClient *testUaaClient.FakeClient
		registry  *testRegistry.FakeRegistryInterface
		fetcher   *RouteFetcher
		logger    lager.Logger
		client    *fake_routing_api.FakeClient

		token *schema.Token

		response     []db.Route
		process      ifrit.Process
		eventChannel chan routing_api.Event
		errorChannel chan error

		clock *fakeclock.FakeClock
	)

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("test")
		cfg = config.DefaultConfig()
		cfg.PruneStaleDropletsInterval = 2 * time.Second

		retryInterval := 0
コード例 #9
0
	. "github.com/onsi/gomega"

	"github.com/cloudfoundry/gorouter/config"
	"github.com/cloudfoundry/gorouter/route"
	"github.com/cloudfoundry/yagnats/fakeyagnats"

	"encoding/json"
	"time"
)

var _ = Describe("RouteRegistry", func() {
	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,
コード例 #10
0
ファイル: router_test.go プロジェクト: rakutentech/gorouter
	testcommon "github.com/cloudfoundry/gorouter/test/common"
	"github.com/pivotal-golang/lager"
	"github.com/pivotal-golang/lager/lagertest"
)

var _ = Describe("Router", func() {

	const uuid_regex = `^[[:xdigit:]]{8}(-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}$`

	var (
		natsRunner *test_util.NATSRunner
		natsPort   uint16
		config     *cfg.Config

		mbusClient   *nats.Conn
		registry     *rregistry.RouteRegistry
		varz         vvarz.Varz
		router       *Router
		signals      chan os.Signal
		closeChannel chan struct{}
		readyChan    chan struct{}
		logger       lager.Logger
	)

	BeforeEach(func() {
		natsPort = test_util.NextAvailPort()
		natsRunner = test_util.NewNATSRunner(int(natsPort))
		natsRunner.Start()

		fakeEmitter := fake.NewFakeEventEmitter("fake")
		dropsonde.InitializeWithEmitter(fakeEmitter)
コード例 #11
0
	"github.com/pivotal-golang/lager/lagertest"

	"testing"
	"time"

	"github.com/cloudfoundry/gorouter/metrics/fakes"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var (
	r             *registry.RouteRegistry
	p             proxy.Proxy
	conf          *config.Config
	proxyServer   net.Listener
	accessLog     access_log.AccessLogger
	accessLogFile *test_util.FakeFile
	crypto        secure.Crypto
	logger        lager.Logger
	cryptoPrev    secure.Crypto
)

func TestProxy(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Proxy Suite")
}

var _ = BeforeEach(func() {
	logger = lagertest.NewTestLogger("test")
	var err error
コード例 #12
0
type connHandler func(*test_util.HttpConn)

type nullVarz struct{}

func (_ nullVarz) MarshalJSON() ([]byte, error)                               { return json.Marshal(nil) }
func (_ nullVarz) ActiveApps() *stats.ActiveApps                              { return stats.NewActiveApps() }
func (_ nullVarz) CaptureBadRequest(*http.Request)                            {}
func (_ nullVarz) CaptureBadGateway(*http.Request)                            {}
func (_ nullVarz) CaptureRoutingRequest(b *route.Endpoint, req *http.Request) {}
func (_ nullVarz) CaptureRoutingResponse(b *route.Endpoint, res *http.Response, t time.Time, d time.Duration) {
}

var _ = Describe("Proxy", func() {
	var r *registry.RouteRegistry
	var p Proxy
	var conf *config.Config
	var proxyServer net.Listener
	var accessLog access_log.AccessLogger
	var accessLogFile *test_util.FakeFile

	BeforeEach(func() {
		conf = config.DefaultConfig()
		conf.TraceKey = "my_trace_key"
		conf.EndpointTimeout = 500 * time.Millisecond

		mbus := fakeyagnats.New()

		r = registry.NewRouteRegistry(conf, mbus)

		accessLogFile = new(test_util.FakeFile)
		accessLog = access_log.NewFileAndLoggregatorAccessLogger(accessLogFile, nil)
コード例 #13
0
ファイル: router_drain_test.go プロジェクト: idouba/gorouter
	. "github.com/cloudfoundry/gorouter/router"
	"github.com/cloudfoundry/gorouter/test"
	"github.com/cloudfoundry/gorouter/test_util"
	vvarz "github.com/cloudfoundry/gorouter/varz"
	"github.com/cloudfoundry/gunk/natsrunner"
	"github.com/cloudfoundry/yagnats"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Router", func() {
	var (
		natsRunner *natsrunner.NATSRunner
		config     *cfg.Config

		mbusClient yagnats.NATSConn
		registry   *rregistry.RouteRegistry
		varz       vvarz.Varz
		router     *Router
		natsPort   uint16
	)

	testAndVerifyRouterStopsNoDrain := func(signals chan os.Signal, closeChannel chan struct{}, sigs ...os.Signal) {
		app := test.NewTestApp([]route.Uri{"drain.vcap.me"}, config.Port, mbusClient, nil, "")
		blocker := make(chan bool)
		resultCh := make(chan bool, 2)
		app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
			blocker <- true

			_, err := ioutil.ReadAll(r.Body)
			defer r.Body.Close()
			Expect(err).ToNot(HaveOccurred())
コード例 #14
0
ファイル: route_fetcher_test.go プロジェクト: krumts/gorouter
	"github.com/cloudfoundry/gosteno"

	. "github.com/cloudfoundry/gorouter/route_fetcher"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("RouteFetcher", func() {
	var (
		cfg           *config.Config
		tokenFetcher  *testTokenFetcher.FakeTokenFetcher
		registry      *testRegistry.FakeRegistryInterface
		fetcher       *RouteFetcher
		logger        *gosteno.Logger
		sink          *gosteno.TestingSink
		client        *fake_routing_api.FakeClient
		retryInterval int

		token *token_fetcher.Token

		response []db.Route
	)

	BeforeEach(func() {
		cfg = config.DefaultConfig()

		retryInterval := 0
		tokenFetcher = &testTokenFetcher.FakeTokenFetcher{}
		registry = &testRegistry.FakeRegistryInterface{}
		sink = gosteno.NewTestingSink()
コード例 #15
0
ファイル: router_test.go プロジェクト: trainchou/gorouter
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httputil"
	"strings"
	"time"
)

var _ = Describe("Router", func() {

	var natsRunner *natsrunner.NATSRunner
	var natsPort uint16
	var config *cfg.Config

	var mbusClient yagnats.NATSConn
	var registry *rregistry.RouteRegistry
	var varz vvarz.Varz
	var router *Router

	BeforeEach(func() {
		natsPort = test_util.NextAvailPort()
		natsRunner = natsrunner.NewNATSRunner(int(natsPort))
		natsRunner.Start()

		fakeEmitter := fake.NewFakeEventEmitter("fake")
		dropsonde.InitializeWithEmitter(fakeEmitter)

		proxyPort := test_util.NextAvailPort()