func (conn *conn) error(err error, desc string) { if err == nil { return } if nerr, ok := err.(net.Error); ok { switch { case nerr.Timeout(), nerr.Temporary(): logger.Infof("客户端连接: %s %v", conn.c.RemoteAddr().String(), nerr) return } } logger.Infof("客户端连接出错: %s %s %v", conn.c.RemoteAddr().String(), desc, err) conn.Close() }
func checkIdleTimeout() { var ( carrierConnList []Conn = getBroadcastConn() carrierConn Conn timeoutTimestamp time.Time = time.Now().Add(-DEFAULT_IDLE_TIME_OUT) ) sort.Sort(ByActiveTime(carrierConnList)) for _, carrierConn = range carrierConnList { if carrierConn.GetLatestActiveTime().After(timeoutTimestamp) { return } logger.Infof("客户端空闲连接超时: %s", carrierConn.GetConnAddr()) carrierConn.Close() } }
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.Infof("节点(%s):刷新cluster slots出错(%v)", server, err) } } }() return c, nil }