Add mutexes around saving the files
This commit is contained in:
parent
bd733bac71
commit
35d8d0d8d8
|
@ -13,6 +13,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,8 +44,11 @@ type Stat struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type subscriptionHandler struct {
|
type subscriptionHandler struct {
|
||||||
|
LockSubs sync.Mutex
|
||||||
Subscribers map[string][]Subscriber
|
Subscribers map[string][]Subscriber
|
||||||
Stats map[string]Stat
|
|
||||||
|
LockStats sync.Mutex
|
||||||
|
Stats map[string]Stat
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http.Request) error {
|
func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
@ -147,6 +151,7 @@ func (handler *subscriptionHandler) handleUnsubscription(w http.ResponseWriter,
|
||||||
|
|
||||||
func (handler *subscriptionHandler) handleSubscription(w http.ResponseWriter, r *http.Request) error {
|
func (handler *subscriptionHandler) handleSubscription(w http.ResponseWriter, r *http.Request) error {
|
||||||
log.Printf("subscription request received: %s %#v\n", r.URL.String(), r.Form)
|
log.Printf("subscription request received: %s %#v\n", r.URL.String(), r.Form)
|
||||||
|
|
||||||
callback := r.Form.Get("hub.callback")
|
callback := r.Form.Get("hub.callback")
|
||||||
topic := r.Form.Get("hub.topic")
|
topic := r.Form.Get("hub.topic")
|
||||||
secret := r.Form.Get("hub.secret")
|
secret := r.Form.Get("hub.secret")
|
||||||
|
@ -319,6 +324,9 @@ func (handler *subscriptionHandler) loadStats() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *subscriptionHandler) loadSubscriptions() error {
|
func (handler *subscriptionHandler) loadSubscriptions() error {
|
||||||
|
handler.LockStats.Lock()
|
||||||
|
defer handler.LockStats.Unlock()
|
||||||
|
|
||||||
file, err := os.Open("./subscription.json")
|
file, err := os.Open("./subscription.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
|
@ -343,6 +351,8 @@ func (handler *subscriptionHandler) load() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *subscriptionHandler) saveStats() error {
|
func (handler *subscriptionHandler) saveStats() error {
|
||||||
|
handler.LockStats.Lock()
|
||||||
|
defer handler.LockStats.Unlock()
|
||||||
file, err := os.Create("./stats.json")
|
file, err := os.Create("./stats.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -355,6 +365,9 @@ func (handler *subscriptionHandler) saveStats() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *subscriptionHandler) saveSubscriptions() error {
|
func (handler *subscriptionHandler) saveSubscriptions() error {
|
||||||
|
handler.LockSubs.Lock()
|
||||||
|
defer handler.LockSubs.Unlock()
|
||||||
|
|
||||||
file, err := os.Create("./subscription.json")
|
file, err := os.Create("./subscription.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user