func newTemplateRouter(cfg templateRouterCfg) (*templateRouter, error) { dir := cfg.dir glog.V(2).Infof("Creating a new template router, writing to %s", dir) if len(cfg.peerEndpointsKey) > 0 { glog.V(2).Infof("Router will use %s service to identify peers", cfg.peerEndpointsKey) } certManagerConfig := &certificateManagerConfig{ certKeyFunc: generateCertKey, caCertKeyFunc: generateCACertKey, destCertKeyFunc: generateDestCertKey, certDir: filepath.Join(dir, certDir), caCertDir: filepath.Join(dir, caCertDir), } certManager, err := newSimpleCertificateManager(certManagerConfig, newSimpleCertificateWriter()) if err != nil { return nil, err } router := &templateRouter{ dir: dir, templates: cfg.templates, reloadScriptPath: cfg.reloadScriptPath, reloadInterval: cfg.reloadInterval, state: make(map[string]ServiceUnit), certManager: certManager, defaultCertificate: cfg.defaultCertificate, defaultCertificatePath: cfg.defaultCertificatePath, statsUser: cfg.statsUser, statsPassword: cfg.statsPassword, statsPort: cfg.statsPort, peerEndpointsKey: cfg.peerEndpointsKey, peerEndpoints: []Endpoint{}, rateLimitedCommitFunction: nil, rateLimitedCommitStopChannel: make(chan struct{}), } keyFunc := func(_ interface{}) (string, error) { return "templaterouter", nil } numSeconds := int(cfg.reloadInterval.Seconds()) router.rateLimitedCommitFunction = ratelimiter.NewRateLimitedFunction(keyFunc, numSeconds, router.commitAndReload) router.rateLimitedCommitFunction.RunUntil(router.rateLimitedCommitStopChannel) glog.V(2).Infof("Template router will coalesce reloads within %v seconds of each other", numSeconds) if err := router.writeDefaultCert(); err != nil { return nil, err } glog.V(4).Infof("Reading persisted state") if err := router.readState(); err != nil { return nil, err } glog.V(4).Infof("Committing state") router.Commit() return router, nil }
func (r *templateRouter) EnableRateLimiter(interval int, handlerFunc ratelimiter.HandlerFunc) { keyFunc := func(_ interface{}) (string, error) { return "templaterouter", nil } r.rateLimitedCommitFunction = ratelimiter.NewRateLimitedFunction(keyFunc, interval, handlerFunc) r.rateLimitedCommitFunction.RunUntil(r.rateLimitedCommitStopChannel) glog.V(2).Infof("Template router will coalesce reloads within %v seconds of each other", interval) }