Skip to content

Commit

Permalink
fix service numCalls read issue
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Oct 2, 2020
1 parent 22b58fc commit b9f5ca1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
14 changes: 9 additions & 5 deletions gee-rpc/day3-service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -72,7 +76,7 @@ func (s *service) registerMethods() {
continue
}
s.method[method.Name] = &methodType{
Method: method,
method: method,
ArgType: argType,
ReplyType: replyType,
}
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions gee-rpc/day3-service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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")
}
14 changes: 9 additions & 5 deletions gee-rpc/day4-http-debug/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -72,7 +76,7 @@ func (s *service) registerMethods() {
continue
}
s.method[method.Name] = &methodType{
Method: method,
method: method,
ArgType: argType,
ReplyType: replyType,
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gee-rpc/day4-http-debug/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b9f5ca1

Please sign in to comment.