Skip to content

Instantly share code, notes, and snippets.

View zhoudaxia233's full-sized avatar
🎯
Focusing

Zheng Zhou zhoudaxia233

🎯
Focusing
View GitHub Profile
@zhoudaxia233
zhoudaxia233 / coro_life.py
Created April 20, 2020 21:03 — forked from ramalho/coro_life.py
John Conway's Game of Life implemented with coroutines, by Brett Slatkin
#!/usr/bin/env python3
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

@zhoudaxia233
zhoudaxia233 / README.md
Created September 13, 2022 12:22 — forked from thelonecabbage/README.md
django, current user

(cliped from http://stackoverflow.com/a/21786764/117292)

The least obstrusive way is to use a CurrentUserMiddleware to store the current user in a thread local object:

current_user.py

from threading import local

_user = local()

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.