I am writing a REST API. In this API I am using a bearer token. This token has a longer expiry time. i.e., several minutes and should be reused until it expires. My REST API may get called concurently from several clients. Whenver, the totken is close to expiry, I need to generate again and store it in a global variable.
Requirement is to delay a function call or fail, when token is being generated. I came across the following topic. Wanted to know whether there is a better method something like mutex in C.
Here is my pseduo code:
// lock
if (isset($this->token) == false || ($this->token_expiry_time - time()) < 60) {
$this->token = $this->generateToken()
}
// unlock
//use token