> On 18 Nov 2020, at 01.06, Afriyie Abraham Kwabena <afriyie.abra...@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/B0663F2A-D51D-43D5-8D63-95C73B51DF90%40gmail.com.

Reply via email to