Skip to content

Commit

Permalink
Fix concurrency issue in http sender
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrin committed Jun 7, 2022
1 parent 44b0150 commit a7d905b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sender/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/Scrin/ruuvi-go-gateway/config"
Expand All @@ -30,6 +31,7 @@ type httpMessageTag struct {
Data string `json:"data"`
}

var lock sync.Mutex
var tags map[string]httpMessageTag = make(map[string]httpMessageTag)
var httpClient http.Client

Expand All @@ -54,7 +56,9 @@ func SetupHTTP(conf config.HTTP, gwMac string) {
GwMac: gwMac,
Tags: tags,
}}
lock.Lock()
data, err := json.Marshal(msg)
lock.Unlock()
if err != nil {
log.WithError(err).Error("Failed to serialize data")
}
Expand Down Expand Up @@ -83,5 +87,7 @@ func SendHTTP(conf config.HTTP, adv ble.Advertisement) {
Timestamp: fmt.Sprint(time.Now().Unix()),
Data: fmt.Sprintf("0201%X%XFF%X", flags, len(data)+1, data),
}
lock.Lock()
tags[mac] = tag
lock.Unlock()
}

0 comments on commit a7d905b

Please sign in to comment.