コード例 #1
0
ファイル: carrier_svr.go プロジェクト: huangzhiyong/carrier
func ServeForClient(listenAddr string) {
	var (
		serverSocket *socket.TServerSocket
		serverConn   net.Conn
		err          error
	)
	if serverSocket, err = socket.NewTServerSocket(listenAddr); err != nil {
		panic(err)
	}
	if err = serverSocket.Listen(); err != nil {
		panic(err)
	}
	defer serverSocket.Close()

	// AcceptLoop
	for {
		if serverConn, err = serverSocket.Accept(); err != nil {
			logger.Notice("Socket To Client Accept: ", err)
			continue
		}
		go func(connClient net.Conn) {
			defer func() {
				logger.Info("客户端断开连接: ", connClient.RemoteAddr().String())
				connClient.Close()
			}()
			logger.Info("客户端新连接: ", connClient.RemoteAddr().String())
			ClientLogic(connClient)
		}(serverConn)
	}
}
コード例 #2
0
ファイル: cluster.go プロジェクト: fucan/carrier
func NewCluster(
	serverList []string, refreshInterval time.Duration,
	maxIdle int, testOnBorrowTimeout time.Duration,
	connectTimeout time.Duration, readTimeout time.Duration, writeTimeout time.Duration,
) (Cluster, error) {
	if len(serverList) <= 0 {
		return nil, fmt.Errorf("cluster配置节点为空")
	}

	var c *cluster = &cluster{
		refreshInterval:     refreshInterval,
		clusterNodePool:     make(map[string]connection.Pool),
		clusterSlots:        slots.NewSlots(),
		maxIdle:             maxIdle,
		testOnBorrowTimeout: testOnBorrowTimeout,
		connectTimeout:      connectTimeout,
		readTimeout:         readTimeout,
		writeTimeout:        writeTimeout,
	}
	var (
		err    error
		server string = serverList[rand.Intn(len(serverList))]
	)
	// 初始化
	if err = c.refresh(server); err != nil {
		return nil, err
	}
	// 定时刷新
	go func() {
		for range time.Tick(refreshInterval) {
			var (
				err    error
				server string = serverList[rand.Intn(len(serverList))]
			)
			if err = c.refresh(server); err != nil {
				logger.Info("节点(%s):刷新cluster slots出错(%v)", server, err)
			}
		}
	}()
	return c, nil
}