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
11 changes: 8 additions & 3 deletions session/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/metagogs/gogs/config"
"github.com/metagogs/gogs/gslog"
"github.com/metagogs/gogs/networkentity"
"github.com/metagogs/gogs/utils/slicex"
"go.uber.org/zap"
)

Expand All @@ -33,29 +34,33 @@ type SessionFilter struct {
type sessionList struct {
mutex sync.Mutex
data map[int64]*sessionWrap
list []int64
}

func (s *sessionList) Add(w *sessionWrap) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.data[w.SessionID] = w
s.list = append(s.list, w.SessionID)
}

func (s *sessionList) Delete(id int64) int {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.data, id)
s.list = slicex.RemoveSliceItem(s.list, id)
return len(s.data)
}

// GetList get session list by filter, return the filter result and all the sessions as backup
// if we can not get the result, we can check user's all the session list
// then we can send message use another session
func (s *sessionList) GetList(filter *SessionFilter) ([]int64, []int64) {
if filter == nil {
return s.list, s.list
}
result := []int64{}
all := []int64{}
for _, v := range s.data {
all = append(all, v.SessionID)
if filter != nil {
if len(filter.ConnType) > 0 && v.ConnType != filter.ConnType {
continue
Expand All @@ -73,7 +78,7 @@ func (s *sessionList) GetList(filter *SessionFilter) ([]int64, []int64) {
result = append(result, v.SessionID)
}

return result, all
return result, s.list
}

type sessionWrap struct {
Expand Down
2 changes: 1 addition & 1 deletion session/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func BroadcastMessage(users []string, send interface{}, filter *SessionFilter, e
continue
}
if result, _ := GetSessionByUID(u, filter); len(result) > 0 {
go SendMessageByID(result[0], send)
SendMessageByID(result[0], send)
}
}
}
12 changes: 11 additions & 1 deletion utils/slicex/slicex.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package slicex

func InSlice(target string, data []string) bool {
func InSlice[T comparable](target T, data []T) bool {
for _, s := range data {
if s == target {
return true
Expand All @@ -9,3 +9,13 @@ func InSlice(target string, data []string) bool {

return false
}

func RemoveSliceItem[T comparable](list []T, item T) []T {
for i, v := range list {
if v == item {
list = append(list[:i], list[i+1:]...)
break
}
}
return list
}
91 changes: 90 additions & 1 deletion utils/slicex/slicex_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package slicex

import "testing"
import (
"reflect"
"testing"
)

func TestInSlice(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -44,4 +47,90 @@ func TestInSlice(t *testing.T) {
}
})
}

type argsInt struct {
target int
data []int
}
testsInt := []struct {
name string
args argsInt
want bool
}{
{
name: "test1",
args: argsInt{
target: 1,
data: []int{1, 2, 3},
},
want: true,
},
{
name: "test2",
args: argsInt{
target: 4,
data: []int{1, 2, 3},
},
want: false,
},
{
name: "test3",
args: argsInt{
target: 1,
data: []int{},
},
want: false,
},
}
for _, tt := range testsInt {
t.Run(tt.name, func(t *testing.T) {
if got := InSlice(tt.args.target, tt.args.data); got != tt.want {
t.Errorf("InSlice() = %v, want %v", got, tt.want)
}
})
}
}

func TestRemoveSliceItem(t *testing.T) {
type args struct {
list []string
item string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "test1",
args: args{
list: []string{"a", "b", "c"},
item: "b",
},
want: []string{"a", "c"},
},
{
name: "test2",
args: args{
list: []string{"a", "b", "c"},
item: "d",
},
want: []string{"a", "b", "c"},
},
{
name: "test3",
args: args{
list: []string{},
item: "d",
},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveSliceItem(tt.args.list, tt.args.item); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveSliceItem() = %v, want %v", got, tt.want)
}
})
}
}