Incoming should be working, but I haven't tested it too much.

This commit is contained in:
Ben 2025-08-01 00:44:37 -07:00
parent d302f39719
commit 309b836931
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
8 changed files with 259 additions and 98 deletions

View file

@ -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 ""}