From ffbd7c8595858b7bd4d570694abba594dd319ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Images=E3=80=82?= Date: Tue, 25 May 2021 11:28:03 +0800 Subject: [PATCH 1/2] Update geerpc-day1.md --- gee-rpc/doc/geerpc-day1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gee-rpc/doc/geerpc-day1.md b/gee-rpc/doc/geerpc-day1.md index 4e0c22a..76df010 100644 --- a/gee-rpc/doc/geerpc-day1.md +++ b/gee-rpc/doc/geerpc-day1.md @@ -183,7 +183,7 @@ var DefaultOption = &Option{ } ``` -一般来说,涉及协议协商的这部分信息,需要设计固定的字节来传输的。但是为了实现上更简单,GeeRPC 客户端固定采用 JSON 编码 Option,后续的 header 和 body 的编码方式由 Option 中的 CodeType 指定,服务端首先使用 JSON 解码 Option,然后通过 Option 得 CodeType 解码剩余的内容。即报文将以这样的形式发送: +一般来说,涉及协议协商的这部分信息,需要设计固定的字节来传输的。但是为了实现上更简单,GeeRPC 客户端固定采用 JSON 编码 Option,后续的 header 和 body 的编码方式由 Option 中的 CodeType 指定,服务端首先使用 JSON 解码 Option,然后通过 Option 的 CodeType 解码剩余的内容。即报文将以这样的形式发送: ```bash | Option{MagicNumber: xxx, CodecType: xxx} | Header{ServiceMethod ...} | Body interface{} | From 3d9516505fa80c9f1b5f32e8c6af7e9da4064f70 Mon Sep 17 00:00:00 2001 From: wubbalubbaaa <543668641@qq.com> Date: Thu, 17 Feb 2022 20:25:10 +0800 Subject: [PATCH 2/2] fix: fix reply arg check fix: fix reply arg check --- gee-rpc/day3-service/service.go | 7 ++++++- gee-rpc/day4-timeout/service.go | 7 ++++++- gee-rpc/day5-http-debug/service.go | 7 ++++++- gee-rpc/day6-load-balance/service.go | 7 ++++++- gee-rpc/day7-registry/service.go | 7 ++++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/gee-rpc/day3-service/service.go b/gee-rpc/day3-service/service.go index 306683c..971ce8a 100644 --- a/gee-rpc/day3-service/service.go +++ b/gee-rpc/day3-service/service.go @@ -72,7 +72,7 @@ func (s *service) registerMethods() { continue } argType, replyType := mType.In(1), mType.In(2) - if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) { + if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) || !isPtrType(replyType) { continue } s.method[method.Name] = &methodType{ @@ -95,5 +95,10 @@ func (s *service) call(m *methodType, argv, replyv reflect.Value) error { } func isExportedOrBuiltinType(t reflect.Type) bool { + return ast.IsExported(t.Name()) || t.PkgPath() == "" } + +func isPtrType(t reflect.Type) bool { + return t.Kind() == reflect.Ptr +} diff --git a/gee-rpc/day4-timeout/service.go b/gee-rpc/day4-timeout/service.go index 306683c..971ce8a 100644 --- a/gee-rpc/day4-timeout/service.go +++ b/gee-rpc/day4-timeout/service.go @@ -72,7 +72,7 @@ func (s *service) registerMethods() { continue } argType, replyType := mType.In(1), mType.In(2) - if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) { + if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) || !isPtrType(replyType) { continue } s.method[method.Name] = &methodType{ @@ -95,5 +95,10 @@ func (s *service) call(m *methodType, argv, replyv reflect.Value) error { } func isExportedOrBuiltinType(t reflect.Type) bool { + return ast.IsExported(t.Name()) || t.PkgPath() == "" } + +func isPtrType(t reflect.Type) bool { + return t.Kind() == reflect.Ptr +} diff --git a/gee-rpc/day5-http-debug/service.go b/gee-rpc/day5-http-debug/service.go index 306683c..971ce8a 100644 --- a/gee-rpc/day5-http-debug/service.go +++ b/gee-rpc/day5-http-debug/service.go @@ -72,7 +72,7 @@ func (s *service) registerMethods() { continue } argType, replyType := mType.In(1), mType.In(2) - if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) { + if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) || !isPtrType(replyType) { continue } s.method[method.Name] = &methodType{ @@ -95,5 +95,10 @@ func (s *service) call(m *methodType, argv, replyv reflect.Value) error { } func isExportedOrBuiltinType(t reflect.Type) bool { + return ast.IsExported(t.Name()) || t.PkgPath() == "" } + +func isPtrType(t reflect.Type) bool { + return t.Kind() == reflect.Ptr +} diff --git a/gee-rpc/day6-load-balance/service.go b/gee-rpc/day6-load-balance/service.go index 306683c..971ce8a 100644 --- a/gee-rpc/day6-load-balance/service.go +++ b/gee-rpc/day6-load-balance/service.go @@ -72,7 +72,7 @@ func (s *service) registerMethods() { continue } argType, replyType := mType.In(1), mType.In(2) - if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) { + if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) || !isPtrType(replyType) { continue } s.method[method.Name] = &methodType{ @@ -95,5 +95,10 @@ func (s *service) call(m *methodType, argv, replyv reflect.Value) error { } func isExportedOrBuiltinType(t reflect.Type) bool { + return ast.IsExported(t.Name()) || t.PkgPath() == "" } + +func isPtrType(t reflect.Type) bool { + return t.Kind() == reflect.Ptr +} diff --git a/gee-rpc/day7-registry/service.go b/gee-rpc/day7-registry/service.go index 306683c..971ce8a 100644 --- a/gee-rpc/day7-registry/service.go +++ b/gee-rpc/day7-registry/service.go @@ -72,7 +72,7 @@ func (s *service) registerMethods() { continue } argType, replyType := mType.In(1), mType.In(2) - if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) { + if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) || !isPtrType(replyType) { continue } s.method[method.Name] = &methodType{ @@ -95,5 +95,10 @@ func (s *service) call(m *methodType, argv, replyv reflect.Value) error { } func isExportedOrBuiltinType(t reflect.Type) bool { + return ast.IsExported(t.Name()) || t.PkgPath() == "" } + +func isPtrType(t reflect.Type) bool { + return t.Kind() == reflect.Ptr +}