Skip to content

Commit

Permalink
add mutex lock to limit requests to bb to 1 at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
trek-boldly-go committed Feb 5, 2024
1 parent ac357b4 commit cb0d833
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion imessage/bluebubbles/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"
"unicode"

Expand Down Expand Up @@ -56,6 +57,8 @@ type blueBubbles struct {
contactsLastRefresh time.Time
contacts []Contact

bbRequestLock sync.Mutex

usingPrivateApi bool
}

Expand Down Expand Up @@ -239,7 +242,7 @@ func (bb *blueBubbles) handleNewMessage(rawMessage json.RawMessage) (err error)

func (bb *blueBubbles) handleMessageSendError(rawMessage json.RawMessage) (err error) {
bb.log.Trace().RawJSON("rawMessage", rawMessage).Msg("handleMessageSendError")
return ErrNotImplemented
return nil // beeper should get the error back during the send response
}

func (bb *blueBubbles) handleMessageUpdated(rawMessage json.RawMessage) (err error) {
Expand Down Expand Up @@ -1217,7 +1220,9 @@ func (bb *blueBubbles) apiUrl(path string, queryParams map[string]string) string
func (bb *blueBubbles) apiGet(path string, queryParams map[string]string, target interface{}) (err error) {
url := bb.apiUrl(path, queryParams)

bb.bbRequestLock.Lock()
response, err := http.Get(url)
bb.bbRequestLock.Unlock()
if err != nil {
bb.log.Error().Err(err).Msg("Error making GET request")
return err
Expand Down Expand Up @@ -1267,7 +1272,9 @@ func (bb *blueBubbles) apiRequest(method, path string, payload interface{}, targ
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
bb.bbRequestLock.Lock()
response, err := client.Do(req)
bb.bbRequestLock.Unlock()
if err != nil {
bb.log.Error().Err(err).Str("method", method).Msg("Error making request")
return err
Expand Down Expand Up @@ -1321,7 +1328,9 @@ func (bb *blueBubbles) apiPostAsFormData(path string, formData map[string]interf
writer.Close()

// Make the HTTP POST request
bb.bbRequestLock.Lock()
response, err := http.Post(url, writer.FormDataContentType(), &body)
bb.bbRequestLock.Unlock()
if err != nil {
bb.log.Error().Err(err).Msg("Error making POST request")
return err
Expand Down

0 comments on commit cb0d833

Please sign in to comment.