diff --git a/gee-rpc/day3-service/service.go b/gee-rpc/day3-service/service.go index e0711cd..306683c 100644 --- a/gee-rpc/day3-service/service.go +++ b/gee-rpc/day3-service/service.go @@ -8,10 +8,14 @@ import ( ) type methodType struct { - Method reflect.Method + method reflect.Method ArgType reflect.Type ReplyType reflect.Type - NumCalls uint64 + numCalls uint64 +} + +func (m *methodType) NumCalls() uint64 { + return atomic.LoadUint64(&m.numCalls) } func (m *methodType) newArgv() reflect.Value { @@ -72,7 +76,7 @@ func (s *service) registerMethods() { continue } s.method[method.Name] = &methodType{ - Method: method, + method: method, ArgType: argType, ReplyType: replyType, } @@ -81,8 +85,8 @@ func (s *service) registerMethods() { } func (s *service) call(m *methodType, argv, replyv reflect.Value) error { - atomic.AddUint64(&m.NumCalls, 1) - f := m.Method.Func + atomic.AddUint64(&m.numCalls, 1) + f := m.method.Func returnValues := f.Call([]reflect.Value{s.rcvr, argv, replyv}) if errInter := returnValues[0].Interface(); errInter != nil { return errInter.(error) diff --git a/gee-rpc/day3-service/service_test.go b/gee-rpc/day3-service/service_test.go index 4b35124..c8266df 100644 --- a/gee-rpc/day3-service/service_test.go +++ b/gee-rpc/day3-service/service_test.go @@ -15,7 +15,7 @@ func (f Foo) Sum(args Args, reply *int) error { return nil } -// it's not a exported method +// it's not a exported Method func (f Foo) sum(args Args, reply *int) error { *reply = args.Num1 + args.Num2 return nil @@ -30,9 +30,9 @@ func _assert(condition bool, msg string, v ...interface{}) { func TestNewService(t *testing.T) { var foo Foo s := newService(&foo) - _assert(len(s.method) == 1, "wrong service method, expect 1, but got %d", len(s.method)) + _assert(len(s.method) == 1, "wrong service Method, expect 1, but got %d", len(s.method)) mType := s.method["Sum"] - _assert(mType != nil, "wrong method, Sum shouldn't nil") + _assert(mType != nil, "wrong Method, Sum shouldn't nil") } func TestMethodType_Call(t *testing.T) { @@ -44,5 +44,5 @@ func TestMethodType_Call(t *testing.T) { replyv := mType.newReplyv() argv.Set(reflect.ValueOf(Args{Num1: 1, Num2: 3})) err := s.call(mType, argv, replyv) - _assert(err == nil && *replyv.Interface().(*int) == 4 && mType.numCalls == 1, "failed to call Foo.Sum") + _assert(err == nil && *replyv.Interface().(*int) == 4 && mType.NumCalls() == 1, "failed to call Foo.Sum") } diff --git a/gee-rpc/day4-http-debug/service.go b/gee-rpc/day4-http-debug/service.go index e0711cd..306683c 100644 --- a/gee-rpc/day4-http-debug/service.go +++ b/gee-rpc/day4-http-debug/service.go @@ -8,10 +8,14 @@ import ( ) type methodType struct { - Method reflect.Method + method reflect.Method ArgType reflect.Type ReplyType reflect.Type - NumCalls uint64 + numCalls uint64 +} + +func (m *methodType) NumCalls() uint64 { + return atomic.LoadUint64(&m.numCalls) } func (m *methodType) newArgv() reflect.Value { @@ -72,7 +76,7 @@ func (s *service) registerMethods() { continue } s.method[method.Name] = &methodType{ - Method: method, + method: method, ArgType: argType, ReplyType: replyType, } @@ -81,8 +85,8 @@ func (s *service) registerMethods() { } func (s *service) call(m *methodType, argv, replyv reflect.Value) error { - atomic.AddUint64(&m.NumCalls, 1) - f := m.Method.Func + atomic.AddUint64(&m.numCalls, 1) + f := m.method.Func returnValues := f.Call([]reflect.Value{s.rcvr, argv, replyv}) if errInter := returnValues[0].Interface(); errInter != nil { return errInter.(error) diff --git a/gee-rpc/day4-http-debug/service_test.go b/gee-rpc/day4-http-debug/service_test.go index 7ba2786..c8266df 100644 --- a/gee-rpc/day4-http-debug/service_test.go +++ b/gee-rpc/day4-http-debug/service_test.go @@ -44,5 +44,5 @@ func TestMethodType_Call(t *testing.T) { replyv := mType.newReplyv() argv.Set(reflect.ValueOf(Args{Num1: 1, Num2: 3})) err := s.call(mType, argv, replyv) - _assert(err == nil && *replyv.Interface().(*int) == 4 && mType.NumCalls == 1, "failed to call Foo.Sum") + _assert(err == nil && *replyv.Interface().(*int) == 4 && mType.NumCalls() == 1, "failed to call Foo.Sum") }