func (s *rpcServer) accept(sock transport.Socket) { defer func() { if r := recover(); r != nil { log.Error(r, string(debug.Stack())) sock.Close() } }() var msg transport.Message if err := sock.Recv(&msg); err != nil { return } ct := msg.Header["Content-Type"] cf, err := s.newCodec(ct) // TODO: needs better error handling if err != nil { sock.Send(&transport.Message{ Header: map[string]string{ "Content-Type": "text/plain", }, Body: []byte(err.Error()), }) sock.Close() return } codec := newRpcPlusCodec(&msg, sock, cf) // strip our headers hdr := make(map[string]string) for k, v := range msg.Header { hdr[k] = v } delete(hdr, "Content-Type") ctx := c.WithMetadata(context.Background(), hdr) // TODO: needs better error handling if err := s.rpc.serveRequest(ctx, codec, ct); err != nil { log.Errorf("Unexpected error serving request, closing socket: %v", err) sock.Close() } }
// publishes a message func pub() { msg := client.NewPublication("topic.go.micro.srv.example", &example.Message{ Say: "This is a publication", }) // create context with metadata ctx := c.WithMetadata(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) // publish message if err := client.Publish(ctx, msg); err != nil { fmt.Println("pub err: ", err) return } fmt.Printf("Published: %v\n", msg) }
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", }) // create context with metadata ctx := c.WithMetadata(context.Background(), map[string]string{ "datacenter": "local", }) rsp := &example.Response{} // Call service if err := client.Call(ctx, req, rsp); err != nil { fmt.Println("call err: ", err, rsp) return } fmt.Println("Call:", i, "rsp:", rsp.Msg) }
func main() { cmd.Init() // use the generated client stub cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient) // Set arbitrary headers in context ctx := c.WithMetadata(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) rsp, err := cl.Hello(ctx, &hello.Request{ Name: "John", }) if err != nil { fmt.Println(err) return } fmt.Println(rsp.Msg) }
func main() { cmd.Init() // Create new request to service go.micro.srv.greeter, method Say.Hello req := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{ Name: "John", }) // Set arbitrary headers in context ctx := c.WithMetadata(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) rsp := &hello.Response{} // Call service if err := client.Call(ctx, req, rsp); err != nil { fmt.Println(err) return } fmt.Println(rsp.Msg) }
func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handler { return func(p broker.Publication) error { msg := p.Message() ct := msg.Header["Content-Type"] cf, err := s.newCodec(ct) if err != nil { return err } hdr := make(map[string]string) for k, v := range msg.Header { hdr[k] = v } delete(hdr, "Content-Type") ctx := c.WithMetadata(context.Background(), hdr) for i := 0; i < len(sb.handlers); i++ { handler := sb.handlers[i] var isVal bool var req reflect.Value if handler.reqType.Kind() == reflect.Ptr { req = reflect.New(handler.reqType.Elem()) } else { req = reflect.New(handler.reqType) isVal = true } if isVal { req = req.Elem() } b := &buffer{bytes.NewBuffer(msg.Body)} co := cf(b) defer co.Close() if err := co.ReadHeader(&codec.Message{}, codec.Publication); err != nil { return err } if err := co.ReadBody(req.Interface()); err != nil { return err } fn := func(ctx context.Context, msg Publication) error { var vals []reflect.Value if sb.typ.Kind() != reflect.Func { vals = append(vals, sb.rcvr) } if handler.ctxType != nil { vals = append(vals, reflect.ValueOf(ctx)) } vals = append(vals, reflect.ValueOf(msg.Message())) returnValues := handler.method.Call(vals) if err := returnValues[0].Interface(); err != nil { return err.(error) } return nil } for i := len(opts.SubWrappers); i > 0; i-- { fn = opts.SubWrappers[i-1](fn) } go fn(ctx, &rpcPublication{ topic: sb.topic, contentType: ct, message: req.Interface(), }) } return nil } }
func (t *traceWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { ctx = c.WithMetadata(ctx, map[string]string{ "X-Trace-Id": fmt.Sprintf("%d", time.Now().Unix()), }) return t.Client.Call(ctx, req, rsp) }
func (c *clientWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error { ctx = cx.WithMetadata(ctx, c.headers) return c.Client.Publish(ctx, p, opts...) }
func (c *clientWrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Streamer, error) { ctx = cx.WithMetadata(ctx, c.headers) return c.Client.Stream(ctx, req, opts...) }
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { ctx = cx.WithMetadata(ctx, c.headers) return c.Client.Call(ctx, req, rsp, opts...) }