Just one thing to keep in mind. Likely you have more than one serve instance running to process the requests. Thus it might happen the client will poll a different server on every request. Just imagine you have servers A, B, C behind a load balance and the domain example.com. As the client is pooling example.com, the first request might reach A, the second B and the third C. Now you have the 3 servers tracking the same client. It might happen server A doesn't receive any request from the client for a while, but not because the client isn't pooling any more, but because all requests are being directed to either B or C.
On Tuesday, 17 November 2020 at 07:12:18 UTC+1 Shulhan wrote: > > > > On 16 Nov 2020, at 16.24, Afriyie Abraham Kwabena <afriyie...@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/75e53871-6821-4ef6-8eb5-a7cb9062fb53n%40googlegroups.com.