Hi,
Yes from the warning it prints the handler function below and after google
searching what i did was to mutex.Lock and Unlock the handler function and
no warning again.
However, my google search people mention of using channels instead of
mutex. What I would like to ask is, using mutex OK and if not the best way
of solving it, how can i use
channels in this case.
Handler function below:
func UpdateNFInstance(response http.ResponseWriter, request *http.Request) {
mu.Lock()
defer mu.Unlock()
var (
localVarHTTPMethod = http.MethodPatch
ctx = context.Background()
patchItems model.PatchItem
)
id := config.GetIdFromRequest(request)
if request.Method == localVarHTTPMethod {
if request.Header.Get("Content-Type") !=
"application/json-patch+json" {
common.WriteError(response,
common.ErrStatusUnsupportedMediaTypePatch)
return
}
err := json.NewDecoder(request.Body).Decode(&patchItems)
if err != nil {
common.WriteError(response, common.ErrBadRequest)
return
}
defer request.Body.Close()
delete(idtime, id) // delete value in map if exit
idtime[id] = time.Now().Unix()
nfProfile, err := config.Conf.FindNFInstanceProfileById(ctx, id)
if err != nil {
common.WriteError(response, common.ErrNotFound)
return
}
if patchItems.Path == "/nfStatus" {
nfProfile.NfStatus = patchItems.Value
nfProfile.LoadTimeStamp = time.Now().String()
success := config.Conf.UpdateNFInstanceHeartBeatNfStatus(ctx,
nfProfile, id)
if !success {
common.WriteError(response, common.ErrInternalServer)
return
}
if request.URL.Scheme != "" {
scheme = request.URL.Scheme
}
response.Header().Set("Content-Location",
scheme+"://"+request.Host+request.URL.Path)
response.Header().Set("Response-Desc", "Success")
common.RespondWith3gppJSONPatchJSON(response,
http.StatusNoContent, nil)
} else {
// patchItems.Path == "/load" information
v, err := strconv.Atoi(patchItems.Value)
if err != nil {
config.Logrus.Errorf("Failed to convert Patch Item Value
string to integer: %s", err.Error())
}
nfProfile.Load = int32(v)
nfProfile.LoadTimeStamp = time.Now().String()
success := config.Conf.UpdateNFInstanceHeartBeatLoad(ctx,
nfProfile, id)
if !success {
common.WriteError(response, common.ErrInternalServer)
return
}
if request.URL.Scheme != "" {
scheme = request.URL.Scheme
}
response.Header().Set("Content-Location",
scheme+"://"+request.Host+request.URL.Path)
response.Header().Set("Response-Desc", "Success")
common.RespondWith3gppJSONPatchJSON(response,
http.StatusNoContent, nil)
}
} else {
common.WriteError(response, common.ErrMethodNotAllowed)
return
}
}
BR
Abraham
On Friday, November 27, 2020 at 6:38:52 AM UTC+2 Shulhan wrote:
>
>
> > On 27 Nov 2020, at 07.06, Afriyie Abraham Kwabena <[email protected]>
> wrote:
> >
> > Hi,
> >
> > Am experiencing data race warning with my pollingworker function below
> even though when i build with the -race flag i do not get any error. Any
> help?
> >
>
> Usually when you got data race warning it will print the line that cause
> read and write race condition. Pay attention to both lines and I think you
> will find the cause.
>
> It's kind of hard to figure it out only through a single function. It
> could be in config.Conf or in function that manipulate idtime outside the
> pollingworker.
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/9fae8852-b60d-4c76-9939-b997db3d685cn%40googlegroups.com.