cmd/ctrld: add default value and CLI flag for cache size

This commit is contained in:
Cuong Manh Le
2022-12-22 10:20:44 +07:00
committed by Cuong Manh Le
parent b93970ccfd
commit e6d77e2586
2 changed files with 22 additions and 2 deletions

View File

@@ -42,7 +42,7 @@ func initCLI() {
`verbose log output, "-v" means query logging enabled, "-vv" means debug level logging enabled`,
)
basicModeFlags := []string{"listen", "primary_upstream", "secondary_upstream", "domains", "log"}
basicModeFlags := []string{"listen", "primary_upstream", "secondary_upstream", "domains", "log", "cache_size"}
runCmd := &cobra.Command{
Use: "run",
Short: "Run the DNS proxy server",
@@ -73,6 +73,7 @@ func initCLI() {
log.Fatalf("invalid config: %v", err)
}
initLogging()
initCache()
if daemon {
exe, err := os.Executable()
if err != nil {
@@ -121,6 +122,7 @@ func initCLI() {
runCmd.Flags().StringVarP(&secondaryUpstream, "secondary_upstream", "", "", "secondary upstream endpoint")
runCmd.Flags().StringSliceVarP(&domains, "domains", "", nil, "list of domain to apply in a split DNS policy")
runCmd.Flags().StringVarP(&logPath, "log", "", "", "path to log file")
runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items")
rootCmd.AddCommand(runCmd)
@@ -211,7 +213,15 @@ func processNoConfigFlags(noConfigStart bool) {
}
v.Set("upstream", upstream)
sc := ctrld.ServiceConfig{}
if logPath != "" {
v.Set("service", ctrld.ServiceConfig{LogLevel: "debug", LogPath: logPath})
sc.LogLevel = "debug"
sc.LogPath = logPath
}
if cacheSize != 0 {
sc.CacheEnable = true
sc.CacheSize = cacheSize
}
v.Set("service", sc)
}

View File

@@ -20,6 +20,7 @@ var (
secondaryUpstream string
domains []string
logPath string
cacheSize int
cfg ctrld.Config
verbose int
@@ -75,3 +76,12 @@ func initLogging() {
}
zerolog.SetGlobalLevel(level)
}
func initCache() {
if !cfg.Service.CacheEnable {
return
}
if cfg.Service.CacheSize == 0 {
cfg.Service.CacheSize = 4096
}
}