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/cc7b885f-074a-4b23-9f4c-08d4a3a7ac62n%40googlegroups.com.