Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cluster/router/condition/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ func TestRouteReturn(t *testing.T) {
resVal := len(filterInvokers)

assert.Equal(t, data.wantVal, resVal)
assert.Equal(t, wantInvokers, filterInvokers)
for i := range filterInvokers {
assert.Equal(t, wantInvokers[i].GetURL().String(), filterInvokers[i].GetURL().String())
}
})
}
}
Expand Down
26 changes: 18 additions & 8 deletions protocol/base/base_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol/result"
)

const (
protocolDestroyed = "dubbo-go-internal-invoker-destroyed"
)

var (
// emptyURL is a global placeholder to prevent nil pointer dereferences during invoker destruction.
// It is returned by GetURL() when the invoker is destroyed, ensuring that concurrent readers receive a safe, initialized object.
emptyURL = common.NewURLWithOptions(common.WithProtocol(protocolDestroyed))
ErrClientClosed = perrors.New("remoting client has closed")
ErrNoReply = perrors.New("request need @response")
ErrDestroyedInvoker = perrors.New("request Destroyed invoker")
Expand All @@ -53,16 +60,15 @@ type Invoker interface {

// BaseInvoker provides default invoker implements Invoker
type BaseInvoker struct {
url *common.URL
url uatomic.Pointer[common.URL]
available uatomic.Bool
destroyed uatomic.Bool
}

// NewBaseInvoker creates a new BaseInvoker
func NewBaseInvoker(url *common.URL) *BaseInvoker {
ivk := &BaseInvoker{
url: url,
}
ivk := &BaseInvoker{}
ivk.url.Store(url)
ivk.available.Store(true)
ivk.destroyed.Store(false)

Expand All @@ -71,7 +77,11 @@ func NewBaseInvoker(url *common.URL) *BaseInvoker {

// GetURL gets base invoker URL
func (bi *BaseInvoker) GetURL() *common.URL {
return bi.url
u := bi.url.Load()
if u == nil {
return emptyURL
}
return u
}
Comment thread
Alanxtl marked this conversation as resolved.

// IsAvailable gets available flag
Expand All @@ -94,13 +104,13 @@ func (bi *BaseInvoker) Destroy() {
logger.Infof("Destroy invoker: %s", bi.GetURL())
bi.destroyed.Store(true)
bi.available.Store(false)
bi.url = nil
bi.url.Store(nil)
}

func (bi *BaseInvoker) String() string {
if bi.url != nil {
if u := bi.url.Load(); u != nil {
return fmt.Sprintf("invoker{protocol: %s, host: %s:%s, path: %s}",
bi.url.Protocol, bi.url.Ip, bi.url.Port, bi.url.Path)
u.Protocol, u.Ip, u.Port, u.Path)
}
return fmt.Sprintf("%#v", bi)
}
5 changes: 4 additions & 1 deletion protocol/base/base_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func TestBaseInvokerWithFullURL(t *testing.T) {
ivk.Destroy()
assert.False(t, ivk.IsAvailable())
assert.True(t, ivk.IsDestroyed())
assert.Nil(t, ivk.GetURL())

safeURL := ivk.GetURL()
assert.NotNil(t, safeURL)
assert.Equal(t, "dubbo-go-internal-invoker-destroyed", safeURL.Protocol)

// Test String method after destroy (url is nil)
str = ivk.String()
Expand Down
Loading