Exemplo n.º 1
0
        LdClient(Configuration configuration, User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            _config = configuration ?? throw new ArgumentNullException(nameof(configuration));

            persister  = Factory.CreatePersistentStorage(configuration);
            deviceInfo = Factory.CreateDeviceInfo(configuration);
            flagChangedEventManager = Factory.CreateFlagChangedEventManager(configuration);

            _user = DecorateUser(user);

            flagCacheManager = Factory.CreateFlagCacheManager(configuration, persister, flagChangedEventManager, User);

            _connectionManager = new ConnectionManager();
            _connectionManager.SetForceOffline(configuration.Offline);
            if (configuration.Offline)
            {
                Log.InfoFormat("Starting LaunchDarkly client in offline mode");
            }
            _connectionManager.SetUpdateProcessorFactory(
                Factory.CreateUpdateProcessorFactory(configuration, User, flagCacheManager, _inBackground),
                true
                );

            _connectivityStateManager = Factory.CreateConnectivityStateManager(configuration);
            _connectivityStateManager.ConnectionChanged += networkAvailable =>
            {
                Log.DebugFormat("Setting online to {0} due to a connectivity change event", networkAvailable);
                _ = _connectionManager.SetNetworkEnabled(networkAvailable);  // do not await the result
                eventProcessor.SetOffline(!networkAvailable || _connectionManager.ForceOffline);
            };
            var isConnected = _connectivityStateManager.IsConnected;

            _connectionManager.SetNetworkEnabled(isConnected);

            eventProcessor = Factory.CreateEventProcessor(configuration);
            eventProcessor.SetOffline(configuration.Offline || !isConnected);

            // Send an initial identify event, but only if we weren't explicitly set to be offline
            if (!configuration.Offline)
            {
                eventProcessor.SendEvent(_eventFactoryDefault.NewIdentifyEvent(User));
            }

            _backgroundModeManager = _config._backgroundModeManager ?? new DefaultBackgroundModeManager();
            _backgroundModeManager.BackgroundModeChanged += OnBackgroundModeChanged;
        }
Exemplo n.º 2
0
        internal Configuration(ConfigurationBuilder builder)
        {
            _allAttributesPrivate      = builder._allAttributesPrivate;
            _backgroundPollingInterval = builder._backgroundPollingInterval;
            _baseUri                  = builder._baseUri;
            _connectionTimeout        = builder._connectionTimeout;
            _enableBackgroundUpdating = builder._enableBackgroundUpdating;
            _evaluationReasons        = builder._evaluationReasons;
            _eventFlushInterval       = builder._eventFlushInterval;
            _eventCapacity            = builder._eventCapacity;
            _eventsUri                = builder._eventsUri;
            _httpMessageHandler       = object.ReferenceEquals(builder._httpMessageHandler, ConfigurationBuilder.DefaultHttpMessageHandlerInstance) ?
                                        PlatformSpecific.Http.CreateHttpMessageHandler(builder._connectionTimeout, builder._readTimeout) :
                                        builder._httpMessageHandler;
            _inlineUsersInEvents   = builder._inlineUsersInEvents;
            _isStreamingEnabled    = builder._isStreamingEnabled;
            _mobileKey             = builder._mobileKey;
            _offline               = builder._offline;
            _persistFlagValues     = builder._persistFlagValues;
            _pollingInterval       = builder._pollingInterval;
            _privateAttributeNames = builder._privateAttributeNames is null ? null :
                                     builder._privateAttributeNames.ToImmutableHashSet();
            _readTimeout           = builder._readTimeout;
            _reconnectTime         = builder._reconnectTime;
            _streamUri             = builder._streamUri;
            _useReport             = builder._useReport;
            _userKeysCapacity      = builder._userKeysCapacity;
            _userKeysFlushInterval = builder._userKeysFlushInterval;

            _backgroundModeManager    = builder._backgroundModeManager;
            _connectivityStateManager = builder._connectivityStateManager;
            _deviceInfo              = builder._deviceInfo;
            _eventProcessor          = builder._eventProcessor;
            _flagCacheManager        = builder._flagCacheManager;
            _flagChangedEventManager = builder._flagChangedEventManager;
            _persistentStorage       = builder._persistentStorage;
            _updateProcessorFactory  = builder._updateProcessorFactory;
        }
 internal ConfigurationBuilder ConnectivityStateManager(IConnectivityStateManager connectivityStateManager)
 {
     _connectivityStateManager = connectivityStateManager;
     return(this);
 }
