Skip to content

Commit d6c452d

Browse files
committed
chore: conn移动到core目录
1 parent 15e54c1 commit d6c452d

File tree

7 files changed

+69
-89
lines changed

7 files changed

+69
-89
lines changed

callback_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func TestOnOpen_RealSocket(t *testing.T) {
6262
}
6363

6464
// 创建Pulse连接
65-
safeConns := &safeConns[Conn]{}
66-
safeConns.init(1000)
65+
safeConns := &core.SafeConns[Conn]{}
66+
safeConns.Init(1000)
6767

6868
pulseConn := &Conn{
6969
fd: int64(fd),
@@ -125,8 +125,8 @@ func TestOnOpen_RealSocket(t *testing.T) {
125125
log.Printf("failed to close original connection: %v", err)
126126
}
127127

128-
safeConns := &safeConns[Conn]{}
129-
safeConns.init(1000)
128+
safeConns := &core.SafeConns[Conn]{}
129+
safeConns.Init(1000)
130130

131131
pulseConn := &Conn{
132132
fd: int64(fd),
@@ -425,8 +425,8 @@ func runClientServerTest(t *testing.T, callback Callback, scenario func(net.Conn
425425
log.Printf("failed to close original connection: %v", err)
426426
}
427427

428-
safeConns := &safeConns[Conn]{}
429-
safeConns.init(1000)
428+
safeConns := &core.SafeConns[Conn]{}
429+
safeConns.Init(1000)
430430

431431
pulseConn := &Conn{
432432
fd: int64(fd),
@@ -567,8 +567,8 @@ func TestCallback_RealConnectionLifecycle(t *testing.T) {
567567
log.Printf("failed to close original connection: %v", err)
568568
}
569569

570-
safeConns := &safeConns[Conn]{}
571-
safeConns.init(1000)
570+
safeConns := &core.SafeConns[Conn]{}
571+
safeConns.Init(1000)
572572

573573
pulseConn := &Conn{
574574
fd: int64(fd),
@@ -714,8 +714,8 @@ func TestCallback_ConcurrentRealConnections(t *testing.T) {
714714
log.Printf("failed to close original connection: %v", err)
715715
}
716716

717-
safeConns := &safeConns[Conn]{}
718-
safeConns.init(1000)
717+
safeConns := &core.SafeConns[Conn]{}
718+
safeConns.Init(1000)
719719

720720
pulseConn := &Conn{
721721
fd: int64(fd),

conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Conn struct {
1717
fd int64
1818
wbufList []*[]byte // write buffer, 为了理精细控制内存使用量
1919
mu sync.Mutex
20-
safeConns *safeConns[Conn]
20+
safeConns *core.SafeConns[Conn]
2121
task driver.TaskExecutor
2222
eventLoop core.PollingApi
2323
readTimer *time.Timer
@@ -38,7 +38,7 @@ func (c *Conn) getFd() int {
3838
return int(atomic.LoadInt64(&c.fd))
3939
}
4040

41-
func newConn(fd int, safeConns *safeConns[Conn],
41+
func newConn(fd int, safeConns *core.SafeConns[Conn],
4242
task selectTasks, taskType TaskType,
4343
eventLoop core.PollingApi, readBufferSize int, flowBackPressureRemoveRead bool) *Conn {
4444
var taskExecutor driver.TaskExecutor

conn_test.go

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"sync/atomic"
77
"testing"
88
"time"
9+
10+
"github.com/antlabs/pulse/core"
911
)
1012

1113
const testSessionData = "test_session"
@@ -108,9 +110,7 @@ func TestConn_SetDeadline(t *testing.T) {
108110
conn := &Conn{
109111
fd: int64(fd.Fd()),
110112
readBufferSize: 4096,
111-
safeConns: &safeConns[Conn]{
112-
conns: make([]*Conn, 1000),
113-
},
113+
safeConns: &core.SafeConns[Conn]{},
114114
}
115115

116116
err = conn.SetDeadline(tt.time)
@@ -155,9 +155,7 @@ func TestConn_SetDeadline(t *testing.T) {
155155
conn := &Conn{
156156
fd: int64(fd.Fd()),
157157
readBufferSize: 4096,
158-
safeConns: &safeConns[Conn]{
159-
conns: make([]*Conn, 1000),
160-
},
158+
safeConns: &core.SafeConns[Conn]{},
161159
}
162160
conn.close()
163161
err = conn.SetDeadline(time.Now().Add(time.Second))
@@ -205,9 +203,7 @@ func TestConn_SetReadDeadline(t *testing.T) {
205203
conn := &Conn{
206204
fd: int64(fd.Fd()),
207205
readBufferSize: 4096,
208-
safeConns: &safeConns[Conn]{
209-
conns: make([]*Conn, 1000),
210-
},
206+
safeConns: &core.SafeConns[Conn]{},
211207
}
212208

213209
err = conn.SetReadDeadline(tt.time)
@@ -252,9 +248,7 @@ func TestConn_SetReadDeadline(t *testing.T) {
252248
conn := &Conn{
253249
fd: int64(fd.Fd()),
254250
readBufferSize: 4096,
255-
safeConns: &safeConns[Conn]{
256-
conns: make([]*Conn, 1000),
257-
},
251+
safeConns: &core.SafeConns[Conn]{},
258252
}
259253
conn.close()
260254
err = conn.SetReadDeadline(time.Now().Add(time.Second))
@@ -302,9 +296,7 @@ func TestConn_SetWriteDeadline(t *testing.T) {
302296
conn := &Conn{
303297
fd: int64(fd.Fd()),
304298
readBufferSize: 4096,
305-
safeConns: &safeConns[Conn]{
306-
conns: make([]*Conn, 1000),
307-
},
299+
safeConns: &core.SafeConns[Conn]{},
308300
}
309301

310302
err = conn.SetWriteDeadline(tt.time)
@@ -349,9 +341,7 @@ func TestConn_SetWriteDeadline(t *testing.T) {
349341
conn := &Conn{
350342
fd: int64(fd.Fd()),
351343
readBufferSize: 4096,
352-
safeConns: &safeConns[Conn]{
353-
conns: make([]*Conn, 1000),
354-
},
344+
safeConns: &core.SafeConns[Conn]{},
355345
}
356346
conn.close()
357347
err = conn.SetWriteDeadline(time.Now().Add(time.Second))
@@ -374,9 +364,7 @@ func TestConn_DeadlineTimeout(t *testing.T) {
374364
conn := &Conn{
375365
fd: int64(fd.Fd()),
376366
readBufferSize: 4096,
377-
safeConns: &safeConns[Conn]{
378-
conns: make([]*Conn, 1000),
379-
},
367+
safeConns: &core.SafeConns[Conn]{},
380368
}
381369

382370
// 设置一个很短的超时时间
@@ -424,9 +412,7 @@ func TestConn_DeadlineReset(t *testing.T) {
424412
conn := &Conn{
425413
fd: int64(fd2),
426414
readBufferSize: 4096,
427-
safeConns: &safeConns[Conn]{
428-
conns: make([]*Conn, 1000),
429-
},
415+
safeConns: &core.SafeConns[Conn]{},
430416
}
431417

432418
// 设置初始超时 - 相对较短的时间
@@ -501,9 +487,7 @@ func TestCallback_OnOpen(t *testing.T) {
501487
return &Conn{
502488
fd: int64(fd.Fd()),
503489
readBufferSize: 4096,
504-
safeConns: &safeConns[Conn]{
505-
conns: make([]*Conn, 1000),
506-
},
490+
safeConns: &core.SafeConns[Conn]{},
507491
}
508492
},
509493
},
@@ -518,9 +502,7 @@ func TestCallback_OnOpen(t *testing.T) {
518502
conn := &Conn{
519503
fd: int64(fd.Fd()),
520504
readBufferSize: 4096,
521-
safeConns: &safeConns[Conn]{
522-
conns: make([]*Conn, 1000),
523-
},
505+
safeConns: &core.SafeConns[Conn]{},
524506
}
525507
conn.SetSession("test_session_data")
526508
return conn
@@ -626,9 +608,7 @@ func TestCallback_OnClose(t *testing.T) {
626608
conn := &Conn{
627609
fd: int64(fd.Fd()),
628610
readBufferSize: 4096,
629-
safeConns: &safeConns[Conn]{
630-
conns: make([]*Conn, 1000),
631-
},
611+
safeConns: &core.SafeConns[Conn]{},
632612
}
633613

634614
var onCloseCalled bool
@@ -698,9 +678,7 @@ func TestConn_CloseCleanup(t *testing.T) {
698678
conn := &Conn{
699679
fd: int64(fd.Fd()),
700680
readBufferSize: 4096,
701-
safeConns: &safeConns[Conn]{
702-
conns: make([]*Conn, 1000),
703-
},
681+
safeConns: &core.SafeConns[Conn]{},
704682
}
705683

706684
// 设置一些状态用于测试清理
@@ -782,9 +760,7 @@ func TestConn_CloseWithTimeout(t *testing.T) {
782760
conn := &Conn{
783761
fd: int64(fd.Fd()),
784762
readBufferSize: 4096,
785-
safeConns: &safeConns[Conn]{
786-
conns: make([]*Conn, 1000),
787-
},
763+
safeConns: &core.SafeConns[Conn]{},
788764
}
789765

790766
var onCloseCallbackCalled bool
@@ -876,9 +852,7 @@ func TestCallback_ToCallback(t *testing.T) {
876852
conn := &Conn{
877853
fd: int64(fd.Fd()),
878854
readBufferSize: 4096,
879-
safeConns: &safeConns[Conn]{
880-
conns: make([]*Conn, 1000),
881-
},
855+
safeConns: &core.SafeConns[Conn]{},
882856
}
883857
defer conn.Close()
884858

safe_conn.go renamed to core/safe_conn.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package pulse
14+
package core
1515

1616
import (
1717
"sync"
@@ -21,19 +21,19 @@ import (
2121

2222
const ptrSize = 4 << (^uintptr(0) >> 63)
2323

24-
type safeConns[T any] struct {
24+
type SafeConns[T any] struct {
2525
mu sync.Mutex
2626
conns []*T
2727
connsPtr **T
2828
len uintptr
2929
}
3030

31-
func (s *safeConns[T]) init(max int) {
31+
func (s *SafeConns[T]) Init(max int) {
3232
s.conns = make([]*T, max)
3333
s.connsPtr = &s.conns[0]
3434
}
3535

36-
func (s *safeConns[T]) Add(fd int, c *T) {
36+
func (s *SafeConns[T]) Add(fd int, c *T) {
3737
if fd == -1 {
3838
return
3939
}
@@ -61,7 +61,7 @@ func add(base unsafe.Pointer, index uintptr) unsafe.Pointer {
6161
return unsafe.Pointer(uintptr(base) + index*ptrSize)
6262
}
6363

64-
func (s *safeConns[T]) addInner(fd int, c *T) {
64+
func (s *SafeConns[T]) addInner(fd int, c *T) {
6565

6666
if fd == -1 {
6767
return
@@ -79,7 +79,7 @@ func (s *safeConns[T]) addInner(fd int, c *T) {
7979
unsafe.Pointer(c))
8080
}
8181

82-
func (s *safeConns[T]) Del(fd int) {
82+
func (s *SafeConns[T]) Del(fd int) {
8383

8484
if fd == -1 {
8585
return
@@ -97,11 +97,11 @@ func (s *safeConns[T]) Del(fd int) {
9797
nil)
9898
}
9999

100-
func (s *safeConns[T]) GetUnsafe(fd int) *T {
100+
func (s *SafeConns[T]) GetUnsafe(fd int) *T {
101101
return s.conns[fd]
102102
}
103103

104-
func (s *safeConns[T]) Get(fd int) *T {
104+
func (s *SafeConns[T]) Get(fd int) *T {
105105
if fd == -1 {
106106
return nil
107107
}
@@ -116,3 +116,7 @@ func (s *safeConns[T]) Get(fd int) *T {
116116
atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&s.connsPtr))),
117117
uintptr(fd)))))
118118
}
119+
120+
func (s *SafeConns[T]) UnsafeConns() []*T {
121+
return s.conns
122+
}

0 commit comments

Comments
 (0)