From 24ca5f01b5fec2f0cb221390baf3976ed4d4fc6b Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 10 Jan 2025 01:02:11 +0900 Subject: [PATCH] added ExtendLifetime --- client-ctl.go | 18 +++++++++++++++--- client.go | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/client-ctl.go b/client-ctl.go index 41eb5e5..fa07f54 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -6,6 +6,7 @@ import "net/http" import "net/url" import "runtime" import "strconv" +import "strings" import "time" import "unsafe" @@ -549,8 +550,11 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter goto oops } - - err = r.ResetLifetime(lifetime) + if strings.HasPrefix(jcr.Lifetime, "+") { + err = r.ExtendLifetime(lifetime) + } else { + err = r.ResetLifetime(lifetime) + } if err != nil { goto oops } case http.MethodDelete: @@ -649,9 +653,17 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit status_code = http.StatusBadRequest; w.WriteHeader(status_code) err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error()) goto oops + } else if lifetime < 0 { + status_code = http.StatusBadRequest; w.WriteHeader(status_code) + err = fmt.Errorf("negative lifetime value %s", jcr.Lifetime) + goto oops } - err = r.ResetLifetime(lifetime) + if strings.HasPrefix(jcr.Lifetime, "+") { + err = r.ExtendLifetime(lifetime) + } else { + err = r.ResetLifetime(lifetime) + } if err != nil { goto oops } case http.MethodDelete: diff --git a/client.go b/client.go index dfb2fa8..e97f8f7 100644 --- a/client.go +++ b/client.go @@ -266,6 +266,23 @@ func (r *ClientRoute) FindClientPeerConnById(conn_id PeerId) *ClientPeerConn { return c } +func (r *ClientRoute) ExtendLifetime(lifetime time.Duration) error { + r.lifetime_mtx.Lock() + defer r.lifetime_mtx.Unlock() + if r.lifetime_timer == nil { + // let's not support timer extend if route was not + // first started with lifetime enabled + return fmt.Errorf("prohibited operation") + } else { + var expiry time.Time + r.lifetime_timer.Stop() + r.lifetime = r.lifetime + lifetime + expiry = r.lifetime_start.Add(r.lifetime) + r.lifetime_timer.Reset(expiry.Sub(time.Now())) + return nil + } +} + func (r *ClientRoute) ResetLifetime(lifetime time.Duration) error { r.lifetime_mtx.Lock() defer r.lifetime_mtx.Unlock()