Skip to content

Commit

Permalink
fix: cache the instance config and api key (#1775)
Browse files Browse the repository at this point in the history
We were needlessly calling the cloud-backend for data that doesn't
change and it was causing load issues on that server at scale.

This alleviates the "slow loading" problem we've been facing when
running the authenticated EM UI in the Cloud UI.

## Description:

## Is this change user facing?
NO

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
  • Loading branch information
adschwartz authored Nov 14, 2023
1 parent efab221 commit dafe2bb
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions enclave-manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"connectrpc.com/connect"
Expand Down Expand Up @@ -38,8 +39,12 @@ type Authentication struct {
}

type WebServer struct {
instanceConfigMutex *sync.RWMutex
apiKeyMutex *sync.RWMutex
engineServiceClient *kurtosis_engine_rpc_api_bindingsconnect.EngineServiceClient
enforceAuth bool
instanceConfig *kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse
apiKey *string
}

func NewWebserver(enforceAuth bool) (*WebServer, error) {
Expand All @@ -50,6 +55,8 @@ func NewWebserver(enforceAuth bool) (*WebServer, error) {
return &WebServer{
engineServiceClient: &engineServiceClient,
enforceAuth: enforceAuth,
instanceConfigMutex: &sync.RWMutex{},
apiKeyMutex: &sync.RWMutex{},
}, nil
}

Expand Down Expand Up @@ -408,6 +415,15 @@ func (c *WebServer) GetCloudInstanceConfig(
ctx context.Context,
apiKey string,
) (*kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse, error) {
// Check if we have already fetched the instance config, if so return the cache
if c.instanceConfig != nil {
return c.instanceConfig, nil
}

// We have not yet fetched the instance configuration, so we write lock, make the external call and cache the result
c.instanceConfigMutex.Lock()
defer c.instanceConfigMutex.Unlock()

client, err := c.createKurtosisCloudBackendClient(
kurtosisCloudApiHost,
kurtosisCloudApiPort,
Expand All @@ -420,6 +436,7 @@ func (c *WebServer) GetCloudInstanceConfig(
ApiKey: apiKey,
},
}

getInstanceResponse, err := (*client).GetOrCreateInstance(ctx, getInstanceRequest)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the instance")
Expand All @@ -435,6 +452,8 @@ func (c *WebServer) GetCloudInstanceConfig(
return nil, stacktrace.Propagate(err, "Failed to get the instance config")
}

c.instanceConfig = getInstanceConfigResponse.Msg

return getInstanceConfigResponse.Msg, nil
}

Expand All @@ -454,20 +473,33 @@ func (c *WebServer) ConvertJwtTokenToApiKey(
AccessToken: jwtToken,
},
}
result, err := (*client).GetOrCreateApiKey(ctx, request)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the API key")
}

if result == nil {
// User does not have an API key (unlikely if valid jwt token)
return nil, stacktrace.NewError("User does not have an API key assigned")
}
if len(result.Msg.ApiKey) > 0 {
if c.apiKey != nil {
return &Authentication{
ApiKey: result.Msg.ApiKey,
ApiKey: *c.apiKey,
JwtToken: jwtToken,
}, nil
} else {
c.apiKeyMutex.Lock()
defer c.apiKeyMutex.Unlock()

result, err := (*client).GetOrCreateApiKey(ctx, request)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the API key")
}

if result == nil {
// User does not have an API key (not really possible if they have a valid jwt token)
return nil, stacktrace.NewError("User does not have an API key assigned")
}

if len(result.Msg.ApiKey) > 0 {
c.apiKey = &result.Msg.ApiKey
return &Authentication{
ApiKey: result.Msg.ApiKey,
JwtToken: jwtToken,
}, nil
}
}

return nil, stacktrace.NewError("an empty API key was returned from Kurtosis Cloud Backend")
Expand Down

0 comments on commit dafe2bb

Please sign in to comment.