diff --git a/ocaml/xapi/session_check.ml b/ocaml/xapi/session_check.ml index 27812fc5244..63ac8fe6e95 100644 --- a/ocaml/xapi/session_check.ml +++ b/ocaml/xapi/session_check.ml @@ -53,9 +53,33 @@ 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 (Ptime.Span.of_int_s 600) + 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