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 

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

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

BR
Abraham



On Sunday, November 15, 2020 at 9:01:27 AM UTC+2 Shulhan wrote:

>
> > On 14 Nov 2020, at 20.06, Afriyie Abraham Kwabena <afriyie...@gmail.com> 
> wrote:
> > 
> > Hi,
> > 
> > My question is about multiple HTTP client polling an HTTP server 
> randomly with a PATCH request to update a resource at the server running in 
> front of MongoDB. The clients poll with their different ids and a valid 
> time in their request. At the server, I can keep their ids and their 
> different times. 
> > How can detect at the server if a specific client stop polling. I would 
> like to write a function that detect at the HTTP server if a specific 
> client stop polling and then remove its resources after some time from a 
> database. Am using gorilla/mux package for the server. Any help, am new to 
> Golang programming. 
> > 
>
> I think this is not Go specific, but more like engineering in general.
>
> My suggestion is you can store the last time when user request to PATCH 
> endpoint in some storage or internal memory (like map[userID]timestamp) and 
> then have some worker that check the timestamp to clean it up every X 
> seconds or minutes.

-- 
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/7798fe2c-d176-4514-8fe0-04b6e19f4399n%40googlegroups.com.

Reply via email to