mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-07-04 10:08:31 +00:00
Refactor bunched remote command
This commit is contained in:
parent
013e49e2c5
commit
db486e4eba
2 changed files with 40 additions and 60 deletions
|
@ -356,7 +356,7 @@ type BunchSubscriberList struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var PendingBunchedRequests map[BunchedRequest]*BunchSubscriberList = make(map[BunchedRequest]*BunchSubscriberList)
|
var PendingBunchedRequests map[BunchedRequest]*BunchSubscriberList = make(map[BunchedRequest]*BunchSubscriberList)
|
||||||
var PendingBunchLock sync.RWMutex
|
var PendingBunchLock sync.Mutex
|
||||||
var CompletedBunchedRequests map[BunchedRequest]BunchedResponse = make(map[BunchedRequest]BunchedResponse)
|
var CompletedBunchedRequests map[BunchedRequest]BunchedResponse = make(map[BunchedRequest]BunchedResponse)
|
||||||
var CompletedBunchLock sync.RWMutex
|
var CompletedBunchLock sync.RWMutex
|
||||||
|
|
||||||
|
@ -374,16 +374,16 @@ func bunchingJanitor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleBunchedRemotecommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
func HandleBunchedRemoteCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||||
br := BunchedRequestFromCM(&msg)
|
br := BunchedRequestFromCM(&msg)
|
||||||
|
|
||||||
CompletedBunchLock.RLock()
|
CompletedBunchLock.RLock()
|
||||||
resp, ok := CompletedBunchedRequests[br]
|
resp, ok := CompletedBunchedRequests[br]
|
||||||
if ok && resp.Timestamp.After(time.Now().Add(-5*time.Minute)) {
|
|
||||||
CompletedBunchLock.RUnlock()
|
CompletedBunchLock.RUnlock()
|
||||||
|
|
||||||
|
if ok && resp.Timestamp.After(time.Now().Add(-5*time.Minute)) {
|
||||||
return SuccessMessageFromString(resp.Response), nil
|
return SuccessMessageFromString(resp.Response), nil
|
||||||
} else if ok {
|
} else if ok {
|
||||||
CompletedBunchLock.RUnlock()
|
|
||||||
|
|
||||||
// Entry expired, let's remove it...
|
// Entry expired, let's remove it...
|
||||||
CompletedBunchLock.Lock()
|
CompletedBunchLock.Lock()
|
||||||
|
@ -393,41 +393,24 @@ func HandleBunchedRemotecommand(conn *websocket.Conn, client *ClientInfo, msg Cl
|
||||||
delete(CompletedBunchedRequests, br)
|
delete(CompletedBunchedRequests, br)
|
||||||
}
|
}
|
||||||
CompletedBunchLock.Unlock()
|
CompletedBunchLock.Unlock()
|
||||||
} else {
|
|
||||||
CompletedBunchLock.RUnlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PendingBunchLock.Lock()
|
||||||
|
list, ok := PendingBunchedRequests[br]
|
||||||
|
if ok {
|
||||||
|
list.Lock()
|
||||||
|
AddToSliceB(&list.Members, client, msg.MessageID)
|
||||||
|
list.Unlock()
|
||||||
|
PendingBunchLock.Unlock()
|
||||||
client.MsgChannelKeepalive.Add(1)
|
client.MsgChannelKeepalive.Add(1)
|
||||||
|
|
||||||
PendingBunchLock.RLock()
|
return ClientMessage{Command: AsyncResponseCommand}, nil
|
||||||
list, ok := PendingBunchedRequests[br]
|
}
|
||||||
var needToStart bool
|
|
||||||
if ok {
|
|
||||||
list.Lock()
|
|
||||||
AddToSliceB(&list.Members, client, msg.MessageID)
|
|
||||||
list.Unlock()
|
|
||||||
PendingBunchLock.RUnlock()
|
|
||||||
|
|
||||||
return ClientMessage{Command: AsyncResponseCommand}, nil
|
|
||||||
} else {
|
|
||||||
PendingBunchLock.RUnlock()
|
|
||||||
PendingBunchLock.Lock()
|
|
||||||
// RECHECK because someone else might have added it
|
|
||||||
list, ok = PendingBunchedRequests[br]
|
|
||||||
if ok {
|
|
||||||
list.Lock()
|
|
||||||
AddToSliceB(&list.Members, client, msg.MessageID)
|
|
||||||
list.Unlock()
|
|
||||||
PendingBunchLock.Unlock()
|
|
||||||
return ClientMessage{Command: AsyncResponseCommand}, nil
|
|
||||||
} else {
|
|
||||||
PendingBunchedRequests[br] = &BunchSubscriberList{Members: []BunchSubscriber{{Client: client, MessageID: msg.MessageID}}}
|
PendingBunchedRequests[br] = &BunchSubscriberList{Members: []BunchSubscriber{{Client: client, MessageID: msg.MessageID}}}
|
||||||
needToStart = true
|
|
||||||
PendingBunchLock.Unlock()
|
PendingBunchLock.Unlock()
|
||||||
}
|
client.MsgChannelKeepalive.Add(1)
|
||||||
}
|
|
||||||
|
|
||||||
if needToStart {
|
|
||||||
go func(request BunchedRequest) {
|
go func(request BunchedRequest) {
|
||||||
resp, err := RequestRemoteDataCached(string(request.Command), request.Param, AuthInfo{})
|
resp, err := RequestRemoteDataCached(string(request.Command), request.Param, AuthInfo{})
|
||||||
|
|
||||||
|
@ -458,9 +441,6 @@ func HandleBunchedRemotecommand(conn *websocket.Conn, client *ClientInfo, msg Cl
|
||||||
}(br)
|
}(br)
|
||||||
|
|
||||||
return ClientMessage{Command: AsyncResponseCommand}, nil
|
return ClientMessage{Command: AsyncResponseCommand}, nil
|
||||||
} else {
|
|
||||||
panic("logic error")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleRemoteCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
func HandleRemoteCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||||
|
|
|
@ -36,8 +36,8 @@ var CommandHandlers = map[Command]CommandHandler{
|
||||||
"survey": HandleSurvey,
|
"survey": HandleSurvey,
|
||||||
|
|
||||||
"twitch_emote": HandleRemoteCommand,
|
"twitch_emote": HandleRemoteCommand,
|
||||||
"get_link": HandleBunchedRemotecommand,
|
"get_link": HandleBunchedRemoteCommand,
|
||||||
"get_display_name": HandleBunchedRemotecommand,
|
"get_display_name": HandleBunchedRemoteCommand,
|
||||||
"update_follow_buttons": HandleRemoteCommand,
|
"update_follow_buttons": HandleRemoteCommand,
|
||||||
"chat_history": HandleRemoteCommand,
|
"chat_history": HandleRemoteCommand,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue