什麼鬼
久違的想更新一下 blog,不知怎的網站突然就蹦了個 500。查了一下 log,似乎是 js, css 的 api requests 太多然後 timeout 了。

嗯?這個 blog 什麼時候變得這麼受歡迎了?這些十成是爬蟲吧,想太多。

是説這個站本來應該可以接納大量 requests 的,而這個 timeout 似乎是 js / css 的 api 服務出問題了。那玩意本來就寫得不好,所以 blog 這邊做了緩存。

但這次噴錯顯然是緩存出了問題,看了一下請求的速度,太概 1 秒有 10 個 requests?然後 js api 那邊肯定處理不了,於是這邊的 api request 就堆積起來一起 timeout 了。

因為緩存那邊用的是 redis,socket 開太多也爆炸了。沒有做 pooling,是説不就是一個 blog 罷了做什麼 pooling 啦!

由於時間限制,總之先弄個快捷的方式,直接將 data 塞進 static var 裏去!反正 js / css 都是固定資源,沒有問題。
diff --git a/api.js b/api.js
index f06d200..7a473af 100644
--- a/api.js
+++ b/api.js
@@ -11,6 +11,8 @@ var conf = cl.load( "config.all" ).botanjs;
 var hash = cl.load( "botansx.utils.hash" );
 var rand = cl.load( "botansx.utils.random" );
 
+var HOT_CACHE = {};
+
 var BotanJS = function()
 {
        events.EventEmitter.call( this );
@@ -89,6 +91,8 @@ BotanJS.prototype.compile = function( type )
 
        var rainet = conf.rinet || conf.serviceUri;
 
+       var hotCache = HOT_CACHE[ type ] = HOT_CACHE[ type ] || {};
+
        if( conf.type && "or".indexOf( conf.type ) != -1 )
        {
                this[ type ] = rainet + conf.type + type + "/" + resList.join( "/" );
@@ -102,8 +106,16 @@ BotanJS.prototype.compile = function( type )
        };
 
 
-       var q = resList.sort().join( "," );
-       var lookupKey = hash.md5( q );
+       var resQuery = resList.sort().join( "," );
+       var lookupKey = hash.md5( resQuery );
+
+       if( hotCache[ lookupKey ] )
+       {
+               Dragonfly.Debug( "HOT_CACHE: " + lookupKey );
+               _self.storeForRender( rainet, type, hotCache[ lookupKey ] );
+               _self.emit( "complete" );
+               return;
+       }
 
        var Session = cl.load( "botansx.modular.session" );
        var fastCache = new Session( lookupKey, "BOTAN_JCACHE", true );
@@ -130,6 +142,8 @@ BotanJS.prototype.compile = function( type )
                                {
                                        _self.storeForRender( rainet, type, ch );
 
+                                       hotCache[ lookupKey ] = ch;
+
                                        Dragonfly.Debug( "Storing " + _trim( ch ) + " to fastcache[ " + fastCache.id + " ]" );
 
                                        fastCache.spawn( 86400 );
@@ -161,7 +175,7 @@ BotanJS.prototype.compile = function( type )
                }
                else
                {
-                       zlib.deflate( q, deflateComplete );
+                       zlib.deflate( resQuery, deflateComplete );
                }
        } );
 };
弄好了之後問題解消了大半。

我是不介意被爬啦,但能不能至少聰明一點爬點有營養的地方?爬 /tag/ 只會走進無限迴廊好不好?我不是在 robots.txt 裏寫明了不要爬 /tag/ 嗎?

這些肯定是 AI 相關的爬蟲吧?算了一下,居然有 20000 個 unique ip

$ kubectl logs blog-dc684b5d5-fl8fc | grep album/tag | cut -c 34- | cut -d' ' -f 1 | sort | uniq | wc -l
Defaulted container "web" out of: web, redis, assets, assets-backup
   21546

這是 botnet 嗎?什麼鬼。

~兩天後~

啊,還是不行⋯⋯今次到 database timeout 了,總之先將 */tags/* 的 requests 卸載到另一個 instance 去吧。還是不行的話就再弄多一個專用的 read-only db 好了。
Tag(s): diary
Profile picture
斟酌 鵬兄
Sun Sep 07 2025 15:17:23 GMT+0000 (Coordinated Universal Time)
Last modified: Tue Sep 09 2025 11:17:48 GMT+0000 (Coordinated Universal Time)
Comments
No comments here.
Do you even comment?
website: 
Not a valid website
Invalid email format
Please enter your email
*Name: 
Please enter a name
Submit
抱歉,Google Recaptcha 服務被牆掉了,所以不能回覆了