public bool TryGetAvailableNode(out NodeEntry node) { var now = DateTime.Now; var candidates = new List <NodeEntry>(); using (locker.CreateLock()) { foreach (var candidate in nodes.Values) { // TODO find out why Debug.Assert(candidate.Node.HasServer); // looks like not thread safe if (!candidate.Node.HasServer) { continue; } if (candidate.IsAvailable && !knownSelfEndPoints.Contains(candidate.Node.EndPoint) && IsReady(candidate, now)) { candidates.Add(candidate); } } } if (!candidates.Any()) { node = null; return(false); } node = candidates[Random.Next(candidates.Count)]; return(true); }
private void ConnectToNode(NodeEntry available) { available.Acquire(); var client = new Client(available.Node.EndPoint, certificate, logger); var session = client.NodeSession; session.OnAuthenticated += (local, remote) => OnServerAuthenticated(session, local, remote); session.OnDisconnected += available.Release; // TODO handle when session is disconnected client.Start(); }
// TODO make somehing more intelligent private bool IsReady(NodeEntry candidate, DateTime now) { var elapsed = now - candidate.DisconnectedTime; return(elapsed.Seconds > 10); }