From 18d76d4f55af74587f7549dad3b6aa56363102f0 Mon Sep 17 00:00:00 2001 From: David Wilkins Date: Wed, 10 Jan 2018 22:36:00 -0800 Subject: [PATCH] Fix concurrent read/write of map --- cookbook/google-app-engine/users.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cookbook/google-app-engine/users.go b/cookbook/google-app-engine/users.go index 19533e51..5ad6ff06 100644 --- a/cookbook/google-app-engine/users.go +++ b/cookbook/google-app-engine/users.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "sync" "github.com/labstack/echo" "github.com/labstack/echo/middleware" @@ -12,14 +13,18 @@ type ( ID string `json:"id"` Name string `json:"name"` } + usersMap struct { + sync.RWMutex + users map[string]user + } ) var ( - users map[string]user + usersStore usersMap ) func init() { - users = map[string]user{ + usersStore.users = map[string]user{ "1": user{ ID: "1", Name: "Wreck-It Ralph", @@ -41,14 +46,20 @@ func createUser(c echo.Context) error { if err := c.Bind(u); err != nil { return err } - users[u.ID] = *u + usersStore.Lock() + defer usersStore.Unlock() + usersStore.users[u.ID] = *u return c.JSON(http.StatusCreated, u) } func getUsers(c echo.Context) error { - return c.JSON(http.StatusOK, users) + usersStore.RLock() + defer usersStore.RUnlock() + return c.JSON(http.StatusOK, usersStore.users) } func getUser(c echo.Context) error { - return c.JSON(http.StatusOK, users[c.Param("id")]) + usersStore.RLock() + defer usersStore.RUnlock() + return c.JSON(http.StatusOK, usersStore.users[c.Param("id")]) }