Skip to content

Commit

Permalink
Make transactions race safe
Browse files Browse the repository at this point in the history
  • Loading branch information
mpl committed Aug 27, 2024
1 parent 04effca commit 615a492
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 0 additions & 2 deletions notecard/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/blues/note-go v1.7.1 h1:Qj6FKTkn+rPsXcj0Q2j/Mqsu07PE0/mwUvmEgtmegXc=
github.com/blues/note-go v1.7.1/go.mod h1:GfslvbmFus7z05P1YykcbMedTKTuDNTf8ryBb1Qjq/4=
github.com/blues/note-go v1.7.2 h1:hasFsMNTnvyp5MPpnhqL46LC1tqzxQ5sGg+NOcJUV8E=
github.com/blues/note-go v1.7.2/go.mod h1:GfslvbmFus7z05P1YykcbMedTKTuDNTf8ryBb1Qjq/4=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
Expand Down
9 changes: 8 additions & 1 deletion notecard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strconv"
"sync"

"github.com/blues/note-go/notecard"
)
Expand All @@ -20,7 +21,10 @@ const (
)

type server struct {
card *notecard.Context
// guards transaction
muCard sync.Mutex
card *notecard.Context

initError error
}

Expand Down Expand Up @@ -62,11 +66,14 @@ func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Printf("notecard request: %s", body)
}

s.muCard.Lock()
note_rsp, err := s.card.TransactionJSON(body)
if err != nil {
handleError(w, err, "while performing a card transaction")
s.muCard.Unlock()
return
}
s.muCard.Unlock()

if debug {
log.Printf("notecard response: %s", note_rsp)
Expand Down

0 comments on commit 615a492

Please sign in to comment.