NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

from functools import wraps
from datetime import datetime, timedelta

STATE_CLOSED = 'closed'
STATE_OPEN = 'open'
STATE_HALF_OPEN = 'half_open'


class CircuitBreaker(object):
def __init__(self, maxFailures=2, resetTimeout=3, fname=None):
self.maxFailures = maxFailures
self.resetTimeout = resetTimeout
self.openTimestamp = datetime.utcnow()
self.fname = fname
self.failure_count = 0
self.state = STATE_CLOSED

def __call__(self, orig_func):
if self.fname is None:
self.fname = orig_func.__name__

@wraps(orig_func)
def wrapper(*args, **kwargs):
return self.check_state(orig_func, *args, **kwargs)

return wrapper

def check_state(self, funct, *args, **kwargs):

if self.state is STATE_CLOSED:
print(STATE_CLOSED)
try:
result = funct(*args, **kwargs)
except Exception:
self.failure_count += 1
if self.failure_count == self.maxFailures:
self.state = STATE_OPEN
else:
self.failure_count = 0
elif self.state is STATE_OPEN:
print(STATE_OPEN)
if self.open_remaining() <= 0:
self.state = STATE_HALF_OPEN
raise CircuitBreakerOpenException("CircuitBreakerOpenException")

if self.state is STATE_HALF_OPEN:
print(STATE_HALF_OPEN)
try:
result = funct(*args, **kwargs)
except Exception:
self.state = STATE_OPEN
raise CircuitBreakerOpenException("CircuitBreakerOpenException")
else:
self.openTimestamp = datetime.utcnow()
self.failure_count = 0
self.state = STATE_CLOSED

def open_until(self):
return self.openTimestamp + timedelta(seconds=self.resetTimeout)

def open_remaining(self):
return (self.open_until() - datetime.utcnow()).total_seconds()


class CircuitBreakerOpenException(Exception):
pass

********************************************************************************************
from datetime import datetime

from collections import OrderedDict
from operator import itemgetter

from twisted.internet import reactor
import twisted.internet.defer, json
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent, readBody
from twisted.web.http_headers import Headers


class BeginningPrinter(Protocol):
def __init__(self, whenFinished):
self.whenFinished = whenFinished

def dataReceived(self, bytes):
print '##### Received #####n%s' % (bytes,)

def connectionLost(self, reason):
print 'Finished:', reason.getErrorMessage()
self.whenFinished.callback(None)

arr = {}
shortestLat = {}

def handleResponse(response):
#print "version=%sncode=%snphrase='%s'" % (r.version, r.code, r.phrase)
#for k, v in r.headers.getAllRawHeaders():
#print "%s: %s" % (k, 'n '.join(v))
times = response.headers.getRawHeaders(name="Date", default=None)
if times is not None:
times = datetime.strptime(times[0], "%a, %d %b %Y %H:%M:%S %Z")
#print(times)
#print(r.request.absoluteURI)
arr[response.request.absoluteURI] = times
whenFinished = readBody(response).addCallback(cbBody)
return whenFinished

def cbBody(body):
try:
json_response = json.loads(body)
print(json_response)
except ValueError:
print("Invalid JSON")
#print('Response body:')
#print(body)

def handleError(reason):
reason.printTraceback()
reactor.stop()

def getPage(url):
#print "Requesting %s" % (url,)
#print(datetime.utcnow())
d = Agent(reactor).request('GET', url, Headers({'x-request-sent-at': [str(datetime.utcnow())]}), None)
d.addCallbacks(handleResponse, handleError)
return d

def printSever(ignored):
shortestLat = OrderedDict(sorted(arr.items(), key=itemgetter(1)))
print(shortestLat)
print(shortestLat.keys()[0])

semaphore = twisted.internet.defer.DeferredSemaphore(2)
dl = list()
dl.append(semaphore.run(getPage, 'http://google.com'))
dl.append(semaphore.run(getPage, 'http://cnn.com'))
dl.append(semaphore.run(getPage, 'http://requestb.in/1koyzc61'))
dl.append(semaphore.run(getPage, 'https://jsonplaceholder.typicode.com/posts/1'))
dl = twisted.internet.defer.DeferredList(dl)
dl.addCallbacks(lambda x: reactor.stop(), handleError)
dl.addCallback(printSever)

reactor.run()
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.