func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) { var bufWriter = bufio.NewWriter(conn) // Write desired request err := types.WriteMessage(req, bufWriter) if err != nil { return nil, err } err = types.WriteMessage(types.RequestFlush(), bufWriter) if err != nil { return nil, err } err = bufWriter.Flush() if err != nil { return nil, err } // Read desired response var res = &types.Response{} err = types.ReadMessage(conn, res) if err != nil { return nil, err } var resFlush = &types.Response{} err = types.ReadMessage(conn, resFlush) if err != nil { return nil, err } if resFlush.Type != types.MessageType_Flush { return nil, errors.New(Fmt("Expected flush response but got something else: %v", resFlush.Type)) } return res, nil }
func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) { // Write desired request err := types.WriteMessage(req, conn) if err != nil { return nil, err } // Write flush request err = types.WriteMessage(types.RequestFlush(), conn) if err != nil { return nil, err } // Read desired response var res = &types.Response{} err = types.ReadMessage(conn, res) if err != nil { return nil, err } // Read flush response var resFlush = &types.Response{} err = types.ReadMessage(conn, resFlush) if err != nil { return nil, err } if resFlush.Type != types.MessageType_Flush { return nil, errors.New(Fmt("Expected types.MessageType_Flush but got %v instead", resFlush.Type)) } return res, nil }
func (cli *TMSPClient) sendRequestsRoutine() { for { select { case <-cli.flushTimer.Ch: select { case cli.reqQueue <- newReqRes(types.RequestFlush()): default: // Probably will fill the buffer, or retry later. } case <-cli.QuitService.Quit: return case reqres := <-cli.reqQueue: cli.willSendReq(reqres) err := types.WriteMessage(reqres.Request, cli.bufWriter) if err != nil { cli.StopForError(err) return } // log.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request) if reqres.Request.Type == types.MessageType_Flush { err = cli.bufWriter.Flush() if err != nil { cli.StopForError(err) return } } } } }
func (cli *TMSPClient) FlushSync() error { cli.queueRequest(types.RequestFlush()).Wait() return cli.err }
func (cli *TMSPClient) FlushAsync() *ReqRes { return cli.queueRequest(types.RequestFlush()) }
func TestStream(t *testing.T) { numAppendTxs := 200000 // Start the listener _, err := server.StartListener("tcp://127.0.0.1:46658", NewDummyApplication()) if err != nil { Exit(err.Error()) } // Connect to the socket conn, err := Connect("tcp://127.0.0.1:46658") if err != nil { Exit(err.Error()) } // Read response data done := make(chan struct{}) go func() { counter := 0 for { var res = &types.Response{} err := types.ReadMessage(conn, res) if err != nil { Exit(err.Error()) } // Process response switch res.Type { case types.MessageType_AppendTx: counter += 1 if res.Code != types.CodeType_OK { t.Error("AppendTx failed with ret_code", res.Code) } if counter > numAppendTxs { t.Fatal("Too many AppendTx responses") } t.Log("response", counter) if counter == numAppendTxs { go func() { time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow close(done) }() } case types.MessageType_Flush: // ignore default: t.Error("Unexpected response type", res.Type) } } }() // Write requests for counter := 0; counter < numAppendTxs; counter++ { // Send request var req = types.RequestAppendTx([]byte("test")) err := types.WriteMessage(req, conn) if err != nil { t.Fatal(err.Error()) } // Sometimes send flush messages if counter%123 == 0 { t.Log("flush") err := types.WriteMessage(types.RequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } } } // Send final flush message err = types.WriteMessage(types.RequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } <-done }