<commit_before>#!/usr/bin/python

import os
import sys
import time
import select
import socket
import requests
import resolver

WAIT    = float(os.environ.get("wait", "0.1"))
CONSUL  = os.environ.get("consul", "127.0.0.1")
HAPROXY = os.environ.get("haproxy", "127.0.0.1")

def connect (s, addr):
    try:
        s.connect(addr)
    except BlockingIOError as e:
        p = select.select([], [s], [], 1)
        if (p[1] == []):
            raise

def ping (addr):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK, 0)
    try:
        connect(s, addr)
        s.send(b"ping")
        p = select.select([s], [], [], 1)
        if (p[0] == []):
            return(b"fail")
        else:
            return(s.recv(512, socket.MSG_DONTWAIT))
    finally:
        s.close()

def repeat (s):
    while True:
        yield (s, 4500)
            
def client (servers):
    c  = {}
    t0 = time.time()
    for s in servers:
        t1 = time.time()
        time.sleep(WAIT)
        if (s is None):
            print("fail: no servers")
            continue
        try:
            m = ping(s)
            if (t1 - t0 >= 1):
                for k in sorted(c.keys()):
                    print("%s [%d]" % (k, c[k]))
                print("--")
                c  = {}
                t0 = t1
            if (len(m) == 0):
                print("fail: connection closed")
            else:
                k = m.decode("utf8").strip()
                c[k] = c.get(k, 0) + 1
        except:
            print("fail: can't connect")

def usage (p):
    print("use: %s local|remote" % p)
            
resolver.start(CONSUL)
if (len(sys.argv) >= 2):
    if (sys.argv[1] == "local"):<commit_msg>summarize errors and loop with no sleep<commit_after>#!/usr/bin/python

import os
import sys
import time
import select
import socket
import requests
import resolver

WAIT    = float(os.environ.get("wait", "0.1"))
CONSUL  = os.environ.get("consul", "127.0.0.1")
HAPROXY = os.environ.get("haproxy", "127.0.0.1")

def connect (s, addr):
    try:
        s.connect(addr)
    except BlockingIOError as e:
        p = select.select([], [s], [], 1)
        if (p[1] == []):
            raise

def ping (addr):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK, 0)
    try:
        connect(s, addr)
        s.send(b"ping")
        p = select.select([s], [], [], 1)
        if (p[0] == []):
            return(b"fail")
        else:
            return(s.recv(512, socket.MSG_DONTWAIT))
    finally:
        s.close()

def repeat (s):
    while True:
        yield (s, 4500)

def pingk (s):
    ke = "fail"
    try:
        m = ping(s)
        if (len(m) == 0):
            k = ke
        else:
            k = m.decode("utf8").strip()
    except:
        k = ke
    return(k)

def dump (c, t0):
    t1 = time.time()
    if (t1 - t0 >= 1):
        for k in sorted(c.keys()):
            print("%s [%d]" % (k, c[k]))
        print("--")
        return({}, t1)
    return(c, t0)

def client (servers):
    c  = {}
    t0 = time.time()
    for s in servers:
        if (s is None):
            k = "error"
        else:
            k = pingk(s)
        c[k] = c.get(k, 0) + 1
        c, t0 = dump(c, t0)

def usage (p):
    print("use: %s local|remote" % p)
            
resolver.start(CONSUL)
if (len(sys.argv) >= 2):
    if (sys.argv[1] == "local"):
{'fraction_non_alphanumeric': 0.1688849452824608, 'fraction_numerical': 0.023957409050576754, 'mean_word_length': 1.669297553275454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '173009', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1219, 'n_words': 304}