Skip to content

Instantly share code, notes, and snippets.

@maiha
maiha / sse.md
Last active April 25, 2024 06:14
Nginx configuration Cheat Sheet

nginx

location /sse/ {
  # enables EventSource support
  proxy_http_version 1.1;
  proxy_set_header Connection "";

  proxy_buffering off;
}
@maiha
maiha / 0.md
Last active March 9, 2024 17:58
practical migrating guide to crystal-0.20.4
@maiha
maiha / RateLimit.md
Last active January 15, 2024 02:02
RateLimitアルゴリズムまとめまとめ (→ Sliding window countersでOK)

TL;DR

  • Leaky bucket : キューイングして出力量(consume)で調節 (アルゴリズムというかキューを使って調節)
  • Token bucket : 定期的に資源を追加し平均値を調節。バーストに強い (ソシャゲのスタミナ回復)
  • Fixed window counters : 単位時間あたりの上限値を指定。キューを使わないLeaky bucket。境界の2倍問題あり
  • Sliding window log : 総数(counters)でなくアクセス時刻(log)で管理。境界問題を解消する一方、消費増加がぱない(IntArray(Time))
  • Sliding window counters : countersのままで、単位時間の割合計算によって境界問題を解消。これにしておけば間違いない

参考: 様々なrate limitアルゴリズム

microservice(MS)

1つのアプリケーションを機能別にAPIとして切り出したもの。大規模化・複雑化が進むにつれ、開発コストおよび新規メンバーの参入コストが高くなるため、2014年頃から流行りだした。対して、もともとの1枚岩のアプリケーションはmonolithicと呼ばれる。日本だとモノリスとも言われる。

microserviceのデメリット

小さくなったのは良いが、「連携設定」「APIのセキュリティ」「リトライ処理」などコストが増した。また、個数の増加に伴い、新しいMSを追加したときの影響範囲も分かりづらく、それらを解消するために「監視体制」「サーキットブレイク」なども必要となり、複雑性を増していった。

microserviceの課題

### global
[vars]
var qname  = "foo"

### job request
var data = "bar"
var id   = (redis) INCR /job/${qname}/ids/max
(redis) HSET  /job/${qname}/datas ${id} ${data}
(redis) LPUSH /job/${qname}/ids ${id}
@maiha
maiha / Repeat.scala
Created September 11, 2014 18:10
gatling: repeat example
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
class Repeat extends Simulation {
val httpProtocol = http.baseURL("http://localhost").inferHtmlResources()
val scn = scenario("repeat test").repeat(10) { exec(http("Home").get("/")) }
setUp(scn.inject(rampUsers(10) over(1)).protocols(httpProtocol))
}
@maiha
maiha / raven.cr.md
Created March 1, 2021 08:42
raven : An unofficial Crystal-language client and integration layer for the Sentry error reporting API.
    private def current_environment_from_env
      ENV["SENTRY_ENVIRONMENT"]? || "default"
 
    private def detect_release_from_env
      ENV["SENTRY_RELEASE"]?