Exemple #1
0
func initialize() error {
	fmt.Println("tf initializing ....")

	//读取配置文件,缺省是./etc/tf.conf
	//flag是commandLine解析package
	confFile := flag.String("c", "./etc/tf.conf", "config file name")
	if confFile == nil {
		return fmt.Errorf("no config file")
	}
	//初始化相关参数
	flag.Parse()

	conf, err := config.ReadDefault(*confFile)
	common.CheckError(err, "load config file failed: ")
	common.Conf = conf

	max_processor := common.GetConfInt("global", "max_processor", runtime.NumCPU())
	fmt.Println("max_processor: ", max_processor)
	runtime.GOMAXPROCS(max_processor)
	fmt.Println("exe max_processor")

	//work dir 有什么用
	dir, err := common.Conf.String("global", "root_dir")
	if err == nil {
		err = os.Chdir(dir)
		if err != nil {
			return fmt.Errorf("change working directory to %s failed:%s, dir, err.Error()")
		}
	}
	common.Dir, _ = os.Getwd()
	fmt.Println("work directory:" + common.Dir)

	//
	num := common.GetConfInt("global", "work_num", 1)
	if num < 1 {
		return fmt.Errorf("work number must bigger than 1")
	}

	//生成channel
	common.WorkerNum = num
	common.PacketChans = make([]chan []byte, num)
	for i := 0; i < num; i++ {
		common.PacketChans[i] = make(chan []byte, common.GetConfInt("server", "packet_chan_size", 10000))
		if common.PacketChans[i] == nil {
			return fmt.Errorf("make packet channel failed")
		}
	}

	fmt.Println("initialize over")
	fmt.Printf("Program %s start success in %s at :%s, Max processor:%d Worker number:%d \n", VERSION, common.Dir, time.Now(), max_processor, num)
	return nil
}
Exemple #2
0
func initialize() error {
	fmt.Println("initilize ...")

	confFile := flag.String("c", "./etc/transfer.conf", "config file name")
	if confFile == nil {
		return fmt.Errorf("no config file")
	}
	flag.Parse()

	fmt.Printf("load configuration:%s\n", *confFile)
	conf, err := config.ReadDefault(*confFile)
	if err != nil {
		return fmt.Errorf("load config file failed:%s\n", err.Error())
	}
	common.Conf = conf

	max_processor := common.GetConfInt("global", "max_processor", runtime.NumCPU())
	runtime.GOMAXPROCS(max_processor)

	dir, err := common.Conf.String("global", "root_dir")
	if err == nil {
		//workding directory is resigned, change to that...
		err = os.Chdir(dir)
		if err != nil {
			return fmt.Errorf("change working directory to %s failed:%s\n", dir, err.Error())
		}
	}
	common.Dir, _ = os.Getwd()

	num := common.GetConfInt("global", "worker_num", 1)
	if num < 1 {
		return fmt.Errorf("work number must be larger than 1")
	}

	common.WorkerNum = num
	common.PacketChans = make([]chan []byte, num)
	for i := 0; i < num; i++ {
		common.PacketChans[i] = make(chan []byte, common.GetConfInt("server", "packet_chan_size", 10000))
		if common.PacketChans[i] == nil {
			return fmt.Errorf("make packet channel failed")
		}
	}

	fmt.Println("initilize over")
	fmt.Printf("Program %s start success in %s at: %s, Max processor:%d Worker number:%d\n", VERSION, common.Dir, time.Now(), max_processor, num)
	common.Alarm(VERSION + "-start")

	return nil
}