Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions unlaunchio/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package client

import (
"errors"
"fmt"
"strings"
"sync/atomic"

"github.com/unlaunch/go-sdk/unlaunchio/engine"
"github.com/unlaunch/go-sdk/unlaunchio/service"
"github.com/unlaunch/go-sdk/unlaunchio/service/api"
"github.com/unlaunch/go-sdk/unlaunchio/util"
"github.com/unlaunch/go-sdk/unlaunchio/util/logger"
"strings"
)

// UnlaunchFactory ...
Expand All @@ -17,6 +20,8 @@ type UnlaunchFactory struct {
logger logger.Interface
}

var sync0Complete atomic.Value

// NewUnlaunchClientFactory is a factory
func NewUnlaunchClientFactory(SDKKey string, cfg *UnlaunchClientConfig) (*UnlaunchFactory, error) {

Expand Down Expand Up @@ -49,33 +54,80 @@ func (f *UnlaunchFactory) Client() Client {
}
}

if sync0Complete.Load() == nil {
sync0Complete.Store(true) // we preemptively marked it as done
client := f.sync0()
if client == nil { // regular server sync
return f.regularServerSync()
} else {
return client
}

} else {
return f.regularServerSync()
}
}

func (f *UnlaunchFactory) regularServerSync() Client {

eventsRecorder := api.NewHTTPEventsRecorder(
false,
util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger),
util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger, false),
"/api/v1/impressions",
f.cfg.MetricsFlushInterval,
f.cfg.MetricsQueueSize,
"impressions",
f.logger)

eventsCounts := api.NewEventsCountAggregator(
util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger),
util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger, false),
"/api/v1/events",
f.cfg.MetricsFlushInterval,
f.cfg.MetricsQueueSize,
f.logger)

hc := util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger)
hc := util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger, false)

return &SimpleClient{
FeatureStore: service.NewHTTPFeatureStore(
hc,
f.cfg.PollingInterval,
f.logger),
f.logger,
false,
nil),
eventsRecorder: eventsRecorder,
eventsCountAggregator: eventsCounts,
logger: f.logger,
evaluator: engine.NewEvaluator(f.logger),
}
}

func (f *UnlaunchFactory) sync0() Client {
hc := util.NewHTTPClient(f.sdkKey, f.cfg.Host, f.cfg.HTTPTimeout, f.logger, true)

res, err := hc.Get("https://app-qa-unlaunch-io-master-flags.s3-us-west-1.amazonaws.com")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong URL. We have different for production. You're using QA one


if res == nil {
return nil
}

if err != nil {
f.logger.Error("[HTTP GET] error reading body", err)
return nil
}

f.logger.Debug(fmt.Sprintf("[HTTP GET] data: %d", res))

return &SimpleClient{
FeatureStore: service.NewHTTPFeatureStore(
hc,
f.cfg.PollingInterval,
f.logger,
true,
res),
logger: f.logger,
evaluator: engine.NewEvaluator(f.logger),
}

// return res
}
60 changes: 40 additions & 20 deletions unlaunchio/service/httpfeaturestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"time"

"github.com/unlaunch/go-sdk/unlaunchio/dtos"
"github.com/unlaunch/go-sdk/unlaunchio/util"
"github.com/unlaunch/go-sdk/unlaunchio/util/logger"
"sort"
"time"
)

type HTTPFeatureStore struct {
Expand All @@ -17,6 +18,8 @@ type HTTPFeatureStore struct {
features map[string]dtos.Feature
initialSyncComplete bool
shutdownCh chan bool
sync0Complete bool
res []byte
}

func (h *HTTPFeatureStore) Shutdown() {
Expand All @@ -25,30 +28,42 @@ func (h *HTTPFeatureStore) Shutdown() {
}

func (h *HTTPFeatureStore) fetchFlags() error {
res, err := h.httpClient.Get("/api/v1/flags")

if err != nil {
if httpError, ok := err.(*dtos.HTTPError); ok {
if httpError.Code == 403 {
h.logger.Error(
fmt.Sprintf("The API key you provided was rejected by the server. %s", util.SDKKeyHelpMessage))
var res []byte
var err error
if !h.sync0Complete {
res, err = h.httpClient.Get("/api/v1/flags")

if err != nil {
if httpError, ok := err.(*dtos.HTTPError); ok {
if httpError.Code == 403 {
h.logger.Error(
fmt.Sprintf("The API key you provided was rejected by the server. %s", util.SDKKeyHelpMessage))
}
} else {
h.logger.Error("error fetching flags ", err)
return err
}
} else {
h.logger.Error("error fetching flags ", err)
return err
}
}

if res == nil {
// No error and empty response means nothing changed
// most like due to 304; not modified
return nil
if res == nil {
// No error and empty response means nothing changed
// most like due to 304; not modified
return nil
}

h.logger.Trace("responseDto ", string(res))

} else {
res = h.res
}

h.logger.Trace("responseDto ", string(res))
return h.initFeatureMap(res)

}

func (h *HTTPFeatureStore) initFeatureMap(res []byte) error {
var responseDto dtos.TopLevelEnvelope
err = json.Unmarshal(res, &responseDto)
err := json.Unmarshal(res, &responseDto)

if err != nil {
h.logger.Error("error parsing feature flag JSON response ", err)
Expand Down Expand Up @@ -77,6 +92,7 @@ func (h *HTTPFeatureStore) fetchFlags() error {
h.features = temp

h.logger.Debug("Downloaded: ", len(h.features))

return nil
}

Expand Down Expand Up @@ -115,12 +131,16 @@ func (h *HTTPFeatureStore) Ready(timeout time.Duration) {
func NewHTTPFeatureStore(
httpClient util.HTTPClient,
pollingInterval time.Duration,
logger logger.Interface) FeatureStore {
logger logger.Interface,
sync0Complete bool,
res []byte) FeatureStore {
httpStore := &HTTPFeatureStore{
httpClient: httpClient,
logger: logger,
initialSyncComplete: false,
features: nil,
sync0Complete: sync0Complete,
res: res,
}

httpStore.shutdownCh = util.RunImmediatelyAndSchedule(httpStore.fetchFlags, pollingInterval)
Expand Down
4 changes: 2 additions & 2 deletions unlaunchio/service/httpfeaturestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestWhen_DataIsReturned_Then_FeatureStoreIsReady(t *testing.T) {
h := &mockHTTPClient{}
h.returnValidJSON = true

fs := NewHTTPFeatureStore(h, 100000000, logger.NewLogger(nil))
fs := NewHTTPFeatureStore(h, 100000000, logger.NewLogger(nil), false, nil)

time.Sleep(100 * time.Millisecond)

Expand All @@ -89,7 +89,7 @@ func getHTTPFeatureStore() FeatureStore {
h := NewHTTPFeatureStore(
&mockHTTPClient{},
900,
logger.NewLogger(nil))
logger.NewLogger(nil), false, nil)
return h
}

Expand Down
84 changes: 74 additions & 10 deletions unlaunchio/util/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package util
import (
"bytes"
"fmt"
"github.com/unlaunch/go-sdk/unlaunchio/dtos"
"github.com/unlaunch/go-sdk/unlaunchio/util/logger"
"io/ioutil"
"net/http"
"time"

"github.com/unlaunch/go-sdk/unlaunchio/dtos"
"github.com/unlaunch/go-sdk/unlaunchio/util/logger"
)

type HTTPClient interface {
Expand All @@ -24,23 +25,45 @@ type simpleHTTPClient struct {
lastModifiedAt string
}

type GenericHTTPClient struct {
host string
httpClient *http.Client
logger logger.Interface
sdkKey string
}

// NewHTTPClient returns a new http client
func NewHTTPClient(
sdkKey string,
host string,
timeout time.Duration,
logger logger.Interface,
sync0 bool,
) HTTPClient {

client := &http.Client{
Timeout: timeout,
}
if sync0 {
client := &http.Client{
Timeout: timeout,
}

return &GenericHTTPClient{
host: host,
httpClient: client,
logger: logger,
sdkKey: sdkKey,
}

} else {
client := &http.Client{
Timeout: timeout,
}

return &simpleHTTPClient{
host: host,
httpClient: client,
logger: logger,
sdkKey: sdkKey,
return &simpleHTTPClient{
host: host,
httpClient: client,
logger: logger,
sdkKey: sdkKey,
}
}
}

Expand Down Expand Up @@ -126,3 +149,44 @@ func (c *simpleHTTPClient) Post(path string, body []byte) error {
Msg: resp.Status,
}
}

func (c *GenericHTTPClient) Get(path string) ([]byte, error) {
apiEndpoint := path
c.logger.Debug("[HTTP GET] ", apiEndpoint)

req, _ := http.NewRequest("GET", apiEndpoint, nil)

resp, err := c.httpClient.Do(req)

if err != nil {
c.logger.Error("[HTTP GET] HTTP error ", err)
return nil, err
}

defer resp.Body.Close()

reader := resp.Body
defer reader.Close()

body, err := ioutil.ReadAll(reader)

if err != nil {
c.logger.Error("[HTTP GET] error reading body", err)
return nil, err
}

c.logger.Debug(fmt.Sprintf("[HTTP GET] status code: %d", resp.StatusCode))

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return body, nil
} else {
return nil, &dtos.HTTPError{
Code: resp.StatusCode,
Msg: resp.Status,
}
}
}

func (c *GenericHTTPClient) Post(path string, body []byte) error {
return nil
}