From 0cea097984d2576b7f62cb0f7ce4b4c2a441626f Mon Sep 17 00:00:00 2001 From: pangdogs Date: Sat, 21 Sep 2024 15:00:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9LockedSlice=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=94=99=E8=AF=AF=202.=E5=87=8F=E5=B0=8FGTP=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E6=8F=A1=E6=89=8B=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/dserv/distservice_msgwatcher.go | 4 +- plugins/gate/acceptor_handshake.go | 2 +- plugins/gate/cli/client_datawatcher.go | 7 +--- plugins/gate/cli/client_eventwatcher.go | 7 +--- plugins/gate/cli/connector_handshake.go | 2 +- plugins/gate/gate_sessionwatcher.go | 7 +--- plugins/gate/session_datawatcher.go | 7 +--- plugins/gate/session_eventwatcher.go | 7 +--- utils/concurrent/lockedslice.go | 56 ++++++++++++++++++++++--- 9 files changed, 64 insertions(+), 35 deletions(-) diff --git a/plugins/dserv/distservice_msgwatcher.go b/plugins/dserv/distservice_msgwatcher.go index 7dc871ff..90f06758 100644 --- a/plugins/dserv/distservice_msgwatcher.go +++ b/plugins/dserv/distservice_msgwatcher.go @@ -21,7 +21,7 @@ package dserv import ( "context" - "github.com/elliotchance/pie/v2" + "slices" ) func (d *_DistService) newMsgWatcher(ctx context.Context, handler RecvMsgHandler) *_MsgWatcher { @@ -76,7 +76,7 @@ func (w *_MsgWatcher) mainLoop() { } w.distributed.msgWatchers.AutoLock(func(watchers *[]*_MsgWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_MsgWatcher) bool { + *watchers = slices.DeleteFunc(*watchers, func(other *_MsgWatcher) bool { return other == w }) }) diff --git a/plugins/gate/acceptor_handshake.go b/plugins/gate/acceptor_handshake.go index 1122cb3f..b4a4662b 100644 --- a/plugins/gate/acceptor_handshake.go +++ b/plugins/gate/acceptor_handshake.go @@ -173,7 +173,7 @@ func (acc *_Acceptor) handshake(ctx context.Context, conn net.Conn) (*_Session, if encryptionFlow { h := sha256.New() - hashBuff := binaryutil.BytesPool.Get(8 * 1024) + hashBuff := binaryutil.BytesPool.Get(4 * 1024) defer binaryutil.BytesPool.Put(hashBuff) h.Reset() diff --git a/plugins/gate/cli/client_datawatcher.go b/plugins/gate/cli/client_datawatcher.go index d25582fd..d1104d22 100644 --- a/plugins/gate/cli/client_datawatcher.go +++ b/plugins/gate/cli/client_datawatcher.go @@ -21,7 +21,6 @@ package cli import ( "context" - "github.com/elliotchance/pie/v2" ) func (c *Client) newDataWatcher(ctx context.Context, handler RecvDataHandler) *_DataWatcher { @@ -75,9 +74,7 @@ func (w *_DataWatcher) mainLoop() { case <-w.client.Done(): } - w.client.dataWatchers.AutoLock(func(watchers *[]*_DataWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_DataWatcher) bool { - return other == w - }) + w.client.dataWatchers.Delete(func(exists *_DataWatcher) bool { + return exists == w }) } diff --git a/plugins/gate/cli/client_eventwatcher.go b/plugins/gate/cli/client_eventwatcher.go index d687e0d2..30b62bfd 100644 --- a/plugins/gate/cli/client_eventwatcher.go +++ b/plugins/gate/cli/client_eventwatcher.go @@ -21,7 +21,6 @@ package cli import ( "context" - "github.com/elliotchance/pie/v2" ) func (c *Client) newEventWatcher(ctx context.Context, handler RecvEventHandler) *_EventWatcher { @@ -75,9 +74,7 @@ func (w *_EventWatcher) mainLoop() { case <-w.client.Done(): } - w.client.eventWatchers.AutoLock(func(watchers *[]*_EventWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_EventWatcher) bool { - return other == w - }) + w.client.eventWatchers.Delete(func(exists *_EventWatcher) bool { + return exists == w }) } diff --git a/plugins/gate/cli/connector_handshake.go b/plugins/gate/cli/connector_handshake.go index e4e10d77..2bad65f2 100644 --- a/plugins/gate/cli/connector_handshake.go +++ b/plugins/gate/cli/connector_handshake.go @@ -124,7 +124,7 @@ func (ctor *_Connector) handshake(ctx context.Context, conn net.Conn, client *Cl // 记录双方hello数据,用于ecdh后加密验证 h := sha256.New() - hashBuff := binaryutil.BytesPool.Get(8 * 1024) + hashBuff := binaryutil.BytesPool.Get(4 * 1024) defer binaryutil.BytesPool.Put(hashBuff) h.Reset() diff --git a/plugins/gate/gate_sessionwatcher.go b/plugins/gate/gate_sessionwatcher.go index 398b4132..1beb782f 100644 --- a/plugins/gate/gate_sessionwatcher.go +++ b/plugins/gate/gate_sessionwatcher.go @@ -21,7 +21,6 @@ package gate import ( "context" - "github.com/elliotchance/pie/v2" ) func (g *_Gate) newSessionWatcher(ctx context.Context, handler SessionStateChangedHandler) *_SessionWatcher { @@ -75,9 +74,7 @@ func (w *_SessionWatcher) mainLoop() { case <-w.gate.ctx.Done(): } - w.gate.sessionWatchers.AutoLock(func(watchers *[]*_SessionWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_SessionWatcher) bool { - return other == w - }) + w.gate.sessionWatchers.Delete(func(exists *_SessionWatcher) bool { + return exists == w }) } diff --git a/plugins/gate/session_datawatcher.go b/plugins/gate/session_datawatcher.go index 4dedca3e..622da5d9 100644 --- a/plugins/gate/session_datawatcher.go +++ b/plugins/gate/session_datawatcher.go @@ -21,7 +21,6 @@ package gate import ( "context" - "github.com/elliotchance/pie/v2" ) func (s *_Session) newDataWatcher(ctx context.Context, handler SessionRecvDataHandler) *_DataWatcher { @@ -75,9 +74,7 @@ func (w *_DataWatcher) mainLoop() { case <-w.session.Done(): } - w.session.dataWatchers.AutoLock(func(watchers *[]*_DataWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_DataWatcher) bool { - return other == w - }) + w.session.dataWatchers.Delete(func(exists *_DataWatcher) bool { + return exists == w }) } diff --git a/plugins/gate/session_eventwatcher.go b/plugins/gate/session_eventwatcher.go index d985870c..87bb4137 100644 --- a/plugins/gate/session_eventwatcher.go +++ b/plugins/gate/session_eventwatcher.go @@ -21,7 +21,6 @@ package gate import ( "context" - "github.com/elliotchance/pie/v2" ) func (s *_Session) newEventWatcher(ctx context.Context, handler SessionRecvEventHandler) *_EventWatcher { @@ -75,9 +74,7 @@ func (w *_EventWatcher) mainLoop() { case <-w.session.Done(): } - w.session.eventWatchers.AutoLock(func(watchers *[]*_EventWatcher) { - *watchers = pie.DropWhile(*watchers, func(other *_EventWatcher) bool { - return other == w - }) + w.session.eventWatchers.Delete(func(exists *_EventWatcher) bool { + return exists == w }) } diff --git a/utils/concurrent/lockedslice.go b/utils/concurrent/lockedslice.go index 74798167..8578877e 100644 --- a/utils/concurrent/lockedslice.go +++ b/utils/concurrent/lockedslice.go @@ -22,6 +22,8 @@ package concurrent import ( "git.golaxy.org/core/utils/generic" "github.com/elliotchance/pie/v2" + "math/rand" + "slices" ) func MakeLockedSlice[T any](len, cap int) LockedSlice[T] { @@ -40,21 +42,63 @@ type LockedSlice[T any] struct { RWLocked[[]T] } -func (ls *LockedSlice[T]) Insert(idx int, values ...T) { +func (ls *LockedSlice[T]) Append(values ...T) { ls.AutoLock(func(s *[]T) { - *s = pie.Insert(*s, idx, values...) + *s = append(*s, values...) }) } -func (ls *LockedSlice[T]) Append(values ...T) { +func (ls *LockedSlice[T]) Delete(fun generic.Func1[T, bool]) { + ls.AutoLock(func(s *[]T) { + *s = slices.DeleteFunc(*s, fun) + }) +} + +func (ls *LockedSlice[T]) Any(fun generic.Func1[T, bool]) (ret bool) { + ls.AutoRLock(func(s *[]T) { + ret = pie.Any(*s, fun) + }) + return +} + +func (ls *LockedSlice[T]) All(fun generic.Func1[T, bool]) (ret bool) { + ls.AutoRLock(func(s *[]T) { + ret = pie.All(*s, fun) + }) + return +} + +func (ls *LockedSlice[T]) Filter(fun generic.Func1[T, bool]) (ret []T) { + ls.AutoRLock(func(s *[]T) { + ret = pie.Filter(*s, fun) + }) + return +} + +func (ls *LockedSlice[T]) FilterNot(fun generic.Func1[T, bool]) (ret []T) { + ls.AutoRLock(func(s *[]T) { + ret = pie.FilterNot(*s, fun) + }) + return +} + +func (ls *LockedSlice[T]) Sort(fun generic.Func2[T, T, int]) { + ls.AutoLock(func(s *[]T) { + slices.SortFunc(*s, fun) + }) +} + +func (ls *LockedSlice[T]) SortStable(fun generic.Func2[T, T, int]) { ls.AutoLock(func(s *[]T) { - *s = pie.Insert(ls.object, len(ls.object), values...) + slices.SortStableFunc(*s, fun) }) } -func (ls *LockedSlice[T]) Delete(idx ...int) { +func (ls *LockedSlice[T]) Shuffle(n int) { ls.AutoLock(func(s *[]T) { - *s = pie.Delete(ls.object, idx...) + rand.Shuffle(n, func(i, j int) { + (*s)[i], (*s)[j] = (*s)[j], (*s)[i] + }) }) }