static ProducerTest() { _nsqdHttpClient = new NsqdHttpClient("127.0.0.1:4151", TimeSpan.FromSeconds(5)); _nsqLookupdHttpClient = new NsqLookupdHttpClient("127.0.0.1:4161", TimeSpan.FromSeconds(5)); }
private void queryLookupd() { string endpoint = nextLookupdEndpoint(); log(LogLevel.Debug, string.Format("querying nsqlookupd {0}", endpoint)); int timeoutMilliseconds = (int)_config.DialTimeout.TotalMilliseconds; if (timeoutMilliseconds < 2000) timeoutMilliseconds = 2000; TopicProducerInformation[] producers; try { var nsqLookupdClient = new NsqLookupdHttpClient(endpoint, TimeSpan.FromMilliseconds(timeoutMilliseconds)); producers = nsqLookupdClient.Lookup(_topic).Producers; } catch (Exception ex) { var webException = ex as WebException; if (webException != null) { var httpWebResponse = webException.Response as HttpWebResponse; if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.NotFound) { log(LogLevel.Warning, string.Format("404 querying nsqlookupd ({0}) for topic {1}", endpoint, _topic)); if (endpoint.Contains(":4151")) { log(LogLevel.Error, string.Format("404 querying nsqlookupd ({0}) - *** {1} *** - {2}", endpoint, "This endpoint looks like an nsqd address. Try connecting to port 4161.", ex)); } return; } } if (endpoint.Contains(":4150") || endpoint.Contains(":4151")) { log(LogLevel.Error, string.Format("error querying nsqlookupd ({0}) - *** {1} *** - {2}", endpoint, "This endpoint looks like an nsqd address. Try connecting to port 4161.", ex)); } else if (endpoint.Contains(":4160")) { log(LogLevel.Error, string.Format("error querying nsqlookupd ({0}) - *** {1} *** - {2}", endpoint, "This endpoint looks like an nsqlookupd TCP port. Try connecting to HTTP port 4161.", ex)); } log(LogLevel.Error, string.Format("error querying nsqlookupd ({0}) - {1}", endpoint, ex)); return; } // { // "channels": [], // "producers": [ // { // "broadcast_address": "jehiah-air.local", // "http_port": 4151, // "tcp_port": 4150 // } // ], // "timestamp": 1340152173 // } var nsqAddrs = new Collection<string>(); foreach (var producer in producers) { var broadcastAddress = producer.BroadcastAddress; var port = producer.TcpPort; var joined = string.Format("{0}:{1}", broadcastAddress, port); nsqAddrs.Add(joined); } var behaviorDelegate = _behaviorDelegate; if (behaviorDelegate != null) { nsqAddrs = new Collection<string>(behaviorDelegate.Filter(nsqAddrs).ToList()); } if (_stopFlag == 1) return; foreach (var addr in nsqAddrs) { try { ConnectToNsqd(addr); } catch (Exception ex) { log(LogLevel.Error, string.Format("({0}) error connecting to nsqd - {1}", addr, ex)); } } }
private void PingNsqLookupd(NsqLookupdModel nsqLookupdModel) { var client = new NsqLookupdHttpClient(nsqLookupdModel.Uri.OriginalString, TimeSpan.FromSeconds(10)); bool success = false; var pingResponse = DateTime.MinValue; try { client.Ping(); pingResponse = DateTime.Now; success = true; } catch (Exception) { } _dispatcher.BeginInvoke(() => { nsqLookupdModel.IsAlive = success; if (success) nsqLookupdModel.LastPingResponse = pingResponse; }); try { var topics = client.GetTopics().Select(p => new TopicModel { Topic = p }); topics = topics.OrderBy(p => p.Topic); _dispatcher.BeginInvoke(() => { if (Topics == null) { Topics = new ObservableCollection<TopicModel>(topics); } else { foreach (var topic in topics) { var existingTopic = Topics.SingleOrDefault(p => p.Topic == topic.Topic); if (existingTopic == null) Topics.Add(topic); } } }); } catch (Exception) { throw; } try { var nsqdNodes = client.GetNodes(); var tasks = new List<Task>(); for (int i = 0; i < nsqdNodes.Length; i++) { var nsqdNode = nsqdNodes[i]; var task = Task.Factory.StartNew(() => GetNsqdInformation(nsqdNode)); tasks.Add(task); } Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(10)); } catch (Exception) { throw; } }
static ConsumerRdyRedistributionTest() { _nsqdHttpClient1 = new NsqdHttpClient("127.0.0.1:4151", TimeSpan.FromSeconds(5)); _nsqdHttpClient2 = new NsqdHttpClient("127.0.0.1:5151", TimeSpan.FromSeconds(5)); _nsqLookupdHttpClient = new NsqLookupdHttpClient("127.0.0.1:4161", TimeSpan.FromSeconds(5)); }