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 }
// // 给定thrift Message, 解码出: typeId, seqId // func DecodeThriftTypIdSeqId(data []byte) (typeId thrift.TMessageType, seqId int32, err error) { transport := NewTMemoryBufferWithBuf(data) protocol := thrift.NewTBinaryProtocolTransport(transport) _, typeId, seqId, err = protocol.ReadMessageBegin() return }
// // 解析Thrift数据的Message Header // func ParseThriftMsgBegin(msg []byte) (name string, typeId thrift.TMessageType, seqId int32, err error) { transport := thrift.NewTMemoryBufferLen(256) transport.Write(msg) protocol := thrift.NewTBinaryProtocolTransport(transport) name, typeId, seqId, err = protocol.ReadMessageBegin() return }
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()) } } }
// // 将Request中的SeqNum进行替换(修改Request部分的数据) // func (r *Request) ReplaceSeqId(newSeq int32) { if r.Request.Data != nil { // log.Printf(Green("Replace SeqNum: %d --> %d"), r.Request.SeqId, newSeq) r.Response.SeqId = newSeq start := 0 if r.ProxyRequest { start = len(r.Service) } if start > 0 { start += 1 // ":" // log.Printf("Service: %s, Name: %s\n", r.Service, r.Request.Name) } transport := NewTMemoryBufferWithBuf(r.Request.Data[start:start]) protocol := thrift.NewTBinaryProtocolTransport(transport) protocol.WriteMessageBegin(r.Request.Name, r.Request.TypeId, newSeq) if start > 0 { r.Request.DataOrig = r.Request.Data } // 将service从name中剥离出去 r.Request.Data = r.Request.Data[start:len(r.Request.Data)] } }
func SerializeSpan(s *zipkincore.Span) ([]byte, error) { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) err := s.Write(p) if err != nil { return nil, err } return t.Buffer.Bytes(), nil }
func fakeData(name string, typeId thrift.TMessageType, seqId int32, buf []byte) int { transport := NewTMemoryBufferWithBuf(buf) protocol := thrift.NewTBinaryProtocolTransport(transport) // 切换回原始的SeqId protocol.WriteMessageBegin(name, typeId, seqId) return transport.Buffer.Len() }
func (r *Request) RestoreSeqId() { if r.Response.Data != nil { transport := NewTMemoryBufferWithBuf(r.Response.Data[0:0]) protocol := thrift.NewTBinaryProtocolTransport(transport) // 切换回原始的SeqId // r.Response.TypeId 和 r.Request.TypeId可能不一样,要以Response为准 protocol.WriteMessageBegin(r.Request.Name, r.Response.TypeId, r.Request.SeqId) } }
func (c *UDPCollector) send(s *zipkin.Span) error { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) err := s.Write(p) if err != nil { return err } _, err = c.conn.WriteToUDP(t.Buffer.Bytes(), c.addr) return err }
// Collect will serialize and send a zipkin.Span to the configured Scribe // endpoint func (c *ScribeCollector) Collect(s *zipkin.Span) { t := thrift.NewTMemoryBuffer() p := thrift.NewTBinaryProtocolTransport(t) err := s.Write(p) if err != nil { logger.Errore(err) } else { logger.Errore(c.CollectSerialized(t.Buffer.Bytes())) } }
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...") }
// // 从Request.Data中读取出 Request的Name, TypeId, SeqId // RequestName可能和thrift package中的name不一致,Service部分从Name中剔除 // func (r *Request) DecodeRequest() { transport := NewTMemoryBufferWithBuf(r.Request.Data) protocol := thrift.NewTBinaryProtocolTransport(transport) r.Request.Name, r.Request.TypeId, r.Request.SeqId, _ = protocol.ReadMessageBegin() // 参考 : TMultiplexedProtocol nameFields := strings.SplitN(r.Request.Name, thrift.MULTIPLEXED_SEPARATOR, 2) if len(nameFields) != 2 { r.Service = "" } else { r.Service = nameFields[0] r.Request.Name = nameFields[1] } }
func newScribeConn(addr *net.TCPAddr) (*scribeConn, error) { transport := thrift.NewTFramedTransport( thrift.NewTSocketFromAddrTimeout(addr, 10*time.Second)) err := transport.Open() if err != nil { return nil, err } proto := thrift.NewTBinaryProtocolTransport(transport) conn := scribeConn{ transport: transport, client: scribe.NewScribeClientProtocol(transport, proto, proto), } return &conn, nil }
func GetWorkerNotFoundData(service string, seqId int32) []byte { // 构建thrift的Transport transport := thrift.NewTMemoryBufferLen(1024) protocol := thrift.NewTBinaryProtocolTransport(transport) // 构建一个Message, 写入Exception msg := fmt.Sprintf("Worker: %s Not Found", service) exc := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, msg) protocol.WriteMessageBegin(service, thrift.EXCEPTION, seqId) exc.Write(protocol) protocol.WriteMessageEnd() bytes := transport.Bytes() return bytes }
func GetWorkerNotFoundData(req *Request, module string) []byte { req.Response.TypeId = thrift.EXCEPTION // 构建thrift的Transport transport := thrift.NewTMemoryBufferLen(100) protocol := thrift.NewTBinaryProtocolTransport(transport) // 构建一个Message, 写入Exception msg := fmt.Sprintf("Worker FOR %s#%s.%s Not Found", module, req.Service, req.Request.Name) exc := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, msg) protocol.WriteMessageBegin(req.Request.Name, thrift.EXCEPTION, req.Request.SeqId) exc.Write(protocol) protocol.WriteMessageEnd() bytes := transport.Bytes() return bytes }
func NewPingRequest() *Request { // 构建thrift的Transport transport := NewTMemoryBufferLen(30) protocol := thrift.NewTBinaryProtocolTransport(transport) protocol.WriteMessageBegin("ping", MESSAGE_TYPE_HEART_BEAT, 0) protocol.WriteMessageEnd() protocol.Flush() r := &Request{} // 告诉Request, Data中不包含service,在ReplaceSeqId时不需要特别处理 r.ProxyRequest = false r.Start = time.Now().Unix() r.Request.Data = transport.Bytes() r.Request.Name = "ping" r.Request.SeqId = 0 // SeqId在这里无效,因此设置为0 r.Request.TypeId = MESSAGE_TYPE_HEART_BEAT return r }
func GetThriftException(req *Request, module string) []byte { req.Response.TypeId = thrift.EXCEPTION // 构建thrift的Transport transport := thrift.NewTMemoryBufferLen(256) protocol := thrift.NewTBinaryProtocolTransport(transport) msg := fmt.Sprintf("Module: %s, Service: %s, Method: %s, Error: %v", module, req.Service, req.Request.Name, req.Response.Err) // 构建一个Message, 写入Exception exc := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, msg) protocol.WriteMessageBegin(req.Service, thrift.EXCEPTION, req.Request.SeqId) exc.Write(protocol) protocol.WriteMessageEnd() bytes := transport.Bytes() return bytes }
// // 生成Thrift格式的Exception Message // func GetServiceNotFoundData(req *Request) []byte { req.Response.TypeId = thrift.EXCEPTION // 构建thrift的Transport transport := thrift.NewTMemoryBufferLen(100) protocol := thrift.NewTBinaryProtocolTransport(transport) // 构建一个Message, 写入Exception msg := fmt.Sprintf("Service: %s Not Found", req.Service) exc := thrift.NewTApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION, msg) protocol.WriteMessageBegin(req.Request.Name, thrift.EXCEPTION, req.Request.SeqId) exc.Write(protocol) protocol.WriteMessageEnd() protocol.Flush() bytes := transport.Bytes() return bytes }
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)) }
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)) }