Ejemplo n.º 1
0
//
// 确保Socket成功连接到后端服务器
//
func (bc *BackendConn) ensureConn() (transport thrift.TTransport, err error) {
	// 1. 创建连接(只要IP没有问题, err一般就是空)
	timeout := time.Second * 5
	if strings.Contains(bc.addr, ":") {
		transport, err = thrift.NewTSocketTimeout(bc.addr, timeout)
	} else {
		transport, err = rpc_utils.NewTUnixDomainTimeout(bc.addr, timeout)
	}
	log.Printf(Cyan("[%s]Create Socket To: %s"), bc.service, bc.addr)

	if err != nil {
		log.ErrorErrorf(err, "[%s]Create Socket Failed: %v, Addr: %s", err, bc.service, bc.addr)
		// 连接不上,失败
		return nil, err
	}

	// 2. 只要服务存在,一般不会出现err
	sleepInterval := 1
	err = transport.Open()
	for err != nil && !bc.IsMarkOffline.Get() {
		log.ErrorErrorf(err, "[%s]Socket Open Failed: %v, Addr: %s", bc.service, err, bc.addr)

		// Sleep: 1, 2, 4这几个间隔
		time.Sleep(time.Duration(sleepInterval) * time.Second)

		if sleepInterval < 4 {
			sleepInterval *= 2
		}
		err = transport.Open()
	}
	return transport, err
}
Ejemplo n.º 2
0
//
// 创建一个TFramedTranport, 支持: tcp socket,unix domain socket
// 1. tcp socket的格式: 60.29.255.199:5550
// 2. unix domain的格式: /usr/local/rpc_proxy/proxy.sock
//
func NewTFramedTransportWithTimeout(addr string, timeout time.Duration) (transport thrift.TTransport, err error) {
	if strings.Contains(addr, ":") {
		transport, err = thrift.NewTSocketTimeout(addr, timeout)
	} else {
		transport, err = NewTUnixDomainTimeout(addr, timeout)
	}
	return
}