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() {
|
func (c *Client) Close() {
|
||||||
close(c.send)
|
_, ok := <-c.send
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
close(c.send)
|
||||||
|
}
|
||||||
c.Conn.Close()
|
c.Conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,12 @@ import (
|
||||||
|
|
||||||
func ClientMessageHandler(client *Client, msg string) {
|
func ClientMessageHandler(client *Client, msg string) {
|
||||||
splitMsg := strings.Split(msg, " ")
|
splitMsg := strings.Split(msg, " ")
|
||||||
|
|
||||||
if len(splitMsg) == 2 && splitMsg[0] == "JOIN" {
|
if len(splitMsg) == 2 && splitMsg[0] == "JOIN" {
|
||||||
client.send <- "OK"
|
client.send <- "OK"
|
||||||
client.FollowingStreamers[splitMsg[1]] = true
|
client.FollowingStreamers[splitMsg[1]] = true
|
||||||
FollowStreamer(splitMsg[1])
|
FollowStreamer(splitMsg[1])
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
client.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
client.send <- "Invalid request"
|
|
||||||
client.Close()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,41 @@
|
||||||
package chat
|
package chat
|
||||||
|
|
||||||
type ClientMap map[*Client]bool
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
var ClientHandler = ClientMap{}
|
type ClientMap struct {
|
||||||
|
Clients map[*Client]bool
|
||||||
func (c ClientMap) AddClient(client *Client) {
|
sync.Mutex
|
||||||
c[client] = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ClientMap) DeleteClient(ID string) {
|
var ClientHandler = ClientMap{
|
||||||
for client := range c {
|
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 {
|
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
|
var clients []*Client
|
||||||
for client := range c {
|
for client := range c.Clients {
|
||||||
// check if the client is following the given streamer
|
// check if the client is following the given streamer
|
||||||
for s, _ := range client.FollowingStreamers {
|
for s, _ := range client.FollowingStreamers {
|
||||||
if s == streamer {
|
if s == streamer {
|
||||||
|
@ -27,5 +44,6 @@ func (c ClientMap) FindClientsByStreamer(streamer string) []*Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.Unlock()
|
||||||
return clients
|
return clients
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,11 +178,13 @@ func SendMessage(msg string) error {
|
||||||
|
|
||||||
// function to check and remove unused streamers from server map
|
// function to check and remove unused streamers from server map
|
||||||
func RemoveUnusedStreamers() {
|
func RemoveUnusedStreamers() {
|
||||||
|
ClientHandler.Lock()
|
||||||
for streamer := range streamersFollowing {
|
for streamer := range streamersFollowing {
|
||||||
found := false
|
found := false
|
||||||
// iterate over each client and their streamer map
|
// 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
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -193,4 +195,5 @@ func RemoveUnusedStreamers() {
|
||||||
sendMessage("PART #" + streamer)
|
sendMessage("PART #" + streamer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ClientHandler.Unlock()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue