Skip to content

Commit 10d0f0f

Browse files
authored
Merge pull request #98 from fangyincheng/imp
Imp: some improvements
2 parents 23f39b2 + 72f5336 commit 10d0f0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+666
-335
lines changed

common/constant/default.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const (
2626
DEFAULT_LOADBALANCE = "random"
2727
DEFAULT_RETRIES = 2
2828
DEFAULT_PROTOCOL = "dubbo"
29-
DEFAULT_VERSION = ""
3029
DEFAULT_REG_TIMEOUT = "10s"
3130
DEFAULT_CLUSTER = "failover"
3231
)
@@ -37,3 +36,7 @@ const (
3736
DEFAULT_REFERENCE_FILTERS = ""
3837
ECHO = "$echo"
3938
)
39+
40+
const (
41+
ANY_VALUE = "*"
42+
)

common/constant/key.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
WEIGHT_KEY = "weight"
4545
WARMUP_KEY = "warmup"
4646
RETRIES_KEY = "retries"
47+
BEAN_NAME = "bean.name"
4748
)
4849

4950
const (

common/logger/logger.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func init() {
5656
logConfFile := os.Getenv(constant.APP_LOG_CONF_FILE)
5757
err := InitLog(logConfFile)
5858
if err != nil {
59-
log.Printf("[InitLog] error: %v", err)
59+
log.Printf("[InitLog] warn: %v", err)
6060
}
6161
}
6262

@@ -110,7 +110,7 @@ func InitLogger(conf *zap.Config) {
110110
} else {
111111
zapLoggerConfig = *conf
112112
}
113-
zapLogger, _ := zapLoggerConfig.Build()
113+
zapLogger, _ := zapLoggerConfig.Build(zap.AddCallerSkip(1))
114114
logger = zapLogger.Sugar()
115115
}
116116

common/proxy/proxy.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package proxy
1919

2020
import (
2121
"reflect"
22+
"sync"
2223
)
2324

2425
import (
@@ -34,6 +35,8 @@ type Proxy struct {
3435
invoke protocol.Invoker
3536
callBack interface{}
3637
attachments map[string]string
38+
39+
once sync.Once
3740
}
3841

3942
var typError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()).Type()
@@ -78,23 +81,31 @@ func (p *Proxy) Implement(v common.RPCService) {
7881
methodName = "$echo"
7982
}
8083

