> On 16 Nov 2020, at 16.24, Afriyie Abraham Kwabena <afriyie.abra...@gmail.com> 
> wrote:
> 
> Hi ,
> 
> You are right but am new to programming and currently this what i have done.
> I have an http server handler that receives the PATCH request and stores the 
> id and the current time stamp of the request.
> But my problem is how to write the worker function to do the clean up every X 
> seconds.
> 
> Code:
> 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()
> 
>         var idtime = make(map[string]string)
> 
>         delete(idtime, id) // delete if already exist 
>         idtime[id] = time.Now() // store id and time stamp in map 

You should store idtime inside the server/service type (the one that have HTTP 
handlers). For example,

----
type Server struct {
  idtime map[string]string
}

func (my *Server) UpdateData(...) {
    ...
    my.idtime[id] = time.Now()
    ...
}
----

> 
>         // how do i write a worker to the clean up after every X seconds? 
> 

Create a function that loop forever and loop the map to check the time, then 
run the function using goroutine before you start your HTTP server.

>     } else {
>         common.WriteError(response, common.ErrMethodNotAllowed)
>         return
>     }
> }
> 



-- 
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/906DFB35-7264-47B0-9E28-42E50694D8B1%40gmail.com.

Reply via email to