Exemplo n.º 4
0
        LdClient(Configuration configuration, User user, TimeSpan startWaitTime)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            _config = configuration ?? throw new ArgumentNullException(nameof(configuration));
            var diagnosticStore = _config.DiagnosticOptOut ? null :
                                  new ClientDiagnosticStore(_config, startWaitTime);
            var diagnosticDisabler = _config.DiagnosticOptOut ? null :
                                     new DiagnosticDisablerImpl();

            _context      = new LdClientContext(configuration, this, diagnosticStore, diagnosticDisabler);
            _log          = _context.BaseLogger;
            _taskExecutor = _context.TaskExecutor;
            diagnosticStore?.SetContext(_context);

            _log.Info("Starting LaunchDarkly Client {0}", Version);

            var persistenceConfiguration = (configuration.PersistenceConfigurationBuilder ?? Components.Persistence())
                                           .CreatePersistenceConfiguration(_context);

            _dataStore = new FlagDataManager(
                configuration.MobileKey,
                persistenceConfiguration,
                _log.SubLogger(LogNames.DataStoreSubLog)
                );

            _userDecorator = new UserDecorator(configuration.DeviceInfo ?? new DefaultDeviceInfo(),
                                               _dataStore.PersistentStore);
            _user = _userDecorator.DecorateUser(user);

            // If we had cached data for the new user, set the current in-memory flag data state to use
            // that data, so that any Variation calls made before Identify has completed will use the
            // last known values.
            var cachedData = _dataStore.GetCachedData(_user);

            if (cachedData != null)
            {
                _log.Debug("Cached flag data is available for this user");
                _dataStore.Init(_user, cachedData.Value, false); // false means "don't rewrite the flags to persistent storage"
            }

            var dataSourceUpdateSink = new DataSourceUpdateSinkImpl(
                _dataStore,
                configuration.Offline,
                _taskExecutor,
                _log.SubLogger(LogNames.DataSourceSubLog)
                );

            _dataSourceUpdateSink = dataSourceUpdateSink;

            _dataSourceStatusProvider = new DataSourceStatusProviderImpl(dataSourceUpdateSink);
            _flagTracker = new FlagTrackerImpl(dataSourceUpdateSink);

            var dataSourceFactory = configuration.DataSourceFactory ?? Components.StreamingDataSource();

            _connectivityStateManager = Factory.CreateConnectivityStateManager(configuration);
            var isConnected = _connectivityStateManager.IsConnected;

            diagnosticDisabler?.SetDisabled(!isConnected || configuration.Offline);

            _eventProcessor = (configuration.EventProcessorFactory ?? Components.SendEvents())
                              .CreateEventProcessor(_context);
            _eventProcessor.SetOffline(configuration.Offline || !isConnected);

            _connectionManager = new ConnectionManager(
                _context,
                dataSourceFactory,
                _dataSourceUpdateSink,
                _eventProcessor,
                diagnosticDisabler,
                configuration.EnableBackgroundUpdating,
                _user,
                _log
                );
            _connectionManager.SetForceOffline(configuration.Offline);
            _connectionManager.SetNetworkEnabled(isConnected);
            if (configuration.Offline)
            {
                _log.Info("Starting LaunchDarkly client in offline mode");
            }

            _connectivityStateManager.ConnectionChanged += networkAvailable =>
            {
                _log.Debug("Setting online to {0} due to a connectivity change event", networkAvailable);
                _ = _connectionManager.SetNetworkEnabled(networkAvailable);  // do not await the result
            };

            // Send an initial identify event, but only if we weren't explicitly set to be offline

            if (!configuration.Offline)
            {
                _eventProcessor.RecordIdentifyEvent(new EventProcessorTypes.IdentifyEvent
                {
                    Timestamp = UnixMillisecondTime.Now,
                    User      = user
                });
            }

            _backgroundModeManager = _config.BackgroundModeManager ?? new DefaultBackgroundModeManager();
            _backgroundModeManager.BackgroundModeChanged += OnBackgroundModeChanged;
        }