Skip to content

Commit fae8c67

Browse files
authored
Merge pull request #8 from ascenmmo/stage
Stage
2 parents dced99b + 8dec05e commit fae8c67

5 files changed

Lines changed: 13 additions & 21 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@ go.work
2222
go.work.sum
2323

2424
# env file
25-
.env
2625
.idea

cmd/udp/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
env.UDPPort,
2222
env.TokenKey,
2323
env.MaxRequestPerSecond,
24-
5,
24+
10,
2525
logger,
2626
true)
2727
if err != nil {

internal/service/service.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ func (s *service) setNewUser(ds connection.DataSender, req []byte) (clientInfo *
108108
s.storage.SetData(ds.GetID(), info)
109109
roomKey := utils.GenerateRoomKey(info)
110110

111+
var room *types.Room
111112
roomData, ok := s.storage.GetData(roomKey)
112113
if !ok {
113-
return clientInfo, errors.ErrRoomNotFound
114-
}
115-
116-
room, ok := roomData.(*types.Room)
117-
if !ok {
118-
return clientInfo, errors.ErrRoomBadValue
114+
room = &types.Room{
115+
GameID: clientInfo.GameID,
116+
RoomID: clientInfo.RoomID,
117+
}
118+
s.setRoom(*clientInfo, room)
119+
} else {
120+
room, ok = roomData.(*types.Room)
121+
if !ok {
122+
return clientInfo, errors.ErrRoomBadValue
123+
}
119124
}
120125

121126
room.SetUser(&types.User{

internal/tests/udp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
var (
24-
clients = 20
24+
clients = 2000
2525
msgs = 100
2626

2727
baseURl = "http://" + env.ServerAddress + ":" + env.TCPPort

pkg/api/types/room.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package types
33
import (
44
"github.com/ascenmmo/udp-server/internal/connection"
55
"github.com/google/uuid"
6-
"sync"
76
"time"
87
)
98

@@ -16,7 +15,6 @@ type Room struct {
1615
Users []*User
1716

1817
UpdatedAt time.Time
19-
mtx sync.RWMutex
2018
}
2119

2220
type User struct {
@@ -26,22 +24,16 @@ type User struct {
2624
}
2725

2826
func (r *Room) SetUser(user *User) {
29-
r.mtx.Lock()
3027
r.Users = r.setUser(r.Users, user)
31-
r.mtx.Unlock()
3228
}
3329

3430
func (r *Room) GetUser() (users []*User) {
35-
r.mtx.RLock()
3631
users = r.Users
37-
r.mtx.RUnlock()
3832
return
3933
}
4034

4135
func (r *Room) RemoveUser(user uuid.UUID) {
42-
r.mtx.Lock()
4336
r.removeFromArray(user)
44-
r.mtx.Unlock()
4537
}
4638

4739
func (r *Room) SetUpdatedAt() {
@@ -63,13 +55,11 @@ func (r *Room) setUser(users []*User, user *User) (allUsers []*User) {
6355
}
6456

6557
func (r *Room) removeFromArray(userID uuid.UUID) {
66-
r.mtx.Lock()
6758
for i, user := range r.Users {
6859
if user.ID == userID {
6960
r.Users = append(r.Users[:i], r.Users[i+1:]...)
7061
}
7162
}
72-
r.mtx.Unlock()
7363
}
7464

7565
func (r *Room) SetServerID(id uuid.UUID) {
@@ -82,11 +72,9 @@ func (r *Room) SetServerID(id uuid.UUID) {
8272
}
8373

8474
func (r *Room) RemoveServerID(id uuid.UUID) {
85-
r.mtx.Lock()
8675
for i, server := range r.ServerID {
8776
if server == id {
8877
r.ServerID = append(r.ServerID[:i], r.ServerID[i+1:]...)
8978
}
9079
}
91-
r.mtx.Unlock()
9280
}

0 commit comments

Comments
 (0)