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.ToRequestFlush(), 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 _, ok := resFlush.Value.(*types.Response_Flush); !ok { return nil, errors.New(Fmt("Expected flush response but got something else: %v", reflect.TypeOf(resFlush))) } 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 *socketClient) sendRequestsRoutine(conn net.Conn) { w := bufio.NewWriter(conn) for { select { case <-cli.flushTimer.Ch: select { case cli.reqQueue <- NewReqRes(types.ToRequestFlush()): 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, w) if err != nil { cli.StopForError(fmt.Errorf("Error writing msg: %v", err)) return } // log.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request) if _, ok := reqres.Request.Value.(*types.Request_Flush); ok { err = w.Flush() if err != nil { cli.StopForError(fmt.Errorf("Error flushing writer: %v", err)) return } } } } }
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 main() { conn, err := Connect("unix://test.sock") if err != nil { Exit(err.Error()) } // Read a bunch of responses go func() { counter := 0 for { var res = &types.Response{} err := types.ReadMessage(conn, res) if err != nil { Exit(err.Error()) } counter += 1 if counter%1000 == 0 { fmt.Println("Read", counter) } } }() // Write a bunch of requests counter := 0 for i := 0; ; i++ { var bufWriter = bufio.NewWriter(conn) var req = types.RequestEcho("foobar") err := types.WriteMessage(req, bufWriter) if err != nil { Exit(err.Error()) } err = bufWriter.Flush() if err != nil { Exit(err.Error()) } counter += 1 if counter%1000 == 0 { fmt.Println("Write", counter) } } }
// Pull responses from 'responses' and write them to conn. func (s *SocketServer) handleResponses(closeConn chan error, responses <-chan *types.Response, conn net.Conn) { var count int var bufWriter = bufio.NewWriter(conn) for { var res = <-responses err := types.WriteMessage(res, bufWriter) if err != nil { closeConn <- fmt.Errorf("Error writing message: %v", err.Error()) return } if _, ok := res.Value.(*types.Response_Flush); ok { err = bufWriter.Flush() if err != nil { closeConn <- fmt.Errorf("Error flushing write buffer: %v", err.Error()) return } } count++ } }
// Pull responses from 'responses' and write them to conn. func handleResponses(closeConn chan error, responses <-chan *types.Response, conn net.Conn) { var count int var bufWriter = bufio.NewWriter(conn) for { var res = <-responses err := types.WriteMessage(res, bufWriter) if err != nil { closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error()) return } if res.Type == types.MessageType_Flush { err = bufWriter.Flush() if err != nil { closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error()) return } } count++ } }
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 }