-
|
rr version 2025.1.8 (build time: 2026-02-19T14:58:23+0000, go1.26.0), OS: linux, arch: amd64 version: "3"
server:
command: "php roadrunner/roadrunner.php"
env:
- XDEBUG_SESSION: 1
grpc:
listen: "tcp://:9003"
proto:
- "grpc.proto"
pool:
debug: false
num_workers: 10
max_jobs: 0
allocate_timeout: 60s
reset_timeout: 60s
destroy_timeout: 60s
# supervisor:
# watch_tick: 60s
# ttl: 120s
# exec_ttl: 60s
logs:
level: debug
mode: development
rpc:
listen: tcp://:50052<?php
require_once __DIR__ . '/vendor/autoload.php';
use GameGrpcServer\Processor\GrpcProcessorInterface;
use Spiral\RoadRunner\GRPC\Invoker;
use Spiral\RoadRunner\GRPC\Server;
use Spiral\RoadRunner\Worker;
use GameGrpcServer\Processor\Request;
use GameGrpcServer\Processor\Response;
use Spiral\RoadRunner\GRPC\ContextInterface;
class Processor implements GrpcProcessorInterface
{
public function Process(ContextInterface $ctx, Request $in): Response
{
static $db = null;
if ($db === null) {
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
'127.0.0.1',
33166,
'database',
'utf8'
);
try {
$db = new PDO($dsn, 'root', 'password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
]);
} catch (PDOException $e) {
throw new RuntimeException('db connect failed:' . $e->getMessage());
}
}
try {
$statement = $db->query("select name from user_info where user_id=51");
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
return new Response(['message' => json_encode($rows)]);
} catch (Throwable $exception) {
return new Response(['message' => $exception->getMessage()]);
}
}
}
$server = new Server(new Invoker(), [
'debug' => true, // optional (default: false)
]);
$server->registerService(GrpcProcessorInterface::class, new Processor());
$server->serve(Worker::create());
When rr runs for a while(may be seconds, may be 1 to 2 mintues), i use postman send grpc request continuly, the query will throw exception mysql has gone away. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
MySQL has a connection lifetime. It expires whether you actively use it or not. There are two ways to solve this problem:
|
Beta Was this translation helpful? Give feedback.
-
|
I just wonder why it has different behavir compare with run in php cli mode |
Beta Was this translation helpful? Give feedback.
MySQL has a connection lifetime. It expires whether you actively use it or not. There are two ways to solve this problem: