func setupForwarders(ctx context.Context, loadedConfig *config.ProxyConfig) ([]protocol.Forwarder, error) { allForwarders := make([]protocol.Forwarder, 0, len(loadedConfig.ForwardTo)) allKeepers := []stats.Keeper{stats.NewGolangKeeper()} for _, forwardConfig := range loadedConfig.ForwardTo { loader, ok := allForwarderLoaders[forwardConfig.Type] if !ok { log.WithField("type", forwardConfig.Type).Error("Unknown loader type") return nil, fmt.Errorf("unknown loader type %s", forwardConfig.Type) } forwarder, err := loader(ctx, forwardConfig) if err != nil { log.WithField("err", err).Error("unable to load config") return nil, err } allForwarders = append(allForwarders, forwarder) allKeepers = append(allKeepers, forwarder) } return allForwarders, nil }
func (proxyCommandLineConfiguration *proxyCommandLineConfigurationT) main() error { log.WithField("configFile", proxyCommandLineConfiguration.configFileName).Info("Looking for config file") log.WithField("env", strings.Join(os.Environ(), " -*- ")).Info("Current env at start") loadedConfig, err := config.Load(proxyCommandLineConfiguration.configFileName) if err != nil { log.WithField("err", err).Error("Unable to load config") return err } proxyCommandLineConfiguration.setupLogrus(loadedConfig) { pidFilename := proxyCommandLineConfiguration.pidFileName if loadedConfig.PidFilename != nil { pidFilename = *loadedConfig.PidFilename } writePidFile(pidFilename) } log.WithField("config", loadedConfig).Info("Config loaded") log.WithField("env", strings.Join(os.Environ(), " -*- ")).Info("Current env after loading config") if loadedConfig.NumProcs != nil { runtime.GOMAXPROCS(*loadedConfig.NumProcs) } else { numProcs := runtime.NumCPU() runtime.GOMAXPROCS(numProcs) } proxyCommandLineConfiguration.allForwarders, err = setupForwarders(proxyCommandLineConfiguration.ctx, loadedConfig) if err != nil { return err } allKeepers := []stats.Keeper{stats.NewGolangKeeper()} multiplexerCounter := &dpsink.Counter{} multiplexer := dpsink.FromChain(demultiplexer.New(recastToSinks(proxyCommandLineConfiguration.allForwarders)), dpsink.NextWrap(multiplexerCounter)) allKeepers = append(allKeepers, stats.ToKeeper(multiplexerCounter, map[string]string{"location": "middle", "name": "proxy", "type": "demultiplexer"})) proxyCommandLineConfiguration.allListeners, err = setupListeners(proxyCommandLineConfiguration.ctx, loadedConfig, multiplexer) if err != nil { return err } // I wish I could do this, but golang doesn't allow append() of super/sub types // allKeepers = append(allKeepers, proxyCommandLineConfiguration.allListeners...) allKeepers = append(allKeepers, recastListenToKeeper(proxyCommandLineConfiguration.allListeners)...) for _, forwarder := range proxyCommandLineConfiguration.allForwarders { allKeepers = append(allKeepers, forwarder) } if loadedConfig.StatsDelayDuration != nil && *loadedConfig.StatsDelayDuration != 0 { proxyCommandLineConfiguration.statDrainThread = stats.NewDrainingThread(*loadedConfig.StatsDelayDuration, recastToSinks(proxyCommandLineConfiguration.allForwarders), allKeepers, proxyCommandLineConfiguration.ctx) } else { log.Info("Skipping stat keeping") } setupDebugServer(allKeepers, loadedConfig, proxyCommandLineConfiguration.pprofaddr) log.Infof("Setup done. Blocking!") if proxyCommandLineConfiguration.closeWhenWaitingToStopChannel != nil { close(proxyCommandLineConfiguration.closeWhenWaitingToStopChannel) } _ = <-proxyCommandLineConfiguration.stopChannel return nil }