Commit ab6602a6 authored by AlexStocks's avatar AlexStocks Committed by Xin.Zh

add apache/dubbo-go common

parent d8e377a0
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxconstant
// nolint
const (
FAILOVER_CLUSTER_NAME = "failover"
ZONEAWARE_CLUSTER_NAME = "zoneAware"
)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxconstant
const (
DUBBO = "dubbo"
PROVIDER_PROTOCOL = "provider"
// compatible with 2.6.x
OVERRIDE_PROTOCOL = "override"
EMPTY_PROTOCOL = "empty"
ROUTER_PROTOCOL = "router"
)
const (
DEFAULT_WEIGHT = 100 //
DEFAULT_WARMUP = 10 * 60 // in java here is 10*60*1000 because of System.currentTimeMillis() is measured in milliseconds & in go time.Unix() is second
)
const (
DEFAULT_LOADBALANCE = "random"
DEFAULT_RETRIES = "2"
DEFAULT_RETRIES_INT = 2
DEFAULT_PROTOCOL = "dubbo"
DEFAULT_REG_TIMEOUT = "10s"
DEFAULT_REG_TTL = "15m"
DEFAULT_CLUSTER = "failover"
DEFAULT_FAILBACK_TIMES = "3"
DEFAULT_FAILBACK_TIMES_INT = 3
DEFAULT_FAILBACK_TASKS = 100
DEFAULT_REST_CLIENT = "resty"
DEFAULT_REST_SERVER = "go-restful"
DEFAULT_PORT = 20000
DEFAULT_SERIALIZATION = HESSIAN2_SERIALIZATION
)
const (
DEFAULT_KEY = "default"
PREFIX_DEFAULT_KEY = "default."
DEFAULT_SERVICE_FILTERS = "echo,token,accesslog,tps,generic_service,execute,pshutdown"
DEFAULT_REFERENCE_FILTERS = "cshutdown"
GENERIC_REFERENCE_FILTERS = "generic"
GENERIC = "$invoke"
ECHO = "$echo"
)
const (
ANY_VALUE = "*"
ANYHOST_VALUE = "0.0.0.0"
LOCAL_HOST_VALUE = "192.168.1.1"
REMOVE_VALUE_PREFIX = "-"
)
const (
CONFIGURATORS_CATEGORY = "configurators"
ROUTER_CATEGORY = "category"
DEFAULT_CATEGORY = PROVIDER_CATEGORY
DYNAMIC_CONFIGURATORS_CATEGORY = "dynamicconfigurators"
APP_DYNAMIC_CONFIGURATORS_CATEGORY = "appdynamicconfigurators"
PROVIDER_CATEGORY = "providers"
CONSUMER_CATEGORY = "consumers"
)
const (
COMMA_SPLIT_PATTERN = "\\s*[,]+\\s*"
)
const (
SIMPLE_METADATA_SERVICE_NAME = "MetadataService"
DEFAULT_REVISION = "N/A"
)
const (
SERVICE_DISCOVERY_DEFAULT_GROUP = "DEFAULT_GROUP"
)
const (
DEFAULT_PROVIDER_CONF_FILE_PATH = "../profiles/dev/server.yml"
DEFAULT_CONSUMER_CONF_FILE_PATH = "../profiles/dev/client.yml"
DEFAULT_LOG_CONF_FILE_PATH = "../profiles/dev/log.yml"
DEFAULT_ROUTER_CONF_FILE_PATH = "../profiles/dev/router.yml"
)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxconstant
// nolint
const (
// CONF_CONSUMER_FILE_PATH ...
CONF_CONSUMER_FILE_PATH = "CONF_CONSUMER_FILE_PATH"
// CONF_PROVIDER_FILE_PATH ...
CONF_PROVIDER_FILE_PATH = "CONF_PROVIDER_FILE_PATH"
// APP_LOG_CONF_FILE ...
APP_LOG_CONF_FILE = "APP_LOG_CONF_FILE"
// CONF_ROUTER_FILE_PATH Specify Path variable of router config file
CONF_ROUTER_FILE_PATH = "CONF_ROUTER_FILE_PATH"
// CONF_VIRTUAL_SERVICE_FILE_PATH Specify path to Virtual service of uniform router config file
CONF_VIRTUAL_SERVICE_FILE_PATH = "CONF_VIRTUAL_SERVICE_FILE_PATH"
// CONF_DEST_RULE_FILE_PATH Specify path to destination rule of uniform router config file
CONF_DEST_RULE_FILE_PATH = "CONF_DEST_RULE_FILE_PATH"
)
This diff is collapsed.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxconstant
const (
S_Hessian2 byte = 2
S_Proto byte = 21
)
const (
HESSIAN2_SERIALIZATION = "hessian2"
PROTOBUF_SERIALIZATION = "protobuf"
)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxconstant
import (
"time"
)
// The value will be 10^6
// 1ms = 10^6ns
var MsToNanoRate = int64(time.Millisecond / time.Nanosecond)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// same as https://github.com/golang/go/blob/master/src/go
// refer from github.com/apache/dubbo-go common
package gxdubbogo
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxdubbogo
import gxnet "github.com/dubbogo/gost/net"
var localIp string
func GetLocalIp() string {
if len(localIp) != 0 {
return localIp
}
localIp, _ = gxnet.GetLocalIP()
return localIp
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxdubbogo
import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestGetLocalIp(t *testing.T) {
assert.NotNil(t, GetLocalIp())
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxdubbogo
// Node use for process dubbo node
type Node interface {
GetURL() *URL
IsAvailable() bool
Destroy()
}
This diff is collapsed.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxdubbogo
import (
"context"
"reflect"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
import (
constant "github.com/dubbogo/gost/dubbogo/constant"
)
const (
referenceTestPath = "com.test.Path"
referenceTestPathDistinct = "com.test.Path1"
testInterfaceName = "testService"
testProtocol = "testprotocol"
testSuiteMethodExpectedString = "interface {}"
)
type TestService struct{}
func (s *TestService) MethodOne(ctx context.Context, arg1, arg2, arg3 interface{}) error {
return nil
}
func (s *TestService) MethodTwo(arg1, arg2, arg3 interface{}) (interface{}, error) {
return struct{}{}, nil
}
func (s *TestService) MethodThree() error {
return nil
}
func (s *TestService) Reference() string {
return referenceTestPath
}
func (s *TestService) MethodMapper() map[string]string {
return map[string]string{
"MethodTwo": "methodTwo",
}
}
type testService struct{}
func (s *testService) Method1(ctx context.Context, args testService, rsp *struct{}) error {
return nil
}
func (s *testService) Method2(ctx context.Context, args []interface{}) (testService, error) {
return testService{}, nil
}
func (s *testService) Method3(ctx context.Context, args []interface{}, rsp *struct{}) {
}
func (s *testService) Method4(ctx context.Context, args []interface{}, rsp *struct{}) *testService {
return nil
}
func (s *testService) Reference() string {
return referenceTestPath
}
type TestService1 struct{}
func (s *TestService1) Reference() string {
return referenceTestPathDistinct
}
func TestServiceMapRegister(t *testing.T) {
// lowercase
s0 := &testService{}
// methods, err := ServiceMap.Register("testporotocol", s0)
_, err := ServiceMap.Register(testInterfaceName, "testporotocol", "", "v0", s0)
assert.EqualError(t, err, "type testService is not exported")
// succ
s := &TestService{}
methods, err := ServiceMap.Register(testInterfaceName, "testporotocol", "", "v1", s)
assert.NoError(t, err)
assert.Equal(t, "MethodOne,MethodThree,methodTwo", methods)
// repeat
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", "", "v1", s)
assert.EqualError(t, err, "service already defined: testService:v1")
// no method
s1 := &TestService1{}
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", "", "v2", s1)
assert.EqualError(t, err, "type testService:v2 has no exported methods of suitable type")
ServiceMap = &serviceMap{
serviceMap: make(map[string]map[string]*Service),
interfaceMap: make(map[string][]*Service),
}
}
func TestServiceMapUnRegister(t *testing.T) {
s := &TestService{}
_, err := ServiceMap.Register("TestService", testProtocol, "", "v1", s)
assert.NoError(t, err)
assert.NotNil(t, ServiceMap.GetService(testProtocol, "TestService", "", "v1"))
assert.Equal(t, 1, len(ServiceMap.GetInterface("TestService")))
err = ServiceMap.UnRegister("", "", ServiceKey("TestService", "", "v1"))
assert.EqualError(t, err, "protocol or serviceKey is nil")
err = ServiceMap.UnRegister("", "protocol", ServiceKey("TestService", "", "v1"))
assert.EqualError(t, err, "no services for protocol")
err = ServiceMap.UnRegister("", testProtocol, ServiceKey("TestService", "", "v0"))
assert.EqualError(t, err, "no service for TestService:v0")
// success
err = ServiceMap.UnRegister("TestService", testProtocol, ServiceKey("TestService", "", "v1"))
assert.NoError(t, err)
}
func TestMethodTypeSuiteContext(t *testing.T) {
mt := &MethodType{ctxType: reflect.TypeOf(context.TODO())}
ctx := context.Background()
key := constant.DubboCtxKey("key")
ctx = context.WithValue(ctx, key, "value")
assert.Equal(t, reflect.ValueOf(ctx), mt.SuiteContext(ctx))
}
func TestSuiteMethod(t *testing.T) {
s := &TestService{}
method, ok := reflect.TypeOf(s).MethodByName("MethodOne")
assert.True(t, ok)
methodType := suiteMethod(method)
method = methodType.Method()
assert.Equal(t, "func(*gxdubbogo.TestService, context.Context, interface {}, interface {}, interface {}) error", method.Type.String())
at := methodType.ArgsType()
assert.Equal(t, testSuiteMethodExpectedString, at[0].String())
assert.Equal(t, testSuiteMethodExpectedString, at[1].String())
assert.Equal(t, testSuiteMethodExpectedString, at[2].String())
ct := methodType.CtxType()
assert.Equal(t, "context.Context", ct.String())
rt := methodType.ReplyType()
assert.Nil(t, rt)
method, ok = reflect.TypeOf(s).MethodByName("MethodTwo")
assert.True(t, ok)
methodType = suiteMethod(method)
method = methodType.Method()
assert.Equal(t, "func(*gxdubbogo.TestService, interface {}, interface {}, interface {}) (interface {}, error)", method.Type.String())
at = methodType.ArgsType()
assert.Equal(t, testSuiteMethodExpectedString, at[0].String())
assert.Equal(t, testSuiteMethodExpectedString, at[1].String())
assert.Equal(t, testSuiteMethodExpectedString, at[2].String())
assert.Nil(t, methodType.CtxType())
rt = methodType.ReplyType()
assert.Equal(t, testSuiteMethodExpectedString, rt.String())
method, ok = reflect.TypeOf(s).MethodByName("MethodThree")
assert.True(t, ok)
methodType = suiteMethod(method)
method = methodType.Method()
assert.Equal(t, "func(*gxdubbogo.TestService) error", method.Type.String())
at = methodType.ArgsType()
assert.Equal(t, 0, len(at))
assert.Nil(t, methodType.CtxType())
rt = methodType.ReplyType()
assert.Nil(t, rt)
// wrong number of in return
s1 := &testService{}
method, ok = reflect.TypeOf(s1).MethodByName("Reference")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
// args not exported
method, ok = reflect.TypeOf(s1).MethodByName("Method1")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
// Reply not exported
method, ok = reflect.TypeOf(s1).MethodByName("Method2")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
// no return
method, ok = reflect.TypeOf(s1).MethodByName("Method3")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
// return value is not error
method, ok = reflect.TypeOf(s1).MethodByName("Method4")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
}
This diff is collapsed.
This diff is collapsed.
module github.com/dubbogo/gost module github.com/dubbogo/gost
require ( require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/Workiva/go-datastructures v1.0.52
github.com/coreos/bbolt v1.3.3 // indirect github.com/apache/dubbo-go v1.5.6
github.com/coreos/etcd v3.3.25+incompatible github.com/coreos/etcd v3.3.25+incompatible
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/dubbogo/go-zookeeper v1.0.3 github.com/dubbogo/go-zookeeper v1.0.3
github.com/dubbogo/jsonparser v1.0.1 github.com/dubbogo/jsonparser v1.0.1
github.com/dustin/go-humanize v1.0.0 // indirect github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible github.com/k0kubun/pp v3.0.1+incompatible
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-isatty v0.0.12
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.9.0 // indirect github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/shirou/gopsutil v3.20.11-0.20201116082039-2fb5da2f2449+incompatible github.com/shirou/gopsutil v3.20.11+incompatible
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.7.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
go.etcd.io/bbolt v1.3.4 // indirect
go.uber.org/atomic v1.7.0 go.uber.org/atomic v1.7.0
go.uber.org/zap v1.16.0 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/grpc v1.33.1 google.golang.org/grpc v1.33.1
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
) )
replace ( replace (
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment