Beispiel #1
0
func createSubHandler(sb *subscriber) broker.Handler {
	return func(msg *broker.Message) {
		hdr := make(map[string]string)
		for k, v := range msg.Header {
			hdr[k] = v
		}
		delete(hdr, "Content-Type")
		ctx := c.WithMetadata(context.Background(), hdr)
		rctx := reflect.ValueOf(ctx)

		for _, handler := range sb.handlers {
			var isVal bool
			var req reflect.Value
			var uerr error

			if handler.reqType.Kind() == reflect.Ptr {
				req = reflect.New(handler.reqType.Elem())
			} else {
				req = reflect.New(handler.reqType)
				isVal = true
			}

			switch msg.Header["Content-Type"] {
			case "application/octet-stream":
				uerr = proto.Unmarshal(msg.Body, req.Interface().(proto.Message))
			case "application/json":
				uerr = json.Unmarshal(msg.Body, req.Interface())
			}

			if uerr != nil {
				continue
			}

			if isVal {
				req = req.Elem()
			}

			var vals []reflect.Value
			if sb.typ.Kind() != reflect.Func {
				vals = append(vals, sb.rcvr)
			}

			if handler.ctxType != nil {
				vals = append(vals, rctx)
			}

			vals = append(vals, req)
			go handler.method.Call(vals)
		}
	}
}
Beispiel #2
0
func (s *rpcServer) accept(sock transport.Socket) {
	var msg transport.Message
	if err := sock.Recv(&msg); err != nil {
		return
	}

	codec := newRpcPlusCodec(&msg, sock)

	// 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)
	s.rpc.ServeRequestWithContext(ctx, codec)
}
Beispiel #3
0
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)
}
Beispiel #4
0
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{
		"X-User-Id": "john",
		"X-From-Id": "script",
	})

	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)
}
Beispiel #5
0
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)
}