Skip to content

Commit

Permalink
(offline mode #3) factor out types that are shared between autoconfig…
Browse files Browse the repository at this point in the history
… and filedata (#200)
  • Loading branch information
eli-darkly authored Oct 15, 2020
1 parent b562add commit f2eded8
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 284 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build:
go build ./...

test:
go test -run=not-a-real-test ./... # just ensures that the tests compile
go test -race -v ./...

test-coverage: $(COVERAGE_PROFILE_RAW)
Expand Down
36 changes: 3 additions & 33 deletions internal/autoconfig/message_handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package autoconfig

import (
"time"

"github.com/launchdarkly/ld-relay/v6/config"
"github.com/launchdarkly/ld-relay/v6/internal/core/relayenv"
"github.com/launchdarkly/ld-relay/v6/internal/envfactory"
)

// MessageHandler defines the methods that StreamManager will call when it receives messages
// from the auto-configuration stream.
type MessageHandler interface {
// AddEnvironment is called when the stream has provided a configuration for an environment
// that StreamManager has not seen before. This can happen due to either a "put" or a "patch".
AddEnvironment(params EnvironmentParams)
AddEnvironment(params envfactory.EnvironmentParams)

// UpdateEnvironment is called when the stream has provided a new configuration for an
// existing environment. This can happen due to either a "put" or a "patch".
UpdateEnvironment(params EnvironmentParams)
UpdateEnvironment(params envfactory.EnvironmentParams)

// DeleteEnvironment is called when an environment should be removed, due to either a "delete"
// message, or a "put" that no longer contains that environment.
Expand All @@ -27,31 +25,3 @@ type MessageHandler interface {
// future requests that use it.
KeyExpired(id config.EnvironmentID, oldKey config.SDKKey)
}

// EnvironmentParams contains information that MessageHandler passes to StreamHandler to describe
// an environment. This is not the same type that we use for the environment representation in the
// auto-config stream, because not all of the properties there are relevant to Relay.
type EnvironmentParams struct {
// ID is the environment ID.
EnvID config.EnvironmentID

// Identifiers contains the project and environment names and keys.
Identifiers relayenv.EnvIdentifiers

// SDKKey is the environment's SDK key; if there is more than one active key, it is the latest.
SDKKey config.SDKKey

// MobileKey is the environment's mobile key.
MobileKey config.MobileKey

// DeprecatedSDKKey is an additional SDK key that should also be allowed (but not surfaced as
// the canonical one), or "" if none. StreamManager is responsible for knowing when the key
// will expire.
ExpiringSDKKey config.SDKKey

// TTL is the cache TTL for PHP clients.
TTL time.Duration

// SecureMode is true if secure mode is required for this environment.
SecureMode bool
}
45 changes: 0 additions & 45 deletions internal/autoconfig/reps_test.go

This file was deleted.

39 changes: 10 additions & 29 deletions internal/autoconfig/stream_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
es "github.com/launchdarkly/eventsource"
"github.com/launchdarkly/ld-relay/v6/config"
"github.com/launchdarkly/ld-relay/v6/internal/core/httpconfig"
"github.com/launchdarkly/ld-relay/v6/internal/core/relayenv"
"github.com/launchdarkly/ld-relay/v6/internal/envfactory"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldlog"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ type StreamManager struct {
key config.AutoConfigKey
uri string
handler MessageHandler
lastKnownEnvs map[config.EnvironmentID]EnvironmentRep
lastKnownEnvs map[config.EnvironmentID]envfactory.EnvironmentRep
expiredKeys chan expiredKey
expiryTimers map[config.SDKKey]*time.Timer
httpConfig httpconfig.HTTPConfig
Expand Down Expand Up @@ -79,7 +79,7 @@ func NewStreamManager(
key: key,
uri: strings.TrimSuffix(baseURI, "/") + autoConfigStreamPath,
handler: handler,
lastKnownEnvs: make(map[config.EnvironmentID]EnvironmentRep),
lastKnownEnvs: make(map[config.EnvironmentID]envfactory.EnvironmentRep),
expiredKeys: make(chan expiredKey),
expiryTimers: make(map[config.SDKKey]*time.Timer),
httpConfig: httpConfig,
Expand Down Expand Up @@ -271,7 +271,7 @@ func (s *StreamManager) consumeStream(stream *es.Stream) {
// All of the private methods below can be assumed to be called from the same goroutine that consumeStream
// is on. We will never be processing more than one stream message at the same time.

func (s *StreamManager) handlePut(allEnvReps map[config.EnvironmentID]EnvironmentRep) {
func (s *StreamManager) handlePut(allEnvReps map[config.EnvironmentID]envfactory.EnvironmentRep) {
// A "put" message represents a full environment set. We will compare them one at a time to the
// current set of environments (if any), calling the handler's AddEnvironment for any new ones,
// UpdateEnvironment for any that have changed, and DeleteEnvironment for any that are no longer
Expand All @@ -295,8 +295,8 @@ func (s *StreamManager) handlePut(allEnvReps map[config.EnvironmentID]Environmen
}
}

func (s *StreamManager) addOrUpdate(rep EnvironmentRep) {
params := MakeEnvironmentParams(rep)
func (s *StreamManager) addOrUpdate(rep envfactory.EnvironmentRep) {
params := rep.ToParams()

// Check whether this is a new environment or an update
currentEnv, exists := s.lastKnownEnvs[rep.EnvID]
Expand Down Expand Up @@ -371,34 +371,15 @@ func (s *StreamManager) handleDelete(envID config.EnvironmentID, version int) {
}
}

// MakeEnvironmentParams converts the JSON properties for an environment into our internal parameter type.
// It is exported here because it is also used in the filedata package.
func MakeEnvironmentParams(rep EnvironmentRep) EnvironmentParams {
return EnvironmentParams{
EnvID: rep.EnvID,
Identifiers: relayenv.EnvIdentifiers{
EnvKey: rep.EnvKey,
EnvName: rep.EnvName,
ProjKey: rep.ProjKey,
ProjName: rep.ProjName,
},
SDKKey: rep.SDKKey.Value,
MobileKey: rep.MobKey,
ExpiringSDKKey: rep.SDKKey.Expiring.Value,
TTL: time.Duration(rep.DefaultTTL) * time.Minute,
SecureMode: rep.SecureMode,
}
}

func makeEnvName(rep EnvironmentRep) string {
func makeEnvName(rep envfactory.EnvironmentRep) string {
return fmt.Sprintf("%s %s", rep.ProjName, rep.EnvName)
}

func makeTombstone(version int) EnvironmentRep {
return EnvironmentRep{Version: version}
func makeTombstone(version int) envfactory.EnvironmentRep {
return envfactory.EnvironmentRep{Version: version}
}

func isTombstone(rep EnvironmentRep) bool {
func isTombstone(rep envfactory.EnvironmentRep) bool {
return rep.EnvID == ""
}

Expand Down
75 changes: 8 additions & 67 deletions internal/autoconfig/stream_manager_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,15 @@ import (
"testing"
"time"

"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"

"gopkg.in/launchdarkly/go-sdk-common.v2/ldlog"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/launchdarkly/go-test-helpers/v2/httphelpers"
"github.com/launchdarkly/ld-relay/v6/config"
"github.com/launchdarkly/ld-relay/v6/internal/core/relayenv"
)

func TestMakeEnvironmentParams(t *testing.T) {
env1 := EnvironmentRep{
EnvID: config.EnvironmentID("envid1"),
EnvKey: "envkey1",
EnvName: "envname1",
MobKey: config.MobileKey("mobkey1"),
ProjKey: "projkey1",
ProjName: "projname1",
SDKKey: SDKKeyRep{Value: config.SDKKey("sdkkey1")},
DefaultTTL: 2,
SecureMode: true,
}
params1 := MakeEnvironmentParams(env1)
assert.Equal(t, EnvironmentParams{
EnvID: env1.EnvID,
Identifiers: relayenv.EnvIdentifiers{
EnvKey: "envkey1",
EnvName: "envname1",
ProjKey: "projkey1",
ProjName: "projname1",
},
SDKKey: env1.SDKKey.Value,
MobileKey: env1.MobKey,
TTL: 2 * time.Minute,
SecureMode: true,
}, params1)

env2 := EnvironmentRep{
EnvID: config.EnvironmentID("envid2"),
EnvKey: "envkey2",
EnvName: "envname2",
MobKey: config.MobileKey("mobkey2"),
ProjKey: "projkey2",
ProjName: "projname2",
SDKKey: SDKKeyRep{
Value: config.SDKKey("sdkkey2"),
Expiring: ExpiringKeyRep{
Value: config.SDKKey("oldkey"),
Timestamp: ldtime.UnixMillisecondTime(10000),
}},
}
params2 := MakeEnvironmentParams(env2)
assert.Equal(t, EnvironmentParams{
EnvID: env2.EnvID,
Identifiers: relayenv.EnvIdentifiers{
EnvKey: "envkey2",
EnvName: "envname2",
ProjKey: "projkey2",
ProjName: "projname2",
},
SDKKey: env2.SDKKey.Value,
ExpiringSDKKey: env2.SDKKey.Expiring.Value,
MobileKey: env2.MobKey,
}, params2)
}

func TestPutEvent(t *testing.T) {
t.Run("add all new environments to empty state", func(t *testing.T) {
event := makePutEvent(testEnv1, testEnv2)
Expand All @@ -85,8 +26,8 @@ func TestPutEvent(t *testing.T) {
if msg1.add.EnvID == testEnv2.EnvID {
msg1, msg2 = msg2, msg1
}
assert.Equal(t, MakeEnvironmentParams(testEnv1), *msg1.add)
assert.Equal(t, MakeEnvironmentParams(testEnv2), *msg2.add)
assert.Equal(t, testEnv1.ToParams(), *msg1.add)
assert.Equal(t, testEnv2.ToParams(), *msg2.add)

p.mockLog.AssertMessageMatch(t, true, ldlog.Info, "Received configuration for 2")
p.mockLog.AssertMessageMatch(t, true, ldlog.Info, "Added environment "+string(testEnv1.EnvID))
Expand All @@ -103,12 +44,12 @@ func TestPutEvent(t *testing.T) {

msg1 := p.requireMessage()
require.NotNil(t, msg1.add)
assert.Equal(t, MakeEnvironmentParams(testEnv1), *msg1.add)
assert.Equal(t, testEnv1.ToParams(), *msg1.add)

p.stream.Enqueue(makePutEvent(testEnv1, testEnv2))
msg2 := p.requireMessage()
require.NotNil(t, msg2.add)
assert.Equal(t, MakeEnvironmentParams(testEnv2), *msg2.add)
assert.Equal(t, testEnv2.ToParams(), *msg2.add)

p.requireNoMoreMessages()

Expand All @@ -133,7 +74,7 @@ func TestPutEvent(t *testing.T) {
p.stream.Enqueue(makePutEvent(testEnv1Mod, testEnv2))
msg := p.requireMessage()
require.NotNil(t, msg.update)
assert.Equal(t, MakeEnvironmentParams(testEnv1Mod), *msg.update)
assert.Equal(t, testEnv1Mod.ToParams(), *msg.update)

p.requireNoMoreMessages()

Expand Down Expand Up @@ -220,7 +161,7 @@ func TestPatchEvent(t *testing.T) {

msg := p.requireMessage()
require.NotNil(t, msg.add)
assert.Equal(t, MakeEnvironmentParams(testEnv1), *msg.add)
assert.Equal(t, testEnv1.ToParams(), *msg.add)
})
})

Expand All @@ -239,7 +180,7 @@ func TestPatchEvent(t *testing.T) {

msg := p.requireMessage()
require.NotNil(t, msg.update)
assert.Equal(t, MakeEnvironmentParams(testEnv1Mod), *msg.update)
assert.Equal(t, testEnv1Mod.ToParams(), *msg.update)
})
})

Expand Down Expand Up @@ -299,7 +240,7 @@ func TestPatchEvent(t *testing.T) {

msg := p.requireMessage()
require.NotNil(t, msg.add)
assert.Equal(t, MakeEnvironmentParams(testEnv1Mod), *msg.add)
assert.Equal(t, testEnv1Mod.ToParams(), *msg.add)
})
})

Expand Down
Loading

0 comments on commit f2eded8

Please sign in to comment.