Ejemplo n.º 1
0
func timeoutRequest(port string) {
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	println("requestor: connected to", s.Addr())

	// Wrap the connection for slow writing to simulate a poor connection
	s.Adopt(&slowWriter{s.Conn(), 2 * time.Second})

	// Send a request -- it will take too long and time out
	sendRequest(s)

	s.Close()
}
Ejemplo n.º 2
0
func requestor(port string) {
	// Connect to our "responder"
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	println("connected to", s.Addr())

	// We use a single channel for receiving all responses
	reschan := make(chan gotalk.Response)

	// In this example all requests are the same
	req := gotalk.NewRequest("echo", []byte("hello"))
	sendRequest := func() {
		err := s.SendRequest(req, reschan)
		if err != nil {
			panic(err)
		}
	}

	// Send 10 requests at the same time
	for i := 0; i != 10; i++ {
		sendRequest()
	}

	// Read all responses
	for i := 0; i != 10; i++ {
		res := <-reschan
		if res.IsRetry() {
			fmt.Printf("requestor: retrying request in %v\n", res.Wait)
			if res.Wait == 0 {
				sendRequest()
			} else {
				go func(wait time.Duration) {
					time.Sleep(res.Wait)
					sendRequest()
				}(res.Wait)
			}
			i--
		} else if res.IsError() {
			panic(res)
		} else {
			fmt.Printf("requestor: received response: %q\n", string(res.Data))
		}
	}

	s.Close()
}
Ejemplo n.º 3
0
func client(port string) {
	gotalk.HandleBufferRequest("ping", func(_ *gotalk.Sock, _ string, b []byte) ([]byte, error) {
		fmt.Printf("client: handling 'ping' request: %q\n", string(b))
		return []byte("pong"), nil
	})

	// Connect to the server
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	fmt.Printf("client: connected to %s\n", s.Addr())

	// Send a notification as JSON
	fmt.Printf("client: sending 'msg' notification\n")
	if err := s.Notify("msg", struct{ Msg string }{"World"}); err != nil {
		fmt.Printf("client: notification: %v\n", err.Error())
	}

	// Send a notification as byte string
	fmt.Printf("client: sending 'msg' notification\n")
	if err := s.BufferNotify("msg", []byte("Hello")); err != nil {
		fmt.Printf("client: notification: error %v\n", err.Error())
	}

	// Send a request & read result via JSON-encoded go values
	fmt.Printf("client: sending 'greet' request\n")
	greeting := GreetOut{}
	if err := s.Request("greet", GreetIn{"Rasmus"}, &greeting); err != nil {
		fmt.Printf("client: greet: error %v\n", err.Error())
	} else {
		fmt.Printf("client: greet: %+v\n", greeting)
	}

	// Send a request & read result as byte strings
	fmt.Printf("client: sending 'echo' request\n")
	b, err := s.BufferRequest("echo", []byte("abc"))
	if err != nil {
		fmt.Printf("client: echo: error %v\n", err.Error())
	} else {
		fmt.Printf("client: echo: %v\n", string(b))
	}

	s.Close()
}
Ejemplo n.º 4
0
func heartbeatKeepAlive(port string) {
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		panic(err)
	}
	println("requestor: connected to", s.Addr())

	// As the responder has a one second timeout, set our heartbeat interval to half that time
	s.HeartbeatInterval = 500 * time.Millisecond

	// Sleep for 3 seconds
	time.Sleep(3 * time.Second)

	// Send a request, which will work since we have kept the connection alive with heartbeats
	sendRequest(s)

	s.Close()
}
Ejemplo n.º 5
0
func requestor(port string) {
	s, err := gotalk.Connect("tcp", "localhost:"+port)
	if err != nil {
		log.Fatalln(err)
	}
	println("requestor: connected to", s.Addr())

	// Send a request with a streaming payload
	req, res := s.StreamRequest("joke")
	if err := req.Write([]byte("tell me")); err != nil {
		log.Fatalln(err)
	}
	if err := req.Write([]byte(" a joke")); err != nil {
		log.Fatalln(err)
	}
	if err := req.Write([]byte(" or two")); err != nil {
		log.Fatalln(err)
	}
	if err := req.End(); err != nil {
		log.Fatalln(err)
	}

	// Read streaming result
	for {
		r := <-res
		if r.IsError() {
			log.Fatalln(r)
		}
		if r.Data == nil {
			break
		}
		fmt.Printf("requestor: received response payload: %q\n", string(r.Data))
		if !r.IsStreaming() || r.Data == nil {
			break
		}
	}

	s.Close()
}