func Run() { PreLoadConfig() confInit() PostLoadConfig() runtime.GOMAXPROCS(runtime.NumCPU()) log.Infof("MineGate %s started.", version_full) go ServerSocket() sig := make(chan os.Signal, 1) signal.Notify(sig, syscall.SIGHUP, syscall.SIGUSR1) for { cur := <-sig switch cur { case syscall.SIGHUP: log.Warn("SIGHUP caught, reloading config...") PreLoadConfig() ConfReload() PostLoadConfig() case syscall.SIGUSR1: log.Warn("SIGUSR1 caught, rotating log...") log.Rotate() default: log.Errorf("Trapped unexpected signal: %s", cur.String()) continue } } }
func ConfReload() { // Do not panic! defer func() { if r := recover(); r != nil { log.Errorf("paniced when reloading config %s, recovered.", config_file) log.Errorf("panic: %s", r) } }() log.Warn("Reloading config") content, err := ioutil.ReadFile(config_file) if err != nil { log.Errorf("unable to reload config %s: %s", config_file, err.Error()) return } prev_listen := config.Listen_addr config_lock.Lock() err = yaml.Unmarshal(content, &config) if err != nil { log.Errorf("error when parsing config file %s: %s", config_file, err.Error()) return } validateConfig() config_lock.Unlock() log.Info("config reloaded.") if config.Listen_addr != prev_listen { log.Warnf("config reload will not reopen server socket, thus no effect on listen address") } log.Infof("%d upstream server(s) found", len(config.Upstream)) }
func validateConfig() { invalid_upstreams := make([]int, 0, len(config.Upstream)) for idx, upstream := range config.Upstream { if !upstream.Validate() { log.Errorf("Upstream %s is not activated.", upstream.Server) invalid_upstreams = append(invalid_upstreams, idx) } } for delta, idx := range invalid_upstreams { idx -= delta config.Upstream[idx] = nil config.Upstream = append(config.Upstream[:idx], config.Upstream[idx+1:]...) } if config.NotFound.Text == "" { log.Warn("Empty error text for not found error, use default string") config.NotFound.Text = "No such host." } config.chatNotFound = ToChatMsg(&config.NotFound) }
func loadConfig() { new_brust, err := minegate.GetExtraConf("conntrack.brust") if err != nil { log.Info("[conntrack] Using default brust: 5") brust = 5 } else { nb := minegate.ToUint(new_brust) log.Infof("[conntrack] Using brust: %d", nb) brust = nb } new_limit, err := minegate.GetExtraConf("conntrack.limit") if err != nil { log.Info("[conntrack] Using default limit: 5") limit = 5 } else { nl := minegate.ToUint(new_limit) if nl != 0 { log.Infof("[conntrack] Using limit: %d", nl) limit = nl } else { log.Info("[conntrack] Limit = 0, disabling conntrack.") limit = 0 } } new_delta, err := minegate.GetExtraConf("conntrack.interval") if err != nil { log.Info("[conntrack] Using default delta: 60") delta = 60 } else { nd := minegate.ToUint(new_delta) if nd < 15 { log.Warn("[conntrack] Minimal interval is 15.") nd = 15 } log.Infof("[conntrack] Using interval %d", nd) delta = nd } }