summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorRuben <ruben.nellen@gmail.com>2016-10-12 13:09:02 +0200
committerKurt Jung <kurt.w.jung@gmail.com>2016-10-12 07:09:02 -0400
commita8ed28c5f45a8bb6c60cdc76464c34ccecf7b7a7 (patch)
tree30e591d2a36ee6988e6196410df74182b6e5c257 /contrib
parent34e60518f28a6283996284f9b909bc9515c67c79 (diff)
added mutex to barcode cache (#85)
to prevent race condition
Diffstat (limited to 'contrib')
-rw-r--r--contrib/barcode/barcode.go21
1 files changed, 15 insertions, 6 deletions
diff --git a/contrib/barcode/barcode.go b/contrib/barcode/barcode.go
index b2ba2c2..8fb371e 100644
--- a/contrib/barcode/barcode.go
+++ b/contrib/barcode/barcode.go
@@ -23,7 +23,7 @@ import (
"image/jpeg"
"io"
"strconv"
-
+ "sync"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/codabar"
"github.com/boombuler/barcode/code128"
@@ -38,7 +38,11 @@ import (
// barcodes represents the barcodes that have been registered through this
// package. They will later be used to be scaled and put on the page.
-var barcodes map[string]barcode.Barcode
+// RubenN: made this a struct with a mutex to prevent race condition
+var barcodes struct {
+ sync.Mutex
+ cache map[string]barcode.Barcode
+}
// barcodePdf is a partial PDF implementation that only implements a subset of
// functions that are required to add the barcode to the PDF.
@@ -56,7 +60,9 @@ type barcodePdf interface {
//
// Positioning with x, y and flow is inherited from Fpdf.Image().
func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) {
- unscaled, ok := barcodes[code]
+ barcodes.Lock()
+ unscaled, ok := barcodes.cache[code]
+ barcodes.Unlock()
if !ok {
err := errors.New("Barcode not found")
@@ -92,12 +98,15 @@ func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) {
// Register registers a barcode but does not put it on the page. Use Barcode()
// with the same code to put the barcode on the PDF page.
func Register(bcode barcode.Barcode) string {
- if len(barcodes) == 0 {
- barcodes = make(map[string]barcode.Barcode)
+ barcodes.Lock()
+ if len(barcodes.cache) == 0 {
+ barcodes.cache = make(map[string]barcode.Barcode)
}
key := barcodeKey(bcode)
- barcodes[key] = bcode
+ barcodes.cache[key] = bcode
+ barcodes.Unlock()
+
return key
}