Skip to content

Commit

Permalink
Merge pull request #5976 from GabrielBuica/private/dbuica/CP-50723-se…
Browse files Browse the repository at this point in the history
…t-last-active

CP-51352: Compare before setting a new value for `last_active`
  • Loading branch information
robhoes authored Sep 11, 2024
2 parents 0438aee + 67df157 commit 08e1437
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
31 changes: 28 additions & 3 deletions ocaml/xapi/session_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,34 @@ let check ~intra_pool_only ~session_id ~action =
if (not pool) && not (Pool_role.is_master ()) then
raise Non_master_login_on_slave ;
if Pool_role.is_master () then
Db_actions.DB_Action.Session.set_last_active ~__context
~self:session_id
~value:(Xapi_stdext_date.Date.of_float (Unix.time ()))
(* before updating the last_active field, check if the field has been
already updated recently. This avoids holding the database lock too often.*)
let n = Xapi_stdext_date.Date.now () in
let last_active =
Db_actions.DB_Action.Session.get_last_active ~__context
~self:session_id
in
let ptime_now = Xapi_stdext_date.Date.to_ptime n in
let refresh_threshold =
let last_active_ptime =
Xapi_stdext_date.Date.to_ptime last_active
in
match
Ptime.add_span last_active_ptime
!Xapi_globs.threshold_last_active
with
| None ->
let err_msg =
"Can't add the configurable threshold of last active to \
the current time."
in
raise Api_errors.(Server_error (internal_error, [err_msg]))
| Some ptime ->
ptime
in
if Ptime.is_later ptime_now ~than:refresh_threshold then
Db_actions.DB_Action.Session.set_last_active ~__context
~self:session_id ~value:n
with
| Db_exn.DBCache_NotFound (_, _, reference) ->
info
Expand Down
9 changes: 9 additions & 0 deletions ocaml/xapi/xapi_globs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ let host_assumed_dead_interval = ref Mtime.Span.(10 * min)
(* If a session has a last_active older than this we delete it *)
let inactive_session_timeout = ref 86400. (* 24 hrs in seconds *)

(* If a session was refreshed more recently than threshold_last_active do not refresh it again. *)
let threshold_last_active = ref (Ptime.Span.of_int_s 600)
(* 10 min in seconds *)

let pending_task_timeout = ref 86400. (* 24 hrs in seconds *)

let completed_task_timeout = ref 3900. (* 65 mins *)
Expand Down Expand Up @@ -1631,6 +1635,11 @@ let other_options =
, (fun () -> string_of_int !external_authentication_cache_size)
, "Specify the maximum capacity of the external authentication cache"
)
; ( "threshold_last_active"
, Arg.Int (fun t -> threshold_last_active := Ptime.Span.of_int_s t)
, (fun () -> Format.asprintf "%a" Ptime.Span.pp !threshold_last_active)
, "Specify the threshold below which we do not refresh the session"
)
]

(* The options can be set with the variable xapiflags in /etc/sysconfig/xapi.
Expand Down

0 comments on commit 08e1437

Please sign in to comment.