Skip to content

Commit 9a614b1

Browse files
committed
fix(protocol/dubbo): remove shared versionInt map state
- remove package-level versionInt maps in dubbo impl and hessian2 response paths - compute version int per call to avoid shared mutable/global state - add concurrent isSupportResponseAttachment tests in both packages Fixes: #3247
1 parent 9e80d8f commit 9a614b1

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

protocol/dubbo/hessian2/hessian_response.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,6 @@ func ReflectResponse(in any, out any) error {
402402
return nil
403403
}
404404

405-
var versionInt = make(map[string]int)
406-
407405
// https://github.com/apache/dubbo/blob/dubbo-2.7.1/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java#L96
408406
// isSupportResponseAttachment is for compatibility among some dubbo version
409407
func isSupportResponseAttachment(ver any) bool {
@@ -412,12 +410,9 @@ func isSupportResponseAttachment(ver any) bool {
412410
return false
413411
}
414412

415-
v, ok := versionInt[version]
416-
if !ok {
417-
v = version2Int(version)
418-
if v == -1 {
419-
return false
420-
}
413+
v := version2Int(version)
414+
if v == -1 {
415+
return false
421416
}
422417

423418
if v >= 2001000 && v <= 2060200 { // 2.0.10 ~ 2.6.2

protocol/dubbo/hessian2/hessian_response_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package hessian2
1919

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

@@ -187,6 +188,23 @@ func TestIsSupportResponseAttachment(t *testing.T) {
187188
assert.True(t, is)
188189
}
189190

191+
func TestIsSupportResponseAttachmentConcurrent(t *testing.T) {
192+
versions := []any{"2.X", "2.0.10", "2.5.3", "2.6.2", "2.0.2", "2.7.2", ""}
193+
var wg sync.WaitGroup
194+
195+
for i := 0; i < 200; i++ {
196+
for _, version := range versions {
197+
wg.Add(1)
198+
go func(v any) {
199+
defer wg.Done()
200+
_ = isSupportResponseAttachment(v)
201+
}(version)
202+
}
203+
}
204+
205+
wg.Wait()
206+
}
207+
190208
func TestVersion2Int(t *testing.T) {
191209
v := version2Int("2.1.3")
192210
assert.Equal(t, 2010300, v)

protocol/dubbo/impl/hessian.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,16 @@ func marshalRequest(encoder *hessian.Encoder, p DubboPackage) ([]byte, error) {
163163
return encoder.Buffer(), err
164164
}
165165

166-
var versionInt = make(map[string]int)
167-
168166
// https://github.com/apache/dubbo/blob/dubbo-2.7.1/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java#L96
169167
// isSupportResponseAttachment is for compatibility among some dubbo version
170168
func isSupportResponseAttachment(version string) bool {
171169
if version == "" {
172170
return false
173171
}
174172

175-
v, ok := versionInt[version]
176-
if !ok {
177-
v = version2Int(version)
178-
if v == -1 {
179-
return false
180-
}
173+
v := version2Int(version)
174+
if v == -1 {
175+
return false
181176
}
182177

183178
if v >= 2001000 && v <= 2060200 { // 2.0.10 ~ 2.6.2

protocol/dubbo/impl/hessian_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package impl
1919

2020
import (
2121
"errors"
22+
"sync"
2223
"testing"
2324
"time"
2425
)
@@ -329,6 +330,23 @@ func TestIsSupportResponseAttachment(t *testing.T) {
329330
})
330331
}
331332

333+
func TestIsSupportResponseAttachmentConcurrent(t *testing.T) {
334+
versions := []string{"", "2.0.10", "2.6.2", "2.7.0", "3.0.0", "invalid"}
335+
var wg sync.WaitGroup
336+
337+
for i := 0; i < 200; i++ {
338+
for _, version := range versions {
339+
wg.Add(1)
340+
go func(v string) {
341+
defer wg.Done()
342+
_ = isSupportResponseAttachment(v)
343+
}(version)
344+
}
345+
}
346+
347+
wg.Wait()
348+
}
349+
332350
func TestMarshalRequestWithTypedNilPointer(t *testing.T) {
333351
encoder := hessian.NewEncoder()
334352
pkg := DubboPackage{

0 commit comments

Comments
 (0)