<commit_before>#include "db.h"
#include <stdlib.h>

using namespace std;

void CAddrInfo::Update(bool good) {
  uint32_t now = time(NULL);
  if (ourLastTry == 0)
    ourLastTry = now - MIN_RETRY;
  int age = now - ourLastTry;
  lastTry = now;
  ourLastTry = now;
  total++;
  if (good) success++;
  stat2H.Update(good, age, 3600*2);
  stat8H.Update(good, age, 3600*8);
  stat1D.Update(good, age, 3600*24);
  stat1W.Update(good, age, 3600*24*7);
  stat1M.Update(good, age, 3600*24*30);
  int ign = GetIgnoreTime();
  if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now;
//  printf("%s: got %s result: success=%i/%i; 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", success, total, 
//  100.0 * stat2H.reliability, 100.0 * (stat2H.reliability + 1.0 - stat2H.weight), stat2H.count,
//  100.0 * stat8H.reliability, 100.0 * (stat8H.reliability + 1.0 - stat8H.weight), stat8H.count,
//  100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - stat1D.weight), stat1D.count,
//  100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count);
}

bool CAddrDb::Get_(CService &ip, int &wait) {
  int64 now = time(NULL);
  int cont = 0;
  int tot = unkId.size();
  do {
    deque<int>::iterator it = ourId.begin();
    while (it < ourId.end()) {
      if (now - idToInfo[*it].ourLastTry > MIN_RETRY) {
        tot++;
        it++;
      } else {
        break;
      }
    }
    if (tot == 0) {
      if (ourId.size() > 0) {
        wait = MIN_RETRY - (now - idToInfo[ourId.front()].ourLastTry);
      } else {
        wait = 5;
      }
      return false;
    }
    int rnd = rand() % tot;
    int ret;
    if (rnd < unkId.size()) {
      if (rnd*10 < unkId.size()) {
        // once every 10 attempts, restart with the oldest unknown IP
        set<int>::iterator it = unkId.begin();
        ret = *it;
      } else {
        // 90% of the time try the last learned IP
        set<int>::reverse_iterator it = unkId.rbegin();
        ret = *it;
      }
      unkId.erase(ret);
    } else {
      ret = ourId.front();<commit_msg>Remove weird 10% inverted selection logic<commit_after>#include "db.h"
#include <stdlib.h>

using namespace std;

void CAddrInfo::Update(bool good) {
  uint32_t now = time(NULL);
  if (ourLastTry == 0)
    ourLastTry = now - MIN_RETRY;
  int age = now - ourLastTry;
  lastTry = now;
  ourLastTry = now;
  total++;
  if (good) success++;
  stat2H.Update(good, age, 3600*2);
  stat8H.Update(good, age, 3600*8);
  stat1D.Update(good, age, 3600*24);
  stat1W.Update(good, age, 3600*24*7);
  stat1M.Update(good, age, 3600*24*30);
  int ign = GetIgnoreTime();
  if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now;
//  printf("%s: got %s result: success=%i/%i; 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", success, total, 
//  100.0 * stat2H.reliability, 100.0 * (stat2H.reliability + 1.0 - stat2H.weight), stat2H.count,
//  100.0 * stat8H.reliability, 100.0 * (stat8H.reliability + 1.0 - stat8H.weight), stat8H.count,
//  100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - stat1D.weight), stat1D.count,
//  100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count);
}

bool CAddrDb::Get_(CService &ip, int &wait) {
  int64 now = time(NULL);
  int cont = 0;
  int tot = unkId.size();
  do {
    deque<int>::iterator it = ourId.begin();
    while (it < ourId.end()) {
      if (now - idToInfo[*it].ourLastTry > MIN_RETRY) {
        tot++;
        it++;
      } else {
        break;
      }
    }
    if (tot == 0) {
      if (ourId.size() > 0) {
        wait = MIN_RETRY - (now - idToInfo[ourId.front()].ourLastTry);
      } else {
        wait = 5;
      }
      return false;
    }
    int rnd = rand() % tot;
    int ret;
    if (rnd < unkId.size()) {
      set<int>::iterator it = unkId.end(); it--;
      ret = *it;
      unkId.erase(it);
    } else {
      ret = ourId.front();
{'fraction_non_alphanumeric': 0.19416767922235723, 'fraction_numerical': 0.059295261239368166, 'mean_word_length': 2.6554174067495557, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2568844', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1670, 'n_words': 357}