Skip to content

Commit

Permalink
1.修改LockedSlice逻辑错误
Browse files Browse the repository at this point in the history
2.减小GTP协议握手内存
  • Loading branch information
pangdogs committed Sep 21, 2024
1 parent 221d586 commit 0cea097
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 35 deletions.
4 changes: 2 additions & 2 deletions plugins/dserv/distservice_msgwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package dserv

import (
"context"
"github.com/elliotchance/pie/v2"
"slices"
)

func (d *_DistService) newMsgWatcher(ctx context.Context, handler RecvMsgHandler) *_MsgWatcher {
Expand Down Expand Up @@ -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
})
})
Expand Down
2 changes: 1 addition & 1 deletion plugins/gate/acceptor_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions plugins/gate/cli/client_datawatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package cli

import (
"context"
"github.com/elliotchance/pie/v2"
)

func (c *Client) newDataWatcher(ctx context.Context, handler RecvDataHandler) *_DataWatcher {
Expand Down Expand Up @@ -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
})
}
7 changes: 2 additions & 5 deletions plugins/gate/cli/client_eventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package cli

import (
"context"
"github.com/elliotchance/pie/v2"
)

func (c *Client) newEventWatcher(ctx context.Context, handler RecvEventHandler) *_EventWatcher {
Expand Down Expand Up @@ -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
})
}
2 changes: 1 addition & 1 deletion plugins/gate/cli/connector_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions plugins/gate/gate_sessionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package gate

import (
"context"
"github.com/elliotchance/pie/v2"
)

func (g *_Gate) newSessionWatcher(ctx context.Context, handler SessionStateChangedHandler) *_SessionWatcher {
Expand Down Expand Up @@ -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
})
}
7 changes: 2 additions & 5 deletions plugins/gate/session_datawatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package gate

import (
"context"
"github.com/elliotchance/pie/v2"
)

func (s *_Session) newDataWatcher(ctx context.Context, handler SessionRecvDataHandler) *_DataWatcher {
Expand Down Expand Up @@ -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
})
}
7 changes: 2 additions & 5 deletions plugins/gate/session_eventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package gate

import (
"context"
"github.com/elliotchance/pie/v2"
)

func (s *_Session) newEventWatcher(ctx context.Context, handler SessionRecvEventHandler) *_EventWatcher {
Expand Down Expand Up @@ -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
})
}
56 changes: 50 additions & 6 deletions utils/concurrent/lockedslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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]
})
})
}

Expand Down

0 comments on commit 0cea097

Please sign in to comment.