81-
start := 0
82-
end := len(in)
83-
if in[0].Type().String() == "context.Context" {
84-
start += 1
85-
}
86-
if len(outs) == 1 {
87-
end -= 1
88-
reply = in[len(in)-1]
89-
} else {
84+
if len(outs) == 2 {
9085
if outs[0].Kind() == reflect.Ptr {
9186
reply = reflect.New(outs[0].Elem())
9287
} else {
9388
reply = reflect.New(outs[0])
9489
}
90+
} else {
91+
reply = valueOf
92+
}
93+
94+
start := 0
95+
end := len(in)
96+
if end > 0 {
97+
if in[0].Type().String() == "context.Context" {
98+
start += 1
99+
}
100+
if len(outs) == 1 && in[end-1].Type().Kind() == reflect.Ptr {
101+
end -= 1
102+
reply = in[len(in)-1]
103+
}
95104
}
96105

97-
if v, ok := in[start].Interface().([]interface{}); ok && end-start == 1 {
106+
if end-start <= 0 {
107+
inArr = []interface{}{}
108+
} else if v, ok := in[start].Interface().([]interface{}); ok && end-start == 1 {
98109
inArr = v
99110
} else {
100111
inArr = make([]interface{}, end-start)
@@ -134,7 +145,6 @@ func (p *Proxy) Implement(v common.RPCService) {
134145
}
135146
f := valueOfElem.Field(i)
136147
if f.Kind() == reflect.Func && f.IsValid() && f.CanSet() {
137-
inNum := t.Type.NumIn()
138148
outNum := t.Type.NumOut()
139149

140150
if outNum != 1 && outNum != 2 {
@@ -149,12 +159,6 @@ func (p *Proxy) Implement(v common.RPCService) {
149159
continue
150160
}
151161

152-
// reply must be Ptr when outNum == 1
153-
if outNum == 1 && t.Type.In(inNum-1).Kind() != reflect.Ptr {
154-
logger.Warnf("reply type of method %q is not a pointer", t.Name)
155-
continue
156-
}
157-
158162
var funcOuts = make([]reflect.Type, outNum)
159163
for i := 0; i < outNum; i++ {
160164
funcOuts[i] = t.Type.Out(i)
@@ -166,7 +170,9 @@ func (p *Proxy) Implement(v common.RPCService) {
166170
}
167171
}
168172

169-
p.rpc = v
173+
p.once.Do(func() {
174+
p.rpc = v
175+
})
170176

171177
}
172178

common/proxy/proxy_factory/default.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ func (factory *DefaultProxyFactory) GetProxy(invoker protocol.Invoker, url *comm
5151
return proxy.NewProxy(invoker, nil, attachments)
5252
}
5353
func (factory *DefaultProxyFactory) GetInvoker(url common.URL) protocol.Invoker {
54-
//TODO:yincheng need to do the service invoker refactor
54+
// todo: call service
5555
return protocol.NewBaseInvoker(url)
5656
}

common/proxy/proxy_factory/default_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ import (
2121
"testing"
2222
)
2323

24+
import (
25+
"github.com/stretchr/testify/assert"
26+
)
27+
2428
import (
2529
"github.com/apache/dubbo-go/common"
2630
"github.com/apache/dubbo-go/protocol"
27-
"github.com/stretchr/testify/assert"
2831
)
2932

3033
func Test_GetProxy(t *testing.T) {
3134
proxyFactory := NewDefaultProxyFactory()
32-
url := common.NewURLWithOptions("testservice")
35+
url := common.NewURLWithOptions()
3336
proxy := proxyFactory.GetProxy(protocol.NewBaseInvoker(*url), url)
3437
assert.NotNil(t, proxy)
3538
}
3639

3740
func Test_GetInvoker(t *testing.T) {
3841
proxyFactory := NewDefaultProxyFactory()
39-
url := common.NewURLWithOptions("testservice")
42+
url := common.NewURLWithOptions()
4043
invoker := proxyFactory.GetInvoker(*url)
4144
assert.True(t, invoker.IsAvailable())
4245
}

common/proxy/proxy_test.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import (
3636

3737
type TestService struct {
3838
MethodOne func(context.Context, int, bool, *interface{}) error
39-
MethodTwo func([]interface{}, *interface{}) error
39+
MethodTwo func([]interface{}) error
4040
MethodThree func(int, bool) (interface{}, error)
4141
MethodFour func(int, bool) (*interface{}, error) `dubbo:"methodFour"`
42+
MethodFive func() error
4243
Echo func(interface{}, *interface{}) error
4344
}
4445

@@ -64,19 +65,25 @@ func TestProxy_Implement(t *testing.T) {
6465
p := NewProxy(invoker, nil, map[string]string{constant.ASYNC_KEY: "false"})
6566
s := &TestService{}
6667
p.Implement(s)
68+
6769
err := p.Get().(*TestService).MethodOne(nil, 0, false, nil)
6870
assert.NoError(t, err)
69-
err = p.Get().(*TestService).MethodTwo(nil, nil)
71+
72+
err = p.Get().(*TestService).MethodTwo(nil)
7073
assert.NoError(t, err)
7174
ret, err := p.Get().(*TestService).MethodThree(0, false)
7275
assert.NoError(t, err)
7376
assert.Nil(t, ret) // ret is nil, because it doesn't be injection yet
77+
7478
ret2, err := p.Get().(*TestService).MethodFour(0, false)
7579
assert.NoError(t, err)
7680
assert.Equal(t, "*interface {}", reflect.TypeOf(ret2).String())
7781
err = p.Get().(*TestService).Echo(nil, nil)
7882
assert.NoError(t, err)
7983

84+
err = p.Get().(*TestService).MethodFive()
85+
assert.NoError(t, err)
86+
8087
// inherit & lowercase
8188
p.rpc = nil
8289
type S1 struct {
@@ -108,24 +115,14 @@ func TestProxy_Implement(t *testing.T) {
108115
p.Implement(s2)
109116
assert.Nil(t, s2.MethodOne)
110117

111-
// reply type
118+
// returns type
112119
p.rpc = nil
113120
type S3 struct {
114121
TestService
115-
MethodOne func(context.Context, []interface{}, struct{}) error
122+
MethodOne func(context.Context, []interface{}, *struct{}) interface{}
116123
}
117124
s3 := &S3{TestService: *s}
118125
p.Implement(s3)
119126
assert.Nil(t, s3.MethodOne)
120127

121-
// returns type
122-
p.rpc = nil
123-
type S4 struct {
124-
TestService
125-
MethodOne func(context.Context, []interface{}, *struct{}) interface{}
126-
}
127-
s4 := &S4{TestService: *s}
128-
p.Implement(s4)
129-
assert.Nil(t, s4.MethodOne)
130-
131128
}

common/rpc_service.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ type RPCService interface {
4040
Version() string
4141
}
4242

43+
// for lowercase func
44+
// func MethodMapper() map[string][string] {
45+
// return map[string][string]{}
46+
// }
47+
const METHOD_MAPPER = "MethodMapper"
48+
4349
var (
4450
// Precompute the reflect type for error. Can't use error directly
4551
// because Typeof takes an empty interface value. This is annoying.
@@ -210,20 +216,26 @@ func isExportedOrBuiltinType(t reflect.Type) bool {
210216
// suitableMethods returns suitable Rpc methods of typ
211217
func suitableMethods(typ reflect.Type) (string, map[string]*MethodType) {
212218
methods := make(map[string]*MethodType)
213-
mts := ""
219+
var mts []string
214220
logger.Debugf("[%s] NumMethod is %d", typ.String(), typ.NumMethod())
221+
method, ok := typ.MethodByName(METHOD_MAPPER)
222+
var methodMapper map[string]string
223+
if ok && method.Type.NumIn() == 1 && method.Type.NumOut() == 1 && method.Type.Out(0).String() == "map[string]string" {
224+
methodMapper = method.Func.Call([]reflect.Value{reflect.New(typ.Elem())})[0].Interface().(map[string]string)
225+
}
226+
215227
for m := 0; m < typ.NumMethod(); m++ {
216-
method := typ.Method(m)
228+
method = typ.Method(m)
217229
if mt := suiteMethod(method); mt != nil {
218-
methods[method.Name] = mt
219-
if m == 0 {
220-
mts += method.Name
221-
} else {
222-
mts += "," + method.Name
230+
methodName, ok := methodMapper[method.Name]
231+
if !ok {
232+
methodName = method.Name
223233
}
234+
methods[methodName] = mt
235+
mts = append(mts, methodName)
224236
}
225237
}
226-
return mts, methods
238+
return strings.Join(mts, ","), methods
227239
}
228240

229241
// suiteMethod returns a suitable Rpc methodType
@@ -256,12 +268,7 @@ func suiteMethod(method reflect.Method) *MethodType {
256268
}
257269

258270
// replyType
259-
if outNum == 1 {
260-
if mtype.In(inNum-1).Kind() != reflect.Ptr {
261-
logger.Errorf("reply type of method %q is not a pointer %v", mname, replyType)
262-
return nil
263-
}
264-
} else {
271+
if outNum == 2 {
265272
replyType = mtype.Out(0)
266273
if !isExportedOrBuiltinType(replyType) {
267274
logger.Errorf("reply type of method %s not exported{%v}", mname, replyType)
@@ -272,7 +279,7 @@ func suiteMethod(method reflect.Method) *MethodType {
272279
index := 1
273280

274281
// ctxType
275-
if mtype.In(1).String() == "context.Context" {
282+
if inNum > 1 && mtype.In(1).String() == "context.Context" {
276283
ctxType = mtype.In(1)
277284
index = 2
278285
}

0 commit comments

Comments
 (0)