mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-07-04 01:58: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 PendingBunchLock sync.RWMutex
|
||||
var PendingBunchLock sync.Mutex
|
||||
var CompletedBunchedRequests map[BunchedRequest]BunchedResponse = make(map[BunchedRequest]BunchedResponse)
|
||||
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)
|
||||
|
||||
CompletedBunchLock.RLock()
|
||||
resp, ok := CompletedBunchedRequests[br]
|
||||
if ok && resp.Timestamp.After(time.Now().Add(-5*time.Minute)) {
|
||||
CompletedBunchLock.RUnlock()
|
||||
|
||||
if ok && resp.Timestamp.After(time.Now().Add(-5*time.Minute)) {
|
||||
return SuccessMessageFromString(resp.Response), nil
|
||||
} else if ok {
|
||||
CompletedBunchLock.RUnlock()
|
||||
|
||||
// Entry expired, let's remove it...
|
||||
CompletedBunchLock.Lock()
|
||||
|
@ -393,41 +393,24 @@ func HandleBunchedRemotecommand(conn *websocket.Conn, client *ClientInfo, msg Cl
|
|||
delete(CompletedBunchedRequests, br)
|
||||
}
|
||||
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)
|
||||
|
||||
PendingBunchLock.RLock()
|
||||
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
|
||||
}
|
||||
|
||||
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}}}
|
||||
needToStart = true
|
||||
PendingBunchLock.Unlock()
|
||||
}
|
||||
}
|
||||
client.MsgChannelKeepalive.Add(1)
|
||||
|
||||
if needToStart {
|
||||
go func(request BunchedRequest) {
|
||||
resp, err := RequestRemoteDataCached(string(request.Command), request.Param, AuthInfo{})
|
||||
|
||||
|
@ -458,9 +441,6 @@ func HandleBunchedRemotecommand(conn *websocket.Conn, client *ClientInfo, msg Cl
|
|||
}(br)
|
||||
|
||||
return ClientMessage{Command: AsyncResponseCommand}, nil
|
||||
} else {
|
||||
panic("logic 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,
|
||||
|
||||
"twitch_emote": HandleRemoteCommand,
|
||||
"get_link": HandleBunchedRemotecommand,
|
||||
"get_display_name": HandleBunchedRemotecommand,
|
||||
"get_link": HandleBunchedRemoteCommand,
|
||||
"get_display_name": HandleBunchedRemoteCommand,
|
||||
"update_follow_buttons": HandleRemoteCommand,
|
||||
"chat_history": HandleRemoteCommand,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue