mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 05:02:58 -05:00
Fix concurrent read and errors by using Mutex
This commit is contained in:
parent
cdea6bd093
commit
ce25f0fd88
4 changed files with 40 additions and 17 deletions
|
@ -67,7 +67,11 @@ func (c *Client) Write() {
|
|||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
_, ok := <-c.send
|
||||
|
||||
if ok {
|
||||
close(c.send)
|
||||
}
|
||||
c.Conn.Close()
|
||||
}
|
||||
|
||||
|
|
|
@ -6,14 +6,12 @@ import (
|
|||
|
||||
func ClientMessageHandler(client *Client, msg string) {
|
||||
splitMsg := strings.Split(msg, " ")
|
||||
|
||||
if len(splitMsg) == 2 && splitMsg[0] == "JOIN" {
|
||||
client.send <- "OK"
|
||||
client.FollowingStreamers[splitMsg[1]] = true
|
||||
FollowStreamer(splitMsg[1])
|
||||
return
|
||||
}
|
||||
|
||||
client.send <- "Invalid request"
|
||||
} else {
|
||||
client.Close()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,41 @@
|
|||
package chat
|
||||
|
||||
type ClientMap map[*Client]bool
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var ClientHandler = ClientMap{}
|
||||
|
||||
func (c ClientMap) AddClient(client *Client) {
|
||||
c[client] = true
|
||||
type ClientMap struct {
|
||||
Clients map[*Client]bool
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (c ClientMap) DeleteClient(ID string) {
|
||||
for client := range c {
|
||||
var ClientHandler = ClientMap{
|
||||
Clients: map[*Client]bool{},
|
||||
Mutex: sync.Mutex{},
|
||||
}
|
||||
|
||||
func (c *ClientMap) AddClient(client *Client) {
|
||||
c.Lock()
|
||||
c.Clients[client] = true
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *ClientMap) DeleteClient(ID string) {
|
||||
c.Lock()
|
||||
|
||||
for client := range c.Clients {
|
||||
if client.ID == ID {
|
||||
delete(c, client)
|
||||
delete(c.Clients, client)
|
||||
}
|
||||
}
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c ClientMap) FindClientsByStreamer(streamer string) []*Client {
|
||||
func (c *ClientMap) FindClientsByStreamer(streamer string) []*Client {
|
||||
c.Lock()
|
||||
|
||||
var clients []*Client
|
||||
for client := range c {
|
||||
for client := range c.Clients {
|
||||
// check if the client is following the given streamer
|
||||
for s, _ := range client.FollowingStreamers {
|
||||
if s == streamer {
|
||||
|
@ -27,5 +44,6 @@ func (c ClientMap) FindClientsByStreamer(streamer string) []*Client {
|
|||
}
|
||||
}
|
||||
}
|
||||
c.Unlock()
|
||||
return clients
|
||||
}
|
||||
|
|
|
@ -178,11 +178,13 @@ func SendMessage(msg string) error {
|
|||
|
||||
// function to check and remove unused streamers from server map
|
||||
func RemoveUnusedStreamers() {
|
||||
ClientHandler.Lock()
|
||||
for streamer := range streamersFollowing {
|
||||
found := false
|
||||
// iterate over each client and their streamer map
|
||||
for client, _ := range ClientHandler {
|
||||
if client.FollowingStreamers[streamer] == true {
|
||||
|
||||
for client, _ := range ClientHandler.Clients {
|
||||
if client.FollowingStreamers[streamer] {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
@ -193,4 +195,5 @@ func RemoveUnusedStreamers() {
|
|||
sendMessage("PART #" + streamer)
|
||||
}
|
||||
}
|
||||
ClientHandler.Unlock()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue