add support for streaming requests. cleanup watcher initilisation

This commit is contained in:
Asim
2015-06-01 18:55:27 +01:00
parent fa2c27b64f
commit 09c784d294
25 changed files with 729 additions and 384 deletions

View File

@@ -10,9 +10,7 @@ import (
"golang.org/x/net/context"
)
func main() {
cmd.Init()
func call(i int) {
// Create new request to service go.micro.srv.example, method Example.Call
req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{
Name: "John",
@@ -28,9 +26,45 @@ func main() {
// Call service
if err := client.Call(ctx, req, rsp); err != nil {
fmt.Println(err)
fmt.Println("err: ", err, rsp)
return
}
fmt.Println(rsp.Msg)
fmt.Println("Call:", i, "rsp:", rsp.Msg)
}
func stream() {
// Create new request to service go.micro.srv.example, method Example.Call
req := client.NewRequest("go.micro.srv.example", "Example.Stream", &example.StreamingRequest{
Count: int64(10),
})
rspChan := make(chan *example.StreamingResponse, 10)
stream, err := client.Stream(context.Background(), req, rspChan)
if err != nil {
fmt.Println("err:", err)
return
}
for rsp := range rspChan {
fmt.Println("Stream: rsp:", rsp.Count)
}
if stream.Error() != nil {
fmt.Println("err:", err)
return
}
stream.Close()
}
func main() {
cmd.Init()
for i := 0; i < 10; i++ {
call(i)
}
stream()
}

View File

@@ -13,7 +13,24 @@ type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
md, _ := c.GetMetadata(ctx)
log.Info("Received Example.Call request with metadata: %v", md)
log.Infof("Received Example.Call request with metadata: %v", md)
rsp.Msg = server.Config().Id() + ": Hello " + req.Name
return nil
}
func (e *Example) Stream(ctx context.Context, req *example.StreamingRequest, response func(interface{}) error) error {
log.Infof("Received Example.Stream request with count: %d", req.Count)
for i := 0; i < int(req.Count); i++ {
log.Infof("Responding: %d", i)
r := &example.StreamingResponse{
Count: int64(i),
}
if err := response(r); err != nil {
return err
}
}
return nil
}

View File

@@ -11,6 +11,8 @@ It is generated from these files:
It has these top-level messages:
Request
Response
StreamingRequest
StreamingResponse
*/
package example
@@ -35,5 +37,21 @@ func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
type StreamingRequest struct {
Count int64 `protobuf:"varint,1,opt,name=count" json:"count,omitempty"`
}
func (m *StreamingRequest) Reset() { *m = StreamingRequest{} }
func (m *StreamingRequest) String() string { return proto.CompactTextString(m) }
func (*StreamingRequest) ProtoMessage() {}
type StreamingResponse struct {
Count int64 `protobuf:"varint,1,opt,name=count" json:"count,omitempty"`
}
func (m *StreamingResponse) Reset() { *m = StreamingResponse{} }
func (m *StreamingResponse) String() string { return proto.CompactTextString(m) }
func (*StreamingResponse) ProtoMessage() {}
func init() {
}

View File

@@ -7,3 +7,11 @@ message Request {
message Response {
string msg = 1;
}
message StreamingRequest {
int64 count = 1;
}
message StreamingResponse {
int64 count = 1;
}