Skip to content

Commit

Permalink
chore: grpc configuration (momentohq#35)
Browse files Browse the repository at this point in the history
* feat: add transport strategy / grpc config to configuration

This commit attempts to bulid out the first few bits of the configuration
API. It creates a module for transport strategy and grpc config,
and adds them to the configuration struct.

---------

Co-authored-by: Chris Price <[email protected]>
  • Loading branch information
nand4011 and cprice404 authored Jun 16, 2023
1 parent 1ba0d44 commit b6d3416
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
14 changes: 12 additions & 2 deletions src/integration-test/momento/integration_test_utils.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ defmodule Momento.IntegrationTestUtils do
use ExUnit.Case

alias Momento.CacheClient
alias Momento.Configuration
alias Momento.Config.Transport.GrpcConfiguration
alias Momento.Config.Transport.TransportStrategy
alias Momento.Config.Configuration
alias Momento.Auth.CredentialProvider

def initialize_cache_client() do
Expand All @@ -13,7 +15,15 @@ defmodule Momento.IntegrationTestUtils do
end

credential_provider = CredentialProvider.from_env_var!("TEST_AUTH_TOKEN")
config = %Configuration{}

config = %Configuration{
transport_strategy: %TransportStrategy{
grpc_config: %GrpcConfiguration{
deadline_millis: 5000
}
}
}

cache_client = CacheClient.create!(config, credential_provider)
[cache_name: cache_name, cache_client: cache_client]
end
Expand Down
4 changes: 2 additions & 2 deletions src/lib/momento/cache_client.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Momento.CacheClient do
alias Momento.Auth.CredentialProvider
alias Momento.Configuration
alias Momento.Internal.ScsControlClient
alias Momento.Internal.ScsDataClient
alias Momento.Config.Configuration, as: Configuration

require Logger

Expand Down Expand Up @@ -43,7 +43,7 @@ defmodule Momento.CacheClient do
- A `%Momento.CacheClient{}` struct representing the connected client.
"""
@spec create!(
config :: Momento.Configuration.t(),
config :: Configuration.t(),
credential_provider :: CredentialProvider.t()
) :: t()
def create!(config, credential_provider) do
Expand Down
8 changes: 0 additions & 8 deletions src/lib/momento/config.ex

This file was deleted.

19 changes: 19 additions & 0 deletions src/lib/momento/config/configuration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Momento.Config.Configuration do
@moduledoc """
Configuration for Momento CacheClient
"""
@enforce_keys[:transport_strategy]
defstruct [:transport_strategy]

@opaque t() :: %__MODULE__{
transport_strategy: Momento.Config.Transport.TransportStrategy.t()
}

@spec with_transport_strategy(
config :: Momento.Config.Configuration.t(),
transport_strategy :: Momento.Config.Transport.TransportStrategy.t()
) :: Momento.Config.Configuration.t()
def with_transport_strategy(config, transport_strategy) do
%{config | transport_strategy: transport_strategy}
end
end
11 changes: 11 additions & 0 deletions src/lib/momento/config/transport/grpc_configuration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Momento.Config.Transport.GrpcConfiguration do
@moduledoc """
Encapsulates gRPC configuration tunables.
"""
@enforce_keys [:deadline_millis]
defstruct [:deadline_millis]

@opaque t() :: %__MODULE__{
deadline_millis: number()
}
end
27 changes: 27 additions & 0 deletions src/lib/momento/config/transport/transport_strategy.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Momento.Config.Transport.TransportStrategy do
alias Momento.Config.Transport.TransportStrategy, as: TransportStrategy
alias Momento.Config.Transport.GrpcConfiguration, as: GrpcConfiguration

@moduledoc """
Configuration for the low-level Momento transport layer
"""
@enforce_keys [:grpc_config]
defstruct [:grpc_config]

@opaque t() :: %__MODULE__{
grpc_config: GrpcConfiguration.t()
}

@doc """
Copy constructor for overriding the gRPC configuration
"""
@spec with_grpc_config(
transport_strategy :: TransportStrategy.t(),
grpc_config :: GrpcConfiguration.t()
) :: TransportStrategy.t()
def with_grpc_config(transport_strategy, grpc_config) do
%TransportStrategy{
grpc_config: grpc_config
}
end
end
40 changes: 40 additions & 0 deletions src/test/momento/config/configuration_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Momento.Config.ConfigurationTest do
alias Momento.Config.Transport.TransportStrategy, as: TransportStrategy
alias Momento.Config.Transport.GrpcConfiguration, as: GrpcConfiguration

use ExUnit.Case
doctest Momento.Config.Configuration

@test_grpc_configuration %GrpcConfiguration{
deadline_millis: 90210
}

@test_transport_strategy %TransportStrategy{
grpc_config: @test_grpc_configuration
}

@test_configuration %Momento.Config.Configuration{
transport_strategy: @test_transport_strategy
}

describe "Constructing Configuration" do
test "overriding transport strategy" do
new_grpc_configuration = %GrpcConfiguration{
deadline_millis: 424_242
}

new_transport_strategy = %TransportStrategy{
grpc_config: new_grpc_configuration
}

new_config =
Momento.Config.Configuration.with_transport_strategy(
@test_configuration,
new_transport_strategy
)

assert new_config.transport_strategy == new_transport_strategy
assert new_config.transport_strategy.grpc_config == new_grpc_configuration
end
end
end

0 comments on commit b6d3416

Please sign in to comment.