Exemplo n.º 1
0
Arquivo: server.go Projeto: yylover/go
func NewServer() *Server {
	s := &Server{
		port:          common.GetConfInt("server", "port", 8888),
		maxClients:    common.GetConfInt("server", "max_clients", 10000),
		clientNum:     0,
		acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5),
		connTimeout:   common.GetConfSecond("server", "connection_timeout", 60*3),
		slowread:      common.GetConfSecond("server", "slow_read", 0),
		headerLen:     common.GetConfInt("server", "header_length", 10),
		maxBodyLen:    common.GetConfInt("server", "max_body_length", 102400),
		running:       false,
		logger:        common.NewLogger("server"),
		heartBeat:     time.Now().Unix(),
		workerNum:     common.WorkerNum,
		currentWorker: -1,
	}

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

	if s.logger == nil {
		fmt.Println("New Server logger failed")
		return nil
	}

	s.ip = common.GetConfString("server", "bind", "")

	// rlimit 是什么限制
	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
}
Exemplo n.º 2
0
func NewBackend(idx int, inputChan chan []byte) *Backend {
	b := &Backend{
		index:      idx,
		backendNum: 0,
		heartbeat:  time.Now().Unix(),
		logger:     common.NewLogger("backend"),
		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("InputChan init failed")
		return nil
	}

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

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

		back := common.GetConfString("backend", option, "")
		if back == "" {
			fmt.Printf("Read conf %s failed, error:%s, getBackend total number:%d \n", option, err.Error(), b.backendNum)
			continue
		}

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

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

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

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

		if point == nil {
			fmt.Println("New backPoint failed")
			return nil //不需要退出?
		}

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

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

	return b
}