Beispiel #1
0
 internal StanOptions(StanOptions options)
 {
     ackTimeout         = options.ackTimeout;
     NatsURL            = DeepCopy(options.NatsURL);
     ConnectTimeout     = options.ConnectTimeout;
     PubAckWait         = options.PubAckWait;
     DiscoverPrefix     = DeepCopy(options.DiscoverPrefix);
     MaxPubAcksInFlight = options.MaxPubAcksInFlight;
     NatsConn           = options.natsConn;
 }
        internal StanSubscriptionOptions(StanSubscriptionOptions opts)
        {
            if (opts == null)
            {
                return;
            }

            ackWait = opts.ackWait;

            if (opts.durableName != null)
            {
                durableName = StanOptions.DeepCopy(opts.durableName);
            }
            leaveOpen         = opts.leaveOpen;
            manualAcks        = opts.manualAcks;
            maxInFlight       = opts.maxInFlight;
            startAt           = opts.startAt;
            startSequence     = opts.startSequence;
            useStartTimeDelta = opts.useStartTimeDelta;
            startTime         = opts.startTime;
            startTimeDelta    = opts.startTimeDelta;
        }
        internal Connection(string stanClusterID, string clientID, StanOptions options)
        {
            this.clientID = clientID;

            if (options != null)
            {
                opts = new StanOptions(options);
            }
            else
            {
                opts = new StanOptions();
            }

            if (opts.natsConn == null)
            {
                ncOwned = true;
                try
                {
                    nc = new ConnectionFactory().CreateConnection(opts.NatsURL);
                }
                catch (Exception ex)
                {
                    throw new StanConnectionException(ex);
                }
            }
            else
            {
                nc      = opts.natsConn;
                ncOwned = false;
            }

            // create a heartbeat inbox
            string hbInbox = newInbox();

            hbSubscription = nc.SubscribeAsync(hbInbox, processHeartBeat);

            string discoverSubject = opts.discoverPrefix + "." + stanClusterID;

            ConnectRequest req = new ConnectRequest();

            req.ClientID       = this.clientID;
            req.HeartbeatInbox = hbInbox;

            Msg cr;

            try
            {
                cr = nc.Request(discoverSubject,
                                ProtocolSerializer.marshal(req),
                                opts.ConnectTimeout);
            }
            catch (NATSTimeoutException)
            {
                throw new StanConnectRequestTimeoutException();
            }

            ConnectResponse response = new ConnectResponse();

            try
            {
                ProtocolSerializer.unmarshal(cr.Data, response);
            }
            catch (Exception e)
            {
                throw new StanConnectRequestException(e);
            }

            if (!string.IsNullOrEmpty(response.Error))
            {
                throw new StanConnectRequestException(response.Error);
            }

            // capture cluster configuration endpoints to publish and subscribe/unsubscribe
            pubPrefix        = response.PubPrefix;
            subRequests      = response.SubRequests;
            unsubRequests    = response.UnsubRequests;
            subCloseRequests = response.SubCloseRequests;
            closeRequests    = response.CloseRequests;

            // setup the Ack subscription
            ackSubject      = StanConsts.DefaultACKPrefix + "." + newGUID();
            ackSubscription = nc.SubscribeAsync(ackSubject, processAck);

            // TODO:  hardcode or options?
            ackSubscription.SetPendingLimits(1024 * 1024, 32 * 1024 * 1024);

            pubAckMap = new BlockingDictionary <string, PublishAck>(opts.maxPubAcksInflight);

            // Setup server heartbeat monitor
            if (options != null && options.ServerHeartbeatTimeoutMillis > 0)
            {
                heartbeatMonitor = new ServerHeartbeatMonitor(1000, options.ServerHeartbeatTimeoutMillis, () =>
                {
                    options.ServerHeartbeatTimeoutCallback?.Invoke();
                });
                heartbeatMonitor.Start();
            }
        }