Closed yash-nisar closed 2 months ago
I have no experience using MS EntraID, but I guess the token refresh mechanism can be implemented like this:
package main
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/redis/rueidis"
"sync"
"time"
)
func getAzureToken() (azcore.AccessToken, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return azcore.AccessToken{}, err
}
token, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{...})
if err != nil {
return azcore.AccessToken{}, err
}
return token, nil
}
func main() {
var mu sync.Mutex
var token azcore.AccessToken
client, err := rueidis.NewClient(rueidis.ClientOption{
AuthCredentialsFn: func(_ rueidis.AuthCredentialsContext) (rueidis.AuthCredentials, error) {
mu.Lock()
defer mu.Unlock()
if token.ExpiresOn.Before(time.Now()) {
tk, err := getAzureToken()
if err != nil {
return rueidis.AuthCredentials{}, err
}
token = tk
}
return rueidis.AuthCredentials{Username: "...", Password: token.Token}, nil
},
})
if err != nil {
panic(err)
}
go func() {
for {
mu.Lock()
duration := time.Until(token.ExpiresOn.Add(time.Minute * -10))
mu.Unlock()
time.Sleep(duration)
tk, err := getAzureToken()
if err != nil {
continue
}
mu.Lock()
token = tk
mu.Unlock()
for _, c := range client.Nodes() {
c.Do(context.Background(), c.B().Auth().Username("...").Password(token.Token).Build())
}
}
}()
}
Thanks @rueian for your response. I wanted to ask the implications of not implementing this renewal mechanism.
Hi @yash-nisar,
client.Receive
will keep retrying to subscribe those provided channels.@rueian So, if we have 500 requests per second being sent to Azure Redis, does each request enforce a new connection ? or am I misunderstanding something here ?
@yash-nisar,
No, there will be at most 2^PipelineMultiplex connections to each redis node.
Awesome @rueian, really appreciate the prompt response. Last set of questions, I promise 😛
Awesome @rueian, really appreciate the prompt response. Last set of questions, I promise 😛
- Ok, so if that variable is set to 32, then there will be atmost 32 connections ?
- Will those 500 requests will each spin a new connection from this connection pool and release it back to the pool ?
- If we don't implement this manual renewal mechanism, do you think there will be downtime ?
Thanks @rueian, closing the issue because all questions were answered ! Really appreciate it :)
I wanted to ask if we need to implement the token refresh mechanism by ourselves if we use MS Entra for cache authentication (https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication)
This is how we want to do it. My questions are: