Skip to content

Commit 61f851a

Browse files
committed
refactor: Update default port and improve error handling
Changes: - Update default port from 11211 to 11016 across all components - Enhance configuration error messages with detailed help text and examples - Improve CLI error handling for server disconnection scenarios - Fix hybrid n-gram generation logic in query and server components - Refine CJK character detection (Kanji-only, excluding Hiragana/Katakana) - Reduce verbose logging (use debug level for progress updates) - Update documentation to reflect new default port
1 parent e10415b commit 61f851a

File tree

17 files changed

+773
-72
lines changed

17 files changed

+773
-72
lines changed

docs/en/libmygramclient.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main() {
4949
// Configure client
5050
ClientConfig config;
5151
config.host = "localhost";
52-
config.port = 11211;
52+
config.port = 11016;
5353
config.timeout_ms = 5000;
5454

5555
// Create client
@@ -283,7 +283,7 @@ int main() {
283283
// Configure client
284284
MygramClientConfig_C config = {
285285
.host = "localhost",
286-
.port = 11211,
286+
.port = 11016,
287287
.timeout_ms = 5000,
288288
.recv_buffer_size = 65536
289289
};
@@ -401,7 +401,7 @@ Napi::Value Search(const Napi::CallbackInfo& info) {
401401
// Create and connect client
402402
MygramClientConfig_C config = {
403403
.host = "localhost",
404-
.port = 11211,
404+
.port = 11016,
405405
.timeout_ms = 5000,
406406
.recv_buffer_size = 65536
407407
};
@@ -449,7 +449,7 @@ NODE_API_MODULE(mygramdb, Init)
449449
450450
#### ClientConfig
451451
- `host` - Server hostname (default: "127.0.0.1")
452-
- `port` - Server port (default: 11211)
452+
- `port` - Server port (default: 11016)
453453
- `timeout_ms` - Connection timeout (default: 5000)
454454
- `recv_buffer_size` - Receive buffer size (default: 65536)
455455

docs/ja/libmygramclient.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int main() {
5151
// クライアント設定
5252
ClientConfig config;
5353
config.host = "localhost";
54-
config.port = 11211;
54+
config.port = 11016;
5555
config.timeout_ms = 5000;
5656

5757
// クライアント作成
@@ -285,7 +285,7 @@ int main() {
285285
// クライアント設定
286286
MygramClientConfig_C config = {
287287
.host = "localhost",
288-
.port = 11211,
288+
.port = 11016,
289289
.timeout_ms = 5000,
290290
.recv_buffer_size = 65536
291291
};
@@ -403,7 +403,7 @@ Napi::Value Search(const Napi::CallbackInfo& info) {
403403
// クライアント作成と接続
404404
MygramClientConfig_C config = {
405405
.host = "localhost",
406-
.port = 11211,
406+
.port = 11016,
407407
.timeout_ms = 5000,
408408
.recv_buffer_size = 65536
409409
};
@@ -452,7 +452,7 @@ NODE_API_MODULE(mygramdb, Init)
452452
#### ClientConfig
453453
454454
- `host` - サーバーホスト名(デフォルト: "127.0.0.1")
455-
- `port` - サーバーポート(デフォルト: 11211
455+
- `port` - サーバーポート(デフォルト: 11016
456456
- `timeout_ms` - 接続タイムアウト(デフォルト: 5000)
457457
- `recv_buffer_size` - 受信バッファサイズ(デフォルト: 65536)
458458

src/cli/mygram-cli.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ char** CommandCompletion(const char* text, int start, int /* end */) {
302302

303303
struct Config {
304304
std::string host = "127.0.0.1";
305-
uint16_t port = 11211;
305+
uint16_t port = 11016;
306306
bool interactive = true;
307307
int retry_count = 0; // Number of retries (0 = no retry)
308308
int retry_interval = 3; // Seconds between retries
@@ -364,7 +364,7 @@ class MygramClient {
364364
std::cerr << "\nPossible reasons:\n";
365365
std::cerr << " 1. MygramDB server is not running\n";
366366
std::cerr << " 2. Server is still initializing (building initial index from MySQL)\n";
367-
std::cerr << " 3. Wrong port (check config.yaml - default is 11211)\n";
367+
std::cerr << " 3. Wrong port (check config.yaml - default is 11016)\n";
368368
std::cerr << "\nTo check server status:\n";
369369
std::cerr << " ps aux | grep mygramdb\n";
370370
std::cerr << " lsof -i -P | grep LISTEN | grep " << config_.port << "\n";
@@ -458,7 +458,12 @@ class MygramClient {
458458
std::string msg = command + "\r\n";
459459
ssize_t sent = send(sock_, msg.c_str(), msg.length(), 0);
460460
if (sent < 0) {
461-
return "(error) Failed to send command: " + std::string(strerror(errno));
461+
int saved_errno = errno;
462+
if (saved_errno == EPIPE || saved_errno == ECONNRESET) {
463+
return "(error) SERVER_DISCONNECTED: Connection lost while sending command. The server may have "
464+
"crashed or been shut down.";
465+
}
466+
return "(error) Failed to send command: " + std::string(strerror(saved_errno));
462467
}
463468

464469
// Receive response
@@ -468,9 +473,20 @@ class MygramClient {
468473
ssize_t received = recv(sock_, buffer, sizeof(buffer) - 1, 0);
469474
if (received <= 0) {
470475
if (received == 0) {
471-
return "(error) Connection closed by server";
476+
return "(error) SERVER_DISCONNECTED: Server closed the connection. This usually means:\n"
477+
" 1. Server was shut down gracefully\n"
478+
" 2. Server crashed or encountered a fatal error\n"
479+
" 3. Server restarted and dropped all connections\n"
480+
"\nTry reconnecting to check if the server is still running.";
481+
}
482+
int saved_errno = errno;
483+
if (saved_errno == ECONNRESET) {
484+
return "(error) SERVER_DISCONNECTED: Connection reset by server. The server may have crashed.";
472485
}
473-
return "(error) Failed to receive response: " + std::string(strerror(errno));
486+
if (saved_errno == ETIMEDOUT) {
487+
return "(error) SERVER_TIMEOUT: Server did not respond in time. It may be under heavy load or frozen.";
488+
}
489+
return "(error) Failed to receive response: " + std::string(strerror(saved_errno));
474490
}
475491

476492
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
@@ -564,6 +580,15 @@ class MygramClient {
564580

565581
// Send command and print response
566582
std::string response = SendCommand(line);
583+
584+
// Check if server disconnected
585+
if (response.find("SERVER_DISCONNECTED") != std::string::npos ||
586+
response.find("SERVER_TIMEOUT") != std::string::npos) {
587+
PrintResponse(response);
588+
std::cout << "\nConnection to server lost. Exiting...\n";
589+
break;
590+
}
591+
567592
PrintResponse(response);
568593
}
569594
}
@@ -735,14 +760,14 @@ void PrintUsage(const char* program_name) {
735760
std::cout << '\n';
736761
std::cout << "Options:" << '\n';
737762
std::cout << " -h HOST Server hostname (default: 127.0.0.1)" << '\n';
738-
std::cout << " -p PORT Server port (default: 11211)" << '\n';
763+
std::cout << " -p PORT Server port (default: 11016)" << '\n';
739764
std::cout << " --retry N Retry connection N times if refused (default: 0)" << '\n';
740765
std::cout << " --wait-ready Keep retrying until server is ready (max 100 attempts)" << '\n';
741766
std::cout << " --help Show this help" << '\n';
742767
std::cout << '\n';
743768
std::cout << "Examples:" << '\n';
744769
std::cout << " " << program_name << " # Interactive mode" << '\n';
745-
std::cout << " " << program_name << " -h localhost -p 11211 # Connect to specific server" << '\n';
770+
std::cout << " " << program_name << " -h localhost -p 11016 # Connect to specific server" << '\n';
746771
std::cout << " " << program_name << " --retry 5 INFO # Retry 5 times if server not ready" << '\n';
747772
std::cout << " " << program_name << " --wait-ready INFO # Wait until server is ready" << '\n';
748773
std::cout << " " << program_name << " SEARCH articles hello # Execute single command" << '\n';

src/client/mygramclient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct ReplicationStatus {
116116
// client settings
117117
struct ClientConfig {
118118
std::string host = "127.0.0.1"; // Server hostname
119-
uint16_t port = 11211; // Default port for MygramDB protocol
119+
uint16_t port = 11016; // Default port for MygramDB protocol
120120
uint32_t timeout_ms = 5000; // Default timeout in milliseconds
121121
uint32_t recv_buffer_size = 65536; // Default buffer size (64KB)
122122
};
@@ -132,7 +132,7 @@ struct ClientConfig {
132132
* @code
133133
* ClientConfig config;
134134
* config.host = "localhost";
135-
* config.port = 11211;
135+
* config.port = 11016;
136136
*
137137
* MygramClient client(config);
138138
* if (auto err = client.Connect()) {

src/client/mygramclient_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ MygramClient_C* mygramclient_create(const MygramClientConfig_C* config) {
7676

7777
ClientConfig cpp_config;
7878
cpp_config.host = (config->host != nullptr) ? config->host : "127.0.0.1";
79-
cpp_config.port = config->port != 0 ? config->port : 11211;
79+
cpp_config.port = config->port != 0 ? config->port : 11016;
8080
cpp_config.timeout_ms = config->timeout_ms != 0 ? config->timeout_ms : 5000;
8181
cpp_config.recv_buffer_size = config->recv_buffer_size != 0 ? config->recv_buffer_size : 65536;
8282

src/client/mygramclient_c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef struct MygramClient_C MygramClient_C;
3333
*/
3434
typedef struct {
3535
const char* host; // Server hostname (default: "127.0.0.1")
36-
uint16_t port; // Server port (default: 11211)
36+
uint16_t port; // Server port (default: 11016)
3737
uint32_t timeout_ms; // Connection timeout in milliseconds (default: 5000)
3838
uint32_t recv_buffer_size; // Receive buffer size (default: 65536)
3939
} MygramClientConfig_C;

0 commit comments

Comments
 (0)