Hi,

Am experiancing 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?

func PollingWorker() {
    tiker := time.NewTicker(60 * time.Second)
    for _ = range tiker.C {
        mu.Lock()
        for keyid := range idtime {
            d := time.Now().Unix() - idtime[keyid]
            if d >= 60 {
                // Find nfinstance using keyid
                nfinstanceProfile, err := 
config.Conf.FindNFInstanceProfileById(ctx, keyid)
                if err != nil {
                    config.Logrus.Error("Could not find NF Instance Profile 
with ID :" + keyid + " to SUSPEND Status")
                }
                // change nfStatus of nfinstance to SUSPENDED
                err = config.Conf.SuspendNFInstanceNfStatus(ctx, 
nfinstanceProfile, keyid)
                if err != nil {
                    config.Logrus.Error(err)
                }
                delete(idtime, keyid)
            }
        }
        mu.Unlock()
    }
}


BR
Abraham
On Wednesday, November 18, 2020 at 10:51:49 AM UTC+2 Afriyie Abraham 
Kwabena wrote:

> Hi Sulhan,  Anderson,
>
> Thanks for your guidance. It works now using time.Ticker
>
> func worker() {
>     tiker := time.NewTicker(30 * time.Second)
>     for _ = range tiker.C {
>
>         mu.Lock()
>         for keyid := range idtime {
>
>             d := time.Now().Unix() - idtime[keyid]
>             if d >= 60 {
>                 // delete resouce in database after 60 seconds
>                 _ = DeleteNFInstance(ctx, keyid)
>             }
>         }
>         mu.Unlock()
>     }
> }
>
> BR
> Abraham
>
>
>
> On Tuesday, November 17, 2020 at 9:41:01 PM UTC+2 Shulhan wrote:
>
>>
>>
>> > On 18 Nov 2020, at 01.06, Afriyie Abraham Kwabena <afriyie...@gmail.com> 
>> wrote: 
>> > 
>> > Hi, 
>> > 
>> > The UpdateData function is the HTTP handler for the route which matches 
>> the URL and is called after the mux.Router after receiving an incoming 
>> request matches the incoming request 
>> > against the registered route. 
>>
>> ... 
>>
>> > var idtime = make(map[string]int64) 
>> > 
>> > 
>> > func UpdateData(response http.ResponseWriter, request *http.Request) { 
>> > 
>> > var ( 
>> > localVarHTTPMethod = http.MethodPatch 
>> > patchItems model.PatchItem 
>> > ) 
>> > 
>> > id := config.GetIdFromRequest(request) 
>> > 
>> > if request.Method == localVarHTTPMethod { 
>> > 
>> > err := json.NewDecoder(request.Body).Decode(&patchItems) 
>> > if err != nil { 
>> > common.WriteError(response, common.ErrBadRequest) 
>> > return 
>> > } 
>> > 
>> > defer request.Body.Close() 
>> > 
>> > idtime[id] = time.Now().Unix() 
>> > 
>>
>> We still may have data race here. 
>>
>> > 
>> > func worker() { 
>> > mu.Lock() 
>> > for keyid := range idtime { 
>> > 
>> > d := time.Now().Unix() - idtime[keyid] 
>> > if d >= 60 { 
>> > 
>> > // delete resouce in database after 60 seconds 
>> > _ = DeleteNFInstance(ctx, keyid) 
>> > } 
>> > } 
>> > mu.Unlock() 
>> > } 
>> > 
>>
>> ... 
>>
>> > // main function 
>> > func main() { 
>> > r := NewRouter() 
>> > 
>> > go worker() 
>> > 
>> > 
>> > fmt.Println("Start listening") 
>> > fmt.Println(http.ListenAndServe(":8080", r)) 
>> > } 
>> > 
>> > I appreciate your help but am still not able to it work. 
>> > 
>>
>> Looks like your worker only loop once and then it finished. Either you 
>> use time.Sleep() to repeat the loop inside loop or use time.Ticker [1]. 
>>
>> [1] https://pkg.go.dev/time#Ticker 
>>
>>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/9db5a074-0b0a-4f3a-ab20-7c5d67f458a0n%40googlegroups.com.

Reply via email to