Incoming should be working, but I haven't tested it too much.
This commit is contained in:
parent
d302f39719
commit
309b836931
8 changed files with 259 additions and 98 deletions
|
@ -42,9 +42,15 @@ func Request(body map[string]any) (responseJSON string, err error) {
|
|||
waitingJobs[id] = nil;
|
||||
waitingJobsMutex.Unlock();
|
||||
|
||||
// Wait for request to finish
|
||||
for waitingJobs[id] == nil {
|
||||
// Wait for request to return
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
waitingJobsMutex.RLock();
|
||||
if waitingJobs[id] != nil {
|
||||
waitingJobsMutex.RUnlock();
|
||||
break;
|
||||
}
|
||||
waitingJobsMutex.RUnlock();
|
||||
}
|
||||
|
||||
// Lock job when dequeueing
|
||||
|
@ -58,26 +64,23 @@ func Request(body map[string]any) (responseJSON string, err error) {
|
|||
}
|
||||
|
||||
/* Handles putting a response into waitingJobs. Returns false on error */
|
||||
func Response(body string) {
|
||||
var unmarshaledJSON any;
|
||||
json.Unmarshal([]byte(body), &unmarshaledJSON);
|
||||
unmarshaledJSONMap, ok := unmarshaledJSON.(map[string]any)
|
||||
if !ok {return}
|
||||
|
||||
func handleResponse(body string, unmarshaledJSONMap map[string]any) {
|
||||
val, ok := unmarshaledJSONMap["id"];
|
||||
if !ok {return}
|
||||
id, ok := val.(string)
|
||||
if !ok {return}
|
||||
|
||||
|
||||
// Write response into
|
||||
// Read-Write lock the mutex
|
||||
waitingJobsMutex.Lock();
|
||||
defer waitingJobsMutex.Unlock();
|
||||
|
||||
// Skip storage if there isn't a request for this ID
|
||||
if _, ok := waitingJobs[id]; !ok {return}
|
||||
// Store response in waiting Jobs
|
||||
waitingJobs[id] = &body;
|
||||
waitingJobsMutex.Unlock();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Helper function to generate a random ID */
|
||||
func genID() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {return ""}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue