HI,
This is what I have tried so far but am not able to get the time difference
in seconds. I mean the time differences between the time stored in the
map[string]time.Time and
the current time in seconds.
code:
type Server struct {
idtime map[string]time.Time
}
var my = Server{}
func main() {
r := mux.NewRouter()
usersData := r.PathPrefix("/users").Subrouter()
usersData.Path("/{id}").Methods(http.MethodPatch).HandlerFunc(UpdateData)
fmt.Println("Start listening")
fmt.Println(http.ListenAndServe(":8080", r))
}
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()
my.idtime = make(map[string]time.Time)
my.idtime[id] = time.Now()
go func() {
for keyid, t := range my.idtime {
ts := t.Format(time.RFC3339)
v, err := time.Parse(time.RFC3339, ts)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
timeRemaining := getTimeRemaining(v)
if timeRemaining.S >= 60 {
// delete resouce in database after 60 seconds
deleteResourceUsingIdkey(keyid)
}
}
}()
common.RespondWith3gppJsonPatchJson(response, http.StatusNoContent,
nil)
} else {
common.WriteError(response, common.ErrMethodNotAllowed)
return
}
}
type count struct {
S int
}
func getTimeRemaining(t time.Time) count {
currentTime := time.Now()
difference := t.Sub(currentTime)
seconds := int(difference.Seconds())
return count{
S: seconds,
}
}
func deleteResourceUsingIdkey(idkey string) {
// do delete here
}
Any help.
On Tuesday, November 17, 2020 at 10:34:39 AM UTC+2 Anderson Queiroz wrote:
> 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 <[email protected]>
>> 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/4335869e-3084-4742-aafd-cf7b2fe7fac1n%40googlegroups.com.