예제 #1
0
            public AddressModel GetAddress()
            {
                int                totalWeight  = 0;
                long               maxCurrent   = long.MinValue;
                var                now          = DateTimeConverter.DateTimeToUnixTimestamp(DateTime.Now);
                AddressModel       selectedAddr = null;
                WeightedRoundRobin selectedWRR  = null;

                foreach (var address in _address)
                {
                    var identifyString     = address.ToString();
                    int weight             = GetWeight(address);
                    var weightedRoundRobin = _concurrent.GetOrAdd(identifyString, k => new Lazy <WeightedRoundRobin>(() =>
                    {
                        WeightedRoundRobin wrr = new WeightedRoundRobin();
                        wrr.SetWeight(weight);
                        return(wrr);
                    })).Value;
                    if (weight != weightedRoundRobin.GetWeight())
                    {
                        //weight changed
                        weightedRoundRobin.SetWeight(weight);
                    }
                    long cur = weightedRoundRobin.IncreaseCurrent();
                    weightedRoundRobin.SetLastUpdate(now);
                    if (cur > maxCurrent)
                    {
                        maxCurrent   = cur;
                        selectedAddr = address;
                        selectedWRR  = weightedRoundRobin;
                    }
                    totalWeight += weight;
                }

                if (_address.Count() != _concurrent.Count())
                {
                    var concurrentsToRemove = _concurrent.Where(p => (now - p.Value.Value.GetLastUpdate()) * 1000 > RECYCLE_PERIOD).ToList();
                    concurrentsToRemove.ForEach(concurrent => _concurrent.TryRemove(concurrent.Key, out Lazy <WeightedRoundRobin> obj));
                }
                if (selectedAddr != null)
                {
                    selectedWRR.Sel(totalWeight);
                    return(selectedAddr);
                }
                return(_address[0]);
            }
예제 #2
0
 public DbCommandInterceptor(params string[] slaveConnectionString)
 {
     weightedRoundRobin = new WeightedRoundRobin(this.GetServerObject(slaveConnectionString));
 }