Ejemplo n.º 1
0
func createUdsClients() {
	socket, err := thrift.NewTSocketTimeout(hostUds, 1*time.Second)
	if err != nil {
		log.Fatal("uds socket error:", err)
	}
	transport := thrift.NewTBufferedTransport(socket, 10240)
	inputProtocol := thrift.NewTBinaryProtocolTransport(transport)
	outputProtocol := thrift.NewTMultiplexedProtocol(inputProtocol, "userManager")
	userManagerClient := services.NewUserManagerClientProtocol(transport, inputProtocol, outputProtocol)
	if err := transport.Open(); err != nil {
		log.Fatal("uds transport open error[userManager]:", err)
	}
	userManagerClients <- userManagerClient

	socket, err = thrift.NewTSocketTimeout(hostUds, 1*time.Second)
	if err != nil {
		log.Fatal("uds socket error:", err)
	}
	transport = thrift.NewTBufferedTransport(socket, 10240)
	inputProtocol = thrift.NewTBinaryProtocolTransport(transport)
	outputProtocol = thrift.NewTMultiplexedProtocol(inputProtocol, "groupManager")
	groupManagerClient := services.NewGroupManagerClientProtocol(transport, inputProtocol, outputProtocol)
	if err := transport.Open(); err != nil {
		log.Fatal("uds transport open error[groupManager]:", err)
	}
	groupManagerClients <- groupManagerClient
}
Ejemplo n.º 2
0
func Benchmark_CallEchoService(b *testing.B) {
	socket, err := thrift.NewTSocketTimeout(net.JoinHostPort("127.0.0.1", "8080"), TIMEOUT)
	if err != nil {
		t.Fatal("Unable to new client socket", err)
	}

	transport := thrift.NewTFramedTransport(socket)
	var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport)
	protocol = thrift.NewTMultiplexedProtocol(protocol, "EchoService")
	client := rpc.NewEchoServiceClientProtocol(transport, protocol, protocol)

	err = transport.Open()
	if err != nil {
		t.Fatal("Unable to open client socket", err)
	}
	defer transport.Close()

	for i := 0; i < b.N; i++ { //use b.N for looping
		_, err2 := client.Echo(currentTimeMillis(), "hello world")
		if err2 != nil {
			exception := thrift.NewTTransportExceptionFromError(err2)
			t.Fatal("test TestCallEchoService failed", exception.TypeId(), exception.Err())
		}
	}
}
Ejemplo n.º 3
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 = 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.º 4
0
func (self *FlumeClient) Connect() error {

	var tsocket *thrift.TSocket
	var err error
	//创建一个物理连接
	tsocket, err = thrift.NewTSocketTimeout(net.JoinHostPort(self.host, strconv.Itoa(self.port)), 10*time.Second)
	if nil != err {
		log.Printf("FLUME_CLIENT|CREATE TSOCKET|FAIL|%s|%s\n", self.HostPort(), err)
		return err
	}

	self.tsocket = tsocket

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

	//TLV 方式传输
	protocolFactory := thrift.NewTCompactProtocolFactory()

	//使用非阻塞io来传输
	self.transport = transportFactory.GetTransport(tsocket)

	self.thriftclient = flume.NewThriftSourceProtocolClientFactory(self.transport, protocolFactory)

	if err := self.transport.Open(); nil != err {
		log.Printf("FLUME_CLIENT|CREATE THRIFT CLIENT|FAIL|%s|%s", self.HostPort(), err)
		return err
	}

	self.status = STATUS_READY

	go self.checkAlive()

	return nil
}
Ejemplo n.º 5
0
func main() {

	fmt.Println("Hello World!")
	log.SetFlags(log.Ldate | log.Lmicroseconds)

	for i := 0; i < cap(clientsPool); i++ {
		socket, err := thrift.NewTSocketTimeout(hostUds, 1*time.Second)
		if err != nil {
			log.Fatal("uds socket error:", err)
		}
		transport := thrift.NewTBufferedTransport(socket, 10240)
		inputProtocol := thrift.NewTBinaryProtocolTransport(transport)
		outputProtocol := thrift.NewTMultiplexedProtocol(inputProtocol, "userManager")
		userManagerClient := services.NewUserManagerClientProtocol(transport, inputProtocol, outputProtocol)

		if err := transport.Open(); err != nil {
			log.Fatal("uds transport open error[userManager]:", err)
		}
		clientsPool <- userManagerClient
	}

	log.Print("start...")

	times := 100000

	ret := make(chan int, times)

	for i := 0; i < times; i++ {
		go func() {
			fromInfo := entity.NewFromInfo()
			fromInfo.Ctx = getUdsCtx()
			fromInfo.FromUid = 1000501
			fromInfo.FromIid = IID
			fromInfo.FromNjs = NJS

			userManagerClient := <-clientsPool
			//			_, err := userManagerClient.GetUserByPhone("15201218403")
			//			_, err := userManagerClient.GetUserById(1000501, entity.DBQueryStatus_NORMAL)
			err := userManagerClient.GetUserById(fromInfo.FromUid, fromInfo)
			clientsPool <- userManagerClient
			if err != nil {
				log.Fatal(err)
			}
			//			log.Print(u.GetBasic().Uid)
			ret <- 1
		}()
	}

	for i := 0; i < times; i++ {
		<-ret
	}

	log.Print("end...")

}
Ejemplo n.º 6
0
func NewThriftRequestExec(tFac thrift.TTransportFactory, clientExec ThriftClientExecutor, timeout time.Duration, hosts ...string) bender.RequestExecutor {
	return func(_ int64, request interface{}) (interface{}, error) {
		addr := hosts[rand.Intn(len(hosts))]
		socket, err := thrift.NewTSocketTimeout(addr, timeout)
		if err != nil {
			return nil, err
		}
		defer socket.Close()

		transport := tFac.GetTransport(socket)
		if err := transport.Open(); err != nil {
			return nil, err
		}
		defer transport.Close()

		return clientExec(request, transport)
	}
}
Ejemplo n.º 7
0
func (self *connection) Connect() error {
	log.Debug("Connecting to %v...", self.addr)

	// Creating connection
	var err error
	self.transportFactory = thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	self.protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	self.socket, err = thrift.NewTSocketTimeout(self.addr, self.timeout)
	if err != nil {
		return fmt.Errorf("socket error: %v", err.Error())
	}
	self.transport = self.transportFactory.GetTransport(self.socket)
	err = self.transport.Open()
	if err != nil {
		return fmt.Errorf("transport open error: %v", err.Error())
	}
	self.renderClient = gopnikrpc.NewRenderClientFactory(self.transport, self.protocolFactory)
	return nil
}
Ejemplo n.º 8
0
func (c *Client) ensureConnected() error {
	if c.ttransport != nil {
		return nil
	}

	transport, err := thrift.NewTSocketTimeout(c.server, c.Timeout)
	if err == nil {
		err = transport.Open()
		if err == nil {
			c.ttransport = thrift.NewTFramedTransport(transport)
			c.tclient = NewKestrelClientFactory(c.ttransport, thrift.NewTBinaryProtocolFactoryDefault())
			return nil
		} else {
			return err
		}
	} else {
		return err
	}
}
Ejemplo n.º 9
0
func (self *RenderSelector) connect(addr string) (*thriftConn, error) {
	var err error
	conn := &thriftConn{
		Addr: addr,
	}

	conn.Socket, err = thrift.NewTSocketTimeout(addr, self.timeout)
	if err != nil {
		return nil, err
	}

	conn.Transport = self.transportFactory.GetTransport(conn.Socket)
	err = conn.Transport.Open()
	if err != nil {
		return nil, err
	}

	conn.Client = gopnikrpc.NewRenderClientFactory(conn.Transport, self.protocolFactory)

	return conn, nil
}
Ejemplo n.º 10
0
func (self *FlumeClient) Connect() error {

	var tsocket *thrift.TSocket
	var err error
	//创建一个物理连接
	tsocket, err = thrift.NewTSocketTimeout(net.JoinHostPort(self.host, strconv.Itoa(self.port)), 10*time.Second)
	if nil != err {
		log.Printf("FLUME_CLIENT|CREATE TSOCKET|FAIL|%s|%s\n", self.HostPort(), err)
		return err
	}

	self.tsocket = tsocket
	self.transportFactory = thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	//TLV 方式传输
	self.protocolFactory = thrift.NewTCompactProtocolFactory()

	self.clientConn()
	self.status = STATUS_READY
	go self.checkAlive()

	return nil
}
Ejemplo n.º 11
0
// ThriftClientFactory is an thrift Client factory which creates a connection
// that uses a thrift codec.
func ThriftClientFactory(addr string) func() (io.Closer, error) {
	return func() (io.Closer, error) {
		var transport thrift.TTransport
		socketTransport, err := thrift.NewTSocketTimeout(addr, defaultTimeout)
		if err != nil {
			return nil, err
		}
		transport = thrift.NewTFramedTransport(
			thrift.NewTBufferedTransport(socketTransport, defaultBufferSize))
		protocol := thrift.NewTBinaryProtocolFactoryDefault()
		if err := transport.Open(); err != nil {
			return nil, err
		}

		client := NewHbaseClientFactory(transport, protocol)
		return &clientCloser{
			mu:          &sync.Mutex{},
			conn:        socketTransport,
			HbaseClient: client,
		}, nil
	}
}
Ejemplo n.º 12
0
func main() {
	// redis
	redisClient, _ := cluster.New(hostRedis)
	go func() {
		for {
			//			str, err := redisClient.Cmd("rpop", fmt.Sprintf("%d_%s", UID, IID)).Str()
			//			if err != nil {
			//				log.Println("noti redis error:", err)
			//			}
			//			if str != "" {
			//				fmt.Println("--: " + str)
			//			}

			str, _ := redisClient.Cmd("rpop", fmt.Sprintf("op_%d_%s", UID, IID)).Str()
			//			if err != nil {
			//				log.Println("op redis error:", err)
			//			}
			if str != "" {
				fmt.Println("--: " + str)
			}

			//				dec := json.NewDecoder(strings.NewReader(str))
			//				dec.UseNumber()
			//				udsResult := &UdsResult{}
			//				dec.Decode(udsResult)

			//				ctx := udsResult.Ctx
			//				context, ok := udsCallCtxs[ctx]
			//				if !ok {
			//					fmt.Println(ctx, "is nil. mn: ", udsResult.Mn)
			//					continue
			//				}

			//				clientResult := &ClientResult{}
			//				clientResult.Ctx = context.ClientContextId
			//				code, _ := strconv.ParseInt(udsResult.Code, 10, 32)
			//				clientResult.Code = int(code)
			//				clientResult.Mn = context.ClientMn + "_result"
			//				clientResult.Body = udsResult.Body

			//				b, _ := json.Marshal(clientResult)

			//				so := context.So
			//				(*so).Emit("videbonjs10", string(b))
			//				delete(udsCallCtxs, ctx)

		}
	}()
	// uds
	socket, _ := thrift.NewTSocketTimeout(hostUds, 3*time.Second)
	transport := thrift.NewTBufferedTransport(socket, 10240)
	protocol := thrift.NewTBinaryProtocolTransport(transport)
	//mprotocol := thrift.NewTMultiplexedProtocol(protocol, "userManager")
	//userManagerClient = service.NewUserManagerClientProtocol(transport, protocol, mprotocol)
	mprotocol := thrift.NewTMultiplexedProtocol(protocol, "chatManager")
	chatManagerClient := service.NewChatManagerClientProtocol(transport, protocol, mprotocol)
	err := transport.Open()
	if err != nil {
		log.Fatal("transport open error:", err)
	}

	fromInfo := entity.NewFromInfo()
	fromInfo.Ctx = getUdsCtx()
	fromInfo.FromUid = UID
	fromInfo.FromIid = IID
	fromInfo.FromNjs = NJS

	//	context := Context{}
	//	context.So = so
	//	context.ClientContextId = msg.Ctx
	//	context.ClientMn = msg.Mn
	//udsCallCtxs[fromInfo.Ctx] = context

	var index int64 = 0
	for i := 0; i < 100; i++ {
		func() {
			index++
			log.Println(index)
			message := &entity.ChatMessage{
				Uid:     1000501,
				Gid:     803,
				Cmt:     1,
				Content: fmt.Sprintf("message testing %d", index),
			}
			mid, err := chatManagerClient.SendChatMessage(message, fromInfo)
			if err != nil {
				log.Fatal("SendChatMessage error:", err)
				//delete(udsCallCtxs, fromInfo.Ctx)
			}
			log.Println("mid:", mid)
		}()

		//		err = transport.Close()
		//		if err != nil {
		//			log.Fatal("transport close error:", err)
		//		}
	}

	time.Sleep(10 * time.Second)

	//	// socketio
	//	server, err := socketio.NewServer(nil)
	//	if err != nil {
	//		log.Fatal(err)
	//	}
	//	server.On("connection", func(so socketio.Socket) {
	//		log.Println("on connection")
	//		so.Join("chat")
	//		so.On("videboc10", func(msg ClientMessage) {
	//			log.Println("on videboc10:", msg)

	//			handleClientMessage(msg, &so)
	//		})
	//		so.On("disconnection", func(reason string) {
	//			log.Println("on disconnection. reason: " + reason)
	//		})

	//		so.On("error", func(error string) {
	//			log.Println("on error. error:", error)
	//		})
	//	})
	//	server.On("error", func(so socketio.Socket, err error) {
	//		log.Println("error:", err)
	//	})

	//	http.Handle("/socket.io/", server)
	//	http.Handle("/", http.FileServer(http.Dir("./asset")))
	//	log.Println("Serving at localhost:5000...")
	//	log.Fatal(http.ListenAndServe(":5000", nil))
}
Ejemplo n.º 13
0
func main() {
	socket, _ := thrift.NewTSocketTimeout(hostUds, 30000000)
	transport := thrift.NewTBufferedTransport(socket, 10240)
	protocol := thrift.NewTBinaryProtocolTransport(transport)
	mprotocol := thrift.NewTMultiplexedProtocol(protocol, "userManager")
	userManagerClient := service.NewUserManagerClientProtocol(transport, protocol, mprotocol)
	_ = transport.Open()

	fromInfo := entity.NewFromInfo()
	fromInfo.Ctx = 1
	fromInfo.FromUid = UID
	fromInfo.FromIid = IID
	fromInfo.FromNjs = NJS

	redisClusterClient, _ := cluster.New(hostRedis)
	count := 0
	go func() {
		for {
			udsResult, _ := redisClusterClient.Cmd("rpop", fromInfo.FromNjs).Str()
			if udsResult != "" {
				fmt.Println(udsResult)
				count++
				fmt.Println(count)
				result := &UdsResult{}
				dec := json.NewDecoder(strings.NewReader(udsResult))
				dec.UseNumber()

				if err := dec.Decode(result); err != nil {
					log.Fatal(err)
				}

				//				fmt.Println(result)
				//				b, _ := json.Marshal(result)
				//				fmt.Println(string(b))
			}
		}
	}()

	// socketio
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.On("connection", func(so socketio.Socket) {
		log.Println("on connection")
		so.Join("chat")
		so.On("videboc10", func(msg map[string]interface{}) {
			log.Println("on videboc10:", msg)

			dec := json.NewDecoder(strings.NewReader(msg))
			dec.UseNumber()
			clientMessage := &ClientMessage{}
			if err := dec.Decode(clientMessage); err != nil {
				log.Fatal(err)
			}

			log.Println(clientMessage.Mn)

			if "login" == clientMessage.Mn {
				_loginName := clientMessage.Body["login_name"]
				_phoneInfo := clientMessage.Body["phone_info"]
				_encryptMode := clientMessage.Body["encryptMode"]
				_isSecreted := clientMessage.Body["isSecreted"]
				_iid := clientMessage.Body["iid"]

				loginName, _ := _loginName.(string)
				phoneInfo, _ := _phoneInfo.(string)
				encryptMode, _ := _encryptMode.(string) // AES
				isSecreted, _ := _isSecreted.(bool)     // TRUE
				iid, _ := _iid.(string)

				fmt.Println(loginName)
				fmt.Println(phoneInfo)
				fmt.Println(encryptMode)
				fmt.Println(isSecreted)
				fmt.Println(iid)

				reUid := regexp.MustCompile(`^\d{0,10}$`)
				rePhone := regexp.MustCompile(`^\d{11,12}$`)
				if reUid.MatchString(loginName) {
					uid, _ := strconv.ParseInt(loginName, 10, 32)
					err := userManagerClient.GetUserById(uid, fromInfo)
					if err != nil {
						fmt.Println(err)
					}

				} else if rePhone.MatchString(loginName) {

				} else {

				}
			}

		})
		so.On("disconnection", func(reason string) {
			log.Println("on disconnection. reason:", reason)
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		log.Println("error:", err)
	})

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Serving at localhost:5000...")
	log.Fatal(http.ListenAndServe(":5000", nil))
}
Ejemplo n.º 14
0
Archivo: hpool.go Proyecto: 1d7500/bfs
func Init(hbaseAddr string, hbaseTimeout time.Duration, hbaseMaxIdle, hbaseMaxActive int) error {
	// init hbase thrift pool
	hbasePool = New(func() (c *HbaseConn, err error) {
		var trans thrift.TTransport
		trans, err = thrift.NewTSocketTimeout(hbaseAddr, hbaseTimeout)
		if err != nil {
			log.Error("thrift.NewTSocketTimeout error(%v)", err)
			return
		}
		trans = thrift.NewTFramedTransport(trans)
		c = new(HbaseConn)
		c.conn = hbasethrift.NewTHBaseServiceClientFactory(trans, thrift.NewTBinaryProtocolFactoryDefault())
		if err = trans.Open(); err != nil {
			log.Error("trans.Open error(%v)", err)
			return
		}
		c.tput.ColumnValues = []*hbasethrift.TColumnValue{
			// vid
			&hbasethrift.TColumnValue{
				Family:    familyBasic,
				Qualifier: columnVid,
				Value:     c.vbuf[:],
			},
			// cookie
			&hbasethrift.TColumnValue{
				Family:    familyBasic,
				Qualifier: columnCookie,
				Value:     c.cbuf[:],
			},
			// insert_time
			&hbasethrift.TColumnValue{
				Family:    familyBasic,
				Qualifier: columnInsertTime,
				Value:     c.ibuf[:],
			},
		}
		c.tdel.Columns = []*hbasethrift.TColumn{
			// vid
			&hbasethrift.TColumn{
				Family:    familyBasic,
				Qualifier: columnVid,
			},
			// cookie
			&hbasethrift.TColumn{
				Family:    familyBasic,
				Qualifier: columnCookie,
			},
			// insert_time
			&hbasethrift.TColumn{
				Family:    familyBasic,
				Qualifier: columnInsertTime,
			},
		}
		return
	}, func(c *HbaseConn) error {
		client := c.conn
		if client != nil && client.Transport != nil {
			client.Transport.Close()
		}
		return nil
	}, hbaseMaxIdle)
	hbasePool.MaxActive = hbaseMaxActive
	hbasePool.IdleTimeout = lvsTimeout
	return nil
}