示例#1
0
文件: server.go 项目: dengzhi007/sky
func NewServer() *Server {
	s := &Server{
		port:          common.GetConfInt("server", "port", 8080),
		maxClients:    common.GetConfInt("server", "max_clients", 100000),
		clientNum:     0,
		acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5),
		connTimeout:   common.GetConfSecond("server", "connection_timeout", 60*3),
		headerLen:     common.GetConfInt("server", "header_length", 10),
		maxBodyLen:    common.GetConfInt("server", "max_body_length", 102400),
		running:       false,
		heartbeat:     time.Now().Unix(),
		workerNum:     common.WorkerNum,
		currentWorker: -1,
		logger:        common.NewLogger("server"),
	}

	if s == nil {
		fmt.Println("new server failed")
		return nil
	}

	if s.logger == nil {
		fmt.Println("new server logger failed")
		return nil
	}

	ip, err := common.Conf.String("server", "bind")
	if err != nil {
		ip = ""
	}
	s.ip = ip

	return s
}
示例#2
0
文件: server.go 项目: dengzhi007/sky
func NewServer() *Server {
	s := &Server{
		port:          common.GetConfInt("server", "port", 8080),
		maxClients:    common.GetConfInt("server", "max_clients", 100000),
		clientNum:     0,
		acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5),
		connTimeout:   common.GetConfSecond("server", "connection_timeout", 60*3),
		headerLen:     common.GetConfInt("server", "header_length", 10),
		maxBodyLen:    common.GetConfInt("server", "max_body_length", 102400),
		running:       false,
		heartbeat:     time.Now().Unix(),
		workerNum:     common.WorkerNum,
		currentWorker: -1,
		slowread:      common.GetConfSecond("server", "slow_read", 0),
		logger:        common.NewLogger("server"),
	}

	if s == nil {
		fmt.Println("new server failed")
		return nil
	}

	if s.logger == nil {
		fmt.Println("new server logger failed")
		return nil
	}

	ip, err := common.Conf.String("server", "bind")
	if err != nil {
		ip = ""
	}
	s.ip = ip

	max_clients := uint64(s.maxClients)
	if max_clients > 1024 {
		var rlim syscall.Rlimit
		err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
		if err != nil {
			fmt.Println("server get rlimit error: " + err.Error())
			return nil
		}
		rlim.Cur = max_clients
		rlim.Max = max_clients
		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
		if err != nil {
			fmt.Println("server set rlimit error: " + err.Error())
			return nil
		}
		s.logger.Info("set fd limit to %d", s.maxClients)
	}

	return s
}
示例#3
0
文件: main.go 项目: yylover/go
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
}
示例#4
0
文件: main.go 项目: dengzhi007/sky
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
}
示例#5
0
文件: updater.go 项目: dengzhi007/sky
func NewUpdater() *Updater {
	u := &Updater{
		running:   false,
		heartbeat: time.Now().Unix(),
		interval:  common.GetConfSecond("updater", "interval", 20*60),
		logger:    common.NewLogger("updater"),
	}

	if u == nil {
		fmt.Println("new updater failed")
		return nil
	}

	redisAddr, err := common.Conf.String("updater", "redis_addr")
	if err != nil {
		fmt.Printf("updater get redis_addr failed, err:%s\n", err.Error())
		return nil
	}
	u.redisCli.Addr = redisAddr
	u.redisCli.Db = common.GetConfInt("updater", "redis_db", 0)

	if u.logger == nil {
		fmt.Println("updater new logger failed")
		return nil
	}

	return u
}
示例#6
0
文件: backend.go 项目: dengzhi007/sky
func NewBackend(idx int, inputChan chan []byte, logging log.Logger) *Backend {
	b := &Backend{
		index:      idx,
		backendNum: 0,
		heartbeat:  time.Now().Unix(),
		logger:     logging,
		inChan:     inputChan,
		running:    false,
	}

	if b == nil {
		fmt.Println("new backend failed")
		return nil
	}

	if b.logger == nil {
		fmt.Println("backend new logger failed")
		return nil
	}

	if b.inChan == nil {
		fmt.Println("backend must has an input channel to get bytes for back point")
		return nil
	}

	options, err := common.Conf.Options("backend")
	if err != nil {
		fmt.Printf("backend get all options failed:%s\n", err.Error())
		return nil
	}

	b.backendList = make([]*Backpoint, 0, 4)
	for _, option := range options {
		if !strings.HasPrefix(option, "backend_list_") {
			continue
		}

		back, err := common.Conf.String("backend", option)
		if err != nil {
			fmt.Printf("Read conf %s failed, error: %s, get backend total number:%d\n", option, err.Error(), b.backendNum)
			break
		}

		backend_name := strings.TrimPrefix(option, "backend_list_")
		if backend_name == "" || backend_name == option {
			fmt.Printf("get backend name failed")
			break
		}

		addrs := strings.Split(back, ";")
		num := len(addrs)

		if num < 1 {
			fmt.Printf("one backend:%s must at least has one address", backend_name)
			continue
		}

		point := &Backpoint{
			name:           backend_name,
			backNum:        num,
			backAddrs:      addrs,
			conn:           nil,
			connTimeout:    common.GetConfSecond("backend", "connection_timeout", 180),
			sendTimeout:    common.GetConfSecond("backend", "send_timeout", 180),
			currentBackIdx: -1,
			retryTimes:     common.GetConfInt("backend", "retry_times", 5),
			retryInterval:  common.GetConfSecond("backend", "retry_interval", 5),
			recvBuf:        make([]byte, common.GetConfInt("backend", "receive_buffer_size", 10)),
			sendingChan:    make(chan []byte, common.GetConfInt("backend", "sending_buffer_size", 10000)),
		}

		b.backendList = append(b.backendList, point)
		b.backendNum += 1
		b.logger.Debug("Backend%d get a back %s: %s, %d points\n", idx, backend_name, back, num)
	}

	if b.backendNum < 1 {
		fmt.Println("no backends")
		return nil
	}

	return b
}
示例#7
0
文件: server.go 项目: dengzhi007/sky
func NewServer() *Server {
	s := &Server{
		port:          common.GetConfInt("server", "port", 8080),
		maxClients:    common.GetConfInt("server", "max_clients", 100000),
		headerLen:     common.GetConfInt("server", "header_length", 10),
		maxBodyLen:    common.GetConfInt("server", "max_body_length", 102400),
		acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5),
		connTimeout:   common.GetConfSecond("server", "connection_timeout", 60*3),
		clientNum:     0,
		currentWorker: -1,
		running:       false,
		heartbeat:     time.Now().Unix(),
		workerNum:     common.WorkerNum,
		logger:        common.NewLogger("server"),
		ipWhitelist:   nil,
		itemWhitelist: nil,
		itemBlacklist: nil,
	}

	if s == nil {
		fmt.Println("new server failed")
		return nil
	}

	if s.logger == nil {
		fmt.Println("new server logger failed")
		return nil
	}

	ip, err := common.Conf.String("server", "bind")
	if err != nil {
		//not set, bind all interface
		ip = ""
	}
	s.ip = ip

	white_list, err := common.Conf.String("server", "ip_whitelist")
	if err == nil {
		s.ipWhitelist = make(map[string]bool)
		for _, ip := range strings.Split(white_list, ";") {
			s.ipWhitelist[ip] = true
		}
	}
	s.logger.Debug("connection ip white list:%v", s.ipWhitelist)

	options, err := common.Conf.Options("item_blacklist")
	if err == nil && len(options) > 0 {
		s.itemBlacklist = make(map[int]string)
		for _, option := range options {
			itemName, err := common.Conf.String("item_blacklist", option)
			if err != nil {
				continue
			}
			itemId, _ := strconv.Atoi(option)
			s.itemBlacklist[itemId] = itemName
		}
		s.logger.Debug("packet item black list:%v", s.itemBlacklist)
	}

	options, err = common.Conf.Options("item_whitelist")
	if err == nil && len(options) > 0 {
		s.itemWhitelist = make(map[int]string)
		for _, option := range options {
			itemName, err := common.Conf.String("item_whitelist", option)
			if err != nil {
				continue
			}
			itemId, _ := strconv.Atoi(option)
			s.itemWhitelist[itemId] = itemName
		}
		s.logger.Debug("packet item white list:%v", s.itemWhitelist)
	}

	return s
}