Skip to content

Commit ad18357

Browse files
authored
Merge pull request #1 from apache/feature/dubbo-2.7.5
Feature/dubbo 2.7.5
2 parents a594afd + 370681a commit ad18357

Some content is hidden

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

47 files changed

+1919
-241
lines changed

common/constant/key.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
VERSION_KEY = "version"
2727
INTERFACE_KEY = "interface"
2828
PATH_KEY = "path"
29+
PROTOCOL_KEY = "protocol"
2930
SERVICE_KEY = "service"
3031
METHODS_KEY = "methods"
3132
TIMEOUT_KEY = "timeout"
@@ -40,6 +41,7 @@ const (
4041
TOKEN_KEY = "token"
4142
LOCAL_ADDR = "local-addr"
4243
REMOTE_ADDR = "remote-addr"
44+
PATH_SEPARATOR = "/"
4345
DUBBO_KEY = "dubbo"
4446
RELEASE_KEY = "release"
4547
ANYHOST_KEY = "anyhost"

common/extension/event_dispatcher.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package extension
19+
20+
import (
21+
"github.com/apache/dubbo-go/common/logger"
22+
"github.com/apache/dubbo-go/common/observer"
23+
)
24+
25+
var (
26+
globalEventDispatcher observer.EventDispatcher
27+
initEventListeners []observer.EventListener
28+
)
29+
30+
var (
31+
dispatchers = make(map[string]func() observer.EventDispatcher, 8)
32+
)
33+
34+
// SetEventDispatcher by name
35+
func SetEventDispatcher(name string, v func() observer.EventDispatcher) {
36+
dispatchers[name] = v
37+
}
38+
39+
// SetAndInitGlobalDispatcher
40+
func SetAndInitGlobalDispatcher(name string) {
41+
if len(name) == 0 {
42+
name = "direct"
43+
}
44+
if globalEventDispatcher != nil {
45+
logger.Warnf("EventDispatcher already init. It will be replaced")
46+
}
47+
if dp, ok := dispatchers[name]; !ok || dp == nil {
48+
panic("EventDispatcher for " + name + " is not existing, make sure you have import the package.")
49+
}
50+
globalEventDispatcher = dispatchers[name]()
51+
globalEventDispatcher.AddEventListeners(initEventListeners)
52+
}
53+
54+
// GetGlobalDispatcher
55+
func GetGlobalDispatcher() observer.EventDispatcher {
56+
return globalEventDispatcher
57+
}
58+
59+
// AddEventListener it will be added in global event dispatcher
60+
func AddEventListener(listener observer.EventListener) {
61+
initEventListeners = append(initEventListeners, listener)
62+
}

common/extension/metadata_report_factory.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
package extension
1919

2020
import (
21-
"github.com/apache/dubbo-go/metadata"
21+
"github.com/apache/dubbo-go/metadata/report/factory"
2222
)
2323

2424
var (
25-
metaDataReportFactories = make(map[string]func() metadata.MetadataReportFactory, 8)
25+
metaDataReportFactories = make(map[string]func() factory.MetadataReportFactory, 8)
2626
)
2727

2828
// SetMetadataReportFactory ...
29-
func SetMetadataReportFactory(name string, v func() metadata.MetadataReportFactory) {
29+
func SetMetadataReportFactory(name string, v func() factory.MetadataReportFactory) {
3030
metaDataReportFactories[name] = v
3131
}
3232

3333
// GetMetadataReportFactory ...
34-
func GetMetadataReportFactory(name string) metadata.MetadataReportFactory {
34+
func GetMetadataReportFactory(name string) factory.MetadataReportFactory {
3535
if metaDataReportFactories[name] == nil {
3636
panic("metadata report for " + name + " is not existing, make sure you have import the package.")
3737
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package dispatcher
19+
20+
import (
21+
"reflect"
22+
)
23+
24+
import (
25+
"github.com/apache/dubbo-go/common/extension"
26+
"github.com/apache/dubbo-go/common/logger"
27+
"github.com/apache/dubbo-go/common/observer"
28+
)
29+
30+
func init() {
31+
extension.SetEventDispatcher("direct", NewDirectEventDispatcher)
32+
}
33+
34+
// DirectEventDispatcher is align with DirectEventDispatcher interface in Java.
35+
// it's the top abstraction
36+
// Align with 2.7.5
37+
// Dispatcher event to listener direct
38+
type DirectEventDispatcher struct {
39+
observer.BaseListenable
40+
}
41+
42+
// NewDirectEventDispatcher ac constructor of DirectEventDispatcher
43+
func NewDirectEventDispatcher() observer.EventDispatcher {
44+
return &DirectEventDispatcher{}
45+
}
46+
47+
// Dispatch event directly
48+
func (ded *DirectEventDispatcher) Dispatch(event observer.Event) {
49+
if event == nil {
50+
logger.Warnf("[DirectEventDispatcher] dispatch event nil")
51+
return
52+
}
53+
eventType := reflect.TypeOf(event).Elem()
54+
value, loaded := ded.ListenersCache.Load(eventType)
55+
if !loaded {
56+
return
57+
}
58+
listenersSlice := value.([]observer.EventListener)
59+
for _, listener := range listenersSlice {
60+
if err := listener.OnEvent(event); err != nil {
61+
logger.Warnf("[DirectEventDispatcher] dispatch event error:%v", err)
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package dispatcher
19+
20+
import (
21+
"fmt"
22+
"reflect"
23+
"testing"
24+
)
25+
26+
import (
27+
"github.com/apache/dubbo-go/common/observer"
28+
)
29+
30+
func TestDirectEventDispatcher_Dispatch(t *testing.T) {
31+
ded := NewDirectEventDispatcher()
32+
ded.AddEventListener(&TestEventListener{})
33+
ded.AddEventListener(&TestEventListener1{})
34+
ded.Dispatch(&TestEvent{})
35+
ded.Dispatch(nil)
36+
}
37+
38+
type TestEvent struct {
39+
observer.BaseEvent
40+
}
41+
42+
type TestEventListener struct {
43+
observer.BaseListenable
44+
observer.EventListener
45+
}
46+
47+
func (tel *TestEventListener) OnEvent(e observer.Event) error {
48+
fmt.Println("TestEventListener")
49+
return nil
50+
}
51+
52+
func (tel *TestEventListener) GetPriority() int {
53+
return -1
54+
}
55+
56+
func (tel *TestEventListener) GetEventType() reflect.Type {
57+
return reflect.TypeOf(&TestEvent{})
58+
}
59+
60+
type TestEventListener1 struct {
61+
observer.EventListener
62+
}
63+
64+
func (tel *TestEventListener1) OnEvent(e observer.Event) error {
65+
fmt.Println("TestEventListener1")
66+
return nil
67+
}
68+
69+
func (tel *TestEventListener1) GetPriority() int {
70+
return 1
71+
}
72+
73+
func (tel *TestEventListener1) GetEventType() reflect.Type {
74+
return reflect.TypeOf(TestEvent{})
75+
}

common/observer/event.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package observer
19+
20+
import (
21+
"fmt"
22+
"math/rand"
23+
"time"
24+
)
25+
26+
func init() {
27+
rand.Seed(time.Now().UnixNano())
28+
}
29+
30+
// Event is align with Event interface in Java.
31+
// it's the top abstraction
32+
// Align with 2.7.5
33+
type Event interface {
34+
fmt.Stringer
35+
GetSource() interface{}
36+
GetTimestamp() time.Time
37+
}
38+
39+
// BaseEvent is the base implementation of Event
40+
// You should never use it directly
41+
type BaseEvent struct {
42+
Source interface{}
43+
Timestamp time.Time
44+
}
45+
46+
// GetSource return the source
47+
func (b *BaseEvent) GetSource() interface{} {
48+
return b.Source
49+
}
50+
51+
// GetTimestamp return the Timestamp when the event is created
52+
func (b *BaseEvent) GetTimestamp() time.Time {
53+
return b.Timestamp
54+
}
55+
56+
// String return a human readable string representing this event
57+
func (b *BaseEvent) String() string {
58+
return fmt.Sprintf("BaseEvent[source = %#v]", b.Source)
59+
}
60+
61+
func newBaseEvent(source interface{}) *BaseEvent {
62+
return &BaseEvent{
63+
Source: source,
64+
Timestamp: time.Now(),
65+
}
66+
}

common/observer/event_dispatcher.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package observer
19+
20+
// EventDispatcher is align with EventDispatcher interface in Java.
21+
// it's the top abstraction
22+
// Align with 2.7.5
23+
type EventDispatcher interface {
24+
Listenable
25+
// Dispatch event
26+
Dispatch(event Event)
27+
}

common/observer/event_listener.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package observer
19+
20+
import (
21+
"reflect"
22+
)
23+
24+
import (
25+
gxsort "github.com/dubbogo/gost/sort"
26+
)
27+
28+
// EventListener is an new interface used to align with dubbo 2.7.5
29+
// It contains the Prioritized means that the listener has its priority
30+
type EventListener interface {
31+
gxsort.Prioritizer
32+
// OnEvent handle this event
33+
OnEvent(e Event) error
34+
// GetEventType listen which event type
35+
GetEventType() reflect.Type
36+
}
37+
38+
// ConditionalEventListener only handle the event which it can handle
39+
type ConditionalEventListener interface {
40+
EventListener
41+
// Accept will make the decision whether it should handle this event
42+
Accept(e Event) bool
43+
}
44+
45+
// TODO (implement ConditionalEventListener)
46+
type ServiceInstancesChangedListener struct {
47+
ServiceName string
48+
}

0 commit comments

Comments
 (0)