Browse Source

few minor update to make code simpler & thread safe

juliendsv 2 years ago
parent
commit
78fe5afcd5

+ 1 - 2
chapter7/controllers/controller.go

@@ -7,10 +7,9 @@ type Controller struct {
 
 // New is a Controller 'constructor'
 func New(storage Storage) *Controller {
-	c := Controller{
+	return &Controller{
 		storage: storage,
 	}
-	return &c
 }
 
 // Payload is our common response

+ 7 - 1
chapter7/grpcjson/internal/keyvalue.go

@@ -2,6 +2,7 @@ package internal
 
 import (
 	"golang.org/x/net/context"
+	"sync"
 
 	"github.com/agtorre/go-cookbook/chapter7/grpcjson/keyvalue"
 	"google.golang.org/grpc"
@@ -10,7 +11,8 @@ import (
 
 // KeyValue is a struct that holds a map
 type KeyValue struct {
-	m map[string]string
+	mutex sync.RWMutex
+	m     map[string]string
 }
 
 // NewKeyValue initializes the map and controller
@@ -22,13 +24,17 @@ func NewKeyValue() *KeyValue {
 
 // Set sets a value to a key, then returns the value
 func (k *KeyValue) Set(ctx context.Context, r *keyvalue.SetKeyValueRequest) (*keyvalue.KeyValueResponse, error) {
+	k.mutex.Lock()
 	k.m[r.GetKey()] = r.GetValue()
+	k.mutex.Unlock()
 	return &keyvalue.KeyValueResponse{Value: r.GetValue()}, nil
 }
 
 // Get gets a value given a key, or say not found if
 // it doesn't exist
 func (k *KeyValue) Get(ctx context.Context, r *keyvalue.GetKeyValueRequest) (*keyvalue.KeyValueResponse, error) {
+	k.mutex.RLock()
+	defer k.mutex.RUnlock()
 	val, ok := k.m[r.GetKey()]
 	if !ok {
 		return nil, grpc.Errorf(codes.NotFound, "key not set")

+ 1 - 1
chapter7/handlers/get.go

@@ -9,7 +9,7 @@ import (
 // with Hello <name>! in plaintext
 func HelloHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "text/plain")
-	if r.Method != "GET" {
+	if r.Method != http.MethodGet {
 		w.WriteHeader(http.StatusMethodNotAllowed)
 		return
 	}

+ 1 - 1
chapter7/handlers/post.go

@@ -20,7 +20,7 @@ type GreetingResponse struct {
 // or a useful payload
 func GreetingHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/json")
-	if r.Method != "POST" {
+	if r.Method != http.MethodPost {
 		w.WriteHeader(http.StatusMethodNotAllowed)
 		return
 	}

+ 1 - 3
chapter7/negotiate/negotiate.go

@@ -18,10 +18,8 @@ type Negotiator struct {
 func GetNegotiator(r *http.Request) *Negotiator {
 	contentType := r.Header.Get("Content-Type")
 
-	n := Negotiator{
+	return &Negotiator{
 		ContentType: contentType,
 		Render:      render.New(),
 	}
-
-	return &n
 }

+ 1 - 2
chapter7/validation/controller.go

@@ -8,8 +8,7 @@ type Controller struct {
 // New initializes a controller with our
 // local validation, it can be overwritten
 func New() *Controller {
-	c := Controller{
+	return &Controller{
 		ValidatePayload: ValidatePayload,
 	}
-	return &c
 }

+ 1 - 1
chapter7/validation/process.go

@@ -8,7 +8,7 @@ import (
 
 // Process is a handler that validates a post payload
 func (c *Controller) Process(w http.ResponseWriter, r *http.Request) {
-	if r.Method != "POST" {
+	if r.Method != http.MethodPost {
 		w.WriteHeader(http.StatusMethodNotAllowed)
 		return
 	}