func main() { c := config.DefaultConfig() logCounter := vcap.NewLogCounter() if configFile != "" { c = config.InitConfigFromFile(configFile) } InitLoggerFromConfig(c, logCounter) logger := steno.NewLogger("router.main") err := dropsonde.Initialize(c.Logging.MetronAddress, c.Logging.JobName) if err != nil { logger.Errorf("Dropsonde failed to initialize: %s", err.Error()) os.Exit(1) } // setup number of procs if c.GoMaxProcs != 0 { runtime.GOMAXPROCS(c.GoMaxProcs) } if c.DebugAddr != "" { cf_debug_server.Run(c.DebugAddr) } logger.Info("Setting up NATs connection") natsClient := connectToNatsServer(c, logger) registry := rregistry.NewRouteRegistry(c, natsClient) logger.Info("Setting up routing_api route fetcher") setupRouteFetcher(c, registry) varz := rvarz.NewVarz(registry) accessLogger, err := access_log.CreateRunningAccessLogger(c) if err != nil { logger.Fatalf("Error creating access logger: %s\n", err) } proxy := buildProxy(c, registry, accessLogger, varz) router, err := router.NewRouter(c, proxy, natsClient, registry, varz, logCounter) if err != nil { logger.Errorf("An error occurred: %s", err.Error()) os.Exit(1) } errChan := router.Run() logger.Info("gorouter.started") waitOnErrOrSignal(c, logger, errChan, router) os.Exit(0) }
BeforeEach(func() { natsPort = test_util.NextAvailPort() natsRunner = natsrunner.NewNATSRunner(int(natsPort)) natsRunner.Start() proxyPort := test_util.NextAvailPort() statusPort := test_util.NextAvailPort() config = test_util.SpecConfig(natsPort, statusPort, proxyPort) config.EndpointTimeout = 5 * time.Second mbusClient = natsRunner.MessageBus.(*yagnats.Client) registry = rregistry.NewRouteRegistry(config, mbusClient) varz = vvarz.NewVarz(registry) logcounter := vcap.NewLogCounter() proxy := proxy.NewProxy(proxy.ProxyArgs{ EndpointTimeout: config.EndpointTimeout, Ip: config.Ip, TraceKey: config.TraceKey, Registry: registry, Reporter: varz, AccessLogger: &access_log.NullAccessLogger{}, }) r, err := NewRouter(config, proxy, mbusClient, registry, varz, logcounter) Ω(err).ShouldNot(HaveOccurred()) router = r r.Run() }) AfterEach(func() {
func main() { c := config.DefaultConfig() logCounter := vcap.NewLogCounter() if configFile != "" { c = config.InitConfigFromFile(configFile) } InitLoggerFromConfig(c, logCounter) logger := steno.NewLogger("router.main") err := dropsonde.Initialize(c.Logging.MetronAddress, c.Logging.JobName) if err != nil { logger.Errorf("Dropsonde failed to initialize: %s", err.Error()) os.Exit(1) } // setup number of procs if c.GoMaxProcs != 0 { runtime.GOMAXPROCS(c.GoMaxProcs) } if c.DebugAddr != "" { cf_debug_server.Run(c.DebugAddr) } natsServers := c.NatsServers() var natsClient yagnats.NATSConn 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) }) registry := rregistry.NewRouteRegistry(c, natsClient) if c.RoutingApiEnabled() { logger.Info("Setting up routing_api route fetcher") 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() } varz := rvarz.NewVarz(registry) accessLogger, err := access_log.CreateRunningAccessLogger(c) if err != nil { logger.Fatalf("Error creating access logger: %s\n", err) } args := proxy.ProxyArgs{ EndpointTimeout: c.EndpointTimeout, Ip: c.Ip, TraceKey: c.TraceKey, Registry: registry, Reporter: varz, AccessLogger: accessLogger, SecureCookies: c.SecureCookies, } p := proxy.NewProxy(args) router, err := router.NewRouter(c, p, natsClient, registry, varz, logCounter) if err != nil { logger.Errorf("An error occurred: %s", err.Error()) os.Exit(1) } signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1) errChan := router.Run() logger.Info("gorouter.started") select { case err := <-errChan: if err != nil { logger.Errorf("Error occurred: %s", err.Error()) os.Exit(1) } case sig := <-signals: go func() { for sig := range signals { logger.Infod( map[string]interface{}{ "signal": sig.String(), }, "gorouter.signal.ignored", ) } }() if sig == syscall.SIGUSR1 { logger.Infod( map[string]interface{}{ "timeout": (c.DrainTimeout).String(), }, "gorouter.draining", ) router.Drain(c.DrainTimeout) } stoppingAt := time.Now() logger.Info("gorouter.stopping") router.Stop() logger.Infod( map[string]interface{}{ "took": time.Since(stoppingAt).String(), }, "gorouter.stopped", ) } os.Exit(0) }
package log import ( "github.com/cloudfoundry/gorouter/common" "github.com/cloudfoundry/gorouter/config" steno "github.com/cloudfoundry/gosteno" "os" ) var logger *steno.Logger var Counter = common.NewLogCounter() func init() { stenoConfig := &steno.Config{ Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)}, Codec: steno.NewJsonCodec(), Level: steno.LOG_ALL, } steno.Init(stenoConfig) logger = steno.NewLogger("router.init") } func SetupLoggerFromConfig(c *config.Config) { l, err := steno.GetLogLevel(c.Logging.Level) if err != nil { panic(err) } s := make([]steno.Sink, 0)
func main() { flag.StringVar(&configFile, "c", "", "Configuration File") cf_lager.AddFlags(flag.CommandLine) flag.Parse() c := config.DefaultConfig() logCounter := vcap.NewLogCounter() if configFile != "" { c = config.InitConfigFromFile(configFile) } InitLoggerFromConfig(c, logCounter) logger := steno.NewLogger("router.main") err := dropsonde.Initialize(c.Logging.MetronAddress, c.Logging.JobName) if err != nil { logger.Errorf("Dropsonde failed to initialize: %s", err.Error()) os.Exit(1) } // setup number of procs if c.GoMaxProcs != 0 { runtime.GOMAXPROCS(c.GoMaxProcs) } if c.DebugAddr != "" { cf_debug_server.Run(c.DebugAddr) } logger.Info("Setting up NATs connection") natsClient := connectToNatsServer(logger, c) metricsReporter := metrics.NewMetricsReporter() registry := rregistry.NewRouteRegistry(c, natsClient, metricsReporter) varz := rvarz.NewVarz(registry) compositeReporter := metrics.NewCompositeReporter(varz, metricsReporter) accessLogger, err := access_log.CreateRunningAccessLogger(c) if err != nil { logger.Fatalf("Error creating access logger: %s\n", err) } var crypto secure.Crypto var cryptoPrev secure.Crypto if c.RouteServiceEnabled { crypto = createCrypto(logger, c.RouteServiceSecret) if c.RouteServiceSecretPrev != "" { cryptoPrev = createCrypto(logger, c.RouteServiceSecretPrev) } } proxy := buildProxy(c, registry, accessLogger, compositeReporter, crypto, cryptoPrev) router, err := router.NewRouter(c, proxy, natsClient, registry, varz, logCounter, nil) if err != nil { logger.Errorf("An error occurred: %s", err.Error()) os.Exit(1) } members := grouper.Members{ {"router", router}, } if c.RoutingApiEnabled() { logger.Info("Setting up route fetcher") routeFetcher := setupRouteFetcher(logger, c, registry) members = append(members, grouper.Member{"router-fetcher", routeFetcher}) } group := grouper.NewOrdered(os.Interrupt, members) monitor := ifrit.Invoke(sigmon.New(group, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)) err = <-monitor.Wait() if err != nil { logger.Error("gorouter.exited-with-failure") os.Exit(1) } os.Exit(0) }
func main() { c := config.DefaultConfig() logCounter := vcap.NewLogCounter() InitLoggerFromConfig(c, logCounter) if configFile != "" { c = config.InitConfigFromFile(configFile) } // setup number of procs if c.GoMaxProcs != 0 { runtime.GOMAXPROCS(c.GoMaxProcs) } InitLoggerFromConfig(c, logCounter) logger := steno.NewLogger("router.main") natsMembers := make([]string, len(c.Nats)) for _, info := range c.Nats { uri := url.URL{ Scheme: "nats", User: url.UserPassword(info.User, info.Pass), Host: fmt.Sprintf("%s:%d", info.Host, info.Port), } natsMembers = append(natsMembers, uri.String()) } natsClient := yagnats.NewApceraClientWrapper(natsMembers) err := natsClient.Connect() if err != nil { logger.Fatalf("Error connecting to NATS: %s\n", err) } registry := rregistry.NewRouteRegistry(c, natsClient) varz := rvarz.NewVarz(registry) accessLogger, err := access_log.CreateRunningAccessLogger(c) if err != nil { logger.Fatalf("Error creating access logger: %s\n", err) } args := proxy.ProxyArgs{ EndpointTimeout: c.EndpointTimeout, Ip: c.Ip, TraceKey: c.TraceKey, Registry: registry, Reporter: varz, AccessLogger: accessLogger, } p := proxy.NewProxy(args) router, err := router.NewRouter(c, p, natsClient, registry, varz, logCounter) if err != nil { logger.Errorf("An error occurred: %s", err.Error()) os.Exit(1) } signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1) errChan := router.Run() logger.Info("gorouter.started") select { case err := <-errChan: if err != nil { logger.Errorf("Error occurred: %s", err.Error()) os.Exit(1) } case sig := <-signals: go func() { for sig := range signals { logger.Infod( map[string]interface{}{ "signal": sig.String(), }, "gorouter.signal.ignored", ) } }() if sig == syscall.SIGUSR1 { logger.Infod( map[string]interface{}{ "timeout": (c.DrainTimeout).String(), }, "gorouter.draining", ) router.Drain(c.DrainTimeout) } stoppingAt := time.Now() logger.Info("gorouter.stopping") router.Stop() logger.Infod( map[string]interface{}{ "took": time.Since(stoppingAt).String(), }, "gorouter.stopped", ) } os.Exit(0) }
func main() { flag.StringVar(&configFile, "c", "", "Configuration File") cf_lager.AddFlags(flag.CommandLine) flag.Parse() c := config.DefaultConfig() logCounter := vcap.NewLogCounter() if configFile != "" { c = config.InitConfigFromFile(configFile) } prefix := "gorouter.stdout" if c.Logging.Syslog != "" { prefix = c.Logging.Syslog } logger, _ := cf_lager.New(prefix) InitLoggerFromConfig(logger, c, logCounter) err := dropsonde.Initialize(c.Logging.MetronAddress, c.Logging.JobName) if err != nil { logger.Fatal("dropsonde-initialize-error", err) } // setup number of procs if c.GoMaxProcs != 0 { runtime.GOMAXPROCS(c.GoMaxProcs) } if c.DebugAddr != "" { cf_debug_server.Run(c.DebugAddr) } logger.Info("setting-up-nats-connection") natsClient := connectToNatsServer(logger.Session("nats"), c) metricsReporter := metrics.NewMetricsReporter() registry := rregistry.NewRouteRegistry(logger.Session("registry"), c, natsClient, metricsReporter) varz := rvarz.NewVarz(registry) compositeReporter := metrics.NewCompositeReporter(varz, metricsReporter) accessLogger, err := access_log.CreateRunningAccessLogger(logger.Session("access-log"), c) if err != nil { logger.Fatal("error-creating-access-logger", err) } var crypto secure.Crypto var cryptoPrev secure.Crypto if c.RouteServiceEnabled { crypto = createCrypto(logger, c.RouteServiceSecret) if c.RouteServiceSecretPrev != "" { cryptoPrev = createCrypto(logger, c.RouteServiceSecretPrev) } } proxy := buildProxy(logger.Session("proxy"), c, registry, accessLogger, compositeReporter, crypto, cryptoPrev) router, err := router.NewRouter(logger.Session("router"), c, proxy, natsClient, registry, varz, logCounter, nil) if err != nil { logger.Fatal("initialize-router-error", err) } members := grouper.Members{ {"router", router}, } if c.RoutingApiEnabled() { logger.Info("setting-up-routing-api") routeFetcher := setupRouteFetcher(logger.Session("route-fetcher"), c, registry) // check connectivity to routing api err := routeFetcher.FetchRoutes() if err != nil { logger.Fatal("routing-api-connection-failed", err) } members = append(members, grouper.Member{"router-fetcher", routeFetcher}) } group := grouper.NewOrdered(os.Interrupt, members) monitor := ifrit.Invoke(sigmon.New(group, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)) err = <-monitor.Wait() if err != nil { logger.Error("gorouter.exited-with-failure", err) os.Exit(1) } os.Exit(0) }