The interviewer asked me the application scenarios of swoole, I was confused!

Introduction to application scenarios

  • Communication with hardware equipment (location equipment)
  • IM system (used for chat communication on live page)

Scenario 1-Real-time collection of positioning data and real-time output (for example, Didi driver driving track)

Description:

Need to receive all the positioning equipment in real time, and display the real-time track record on the map

important point:

The first point:

Users 1, 2, 3, and web1 connected to the web1 server can only broadcast users 1, 2, 3 when broadcasting information, and users 4, 5, and 6 connected to web2 cannot be broadcast. Assuming that the scene is chatting, user 1 sends a message, only web1 Server users can see, but web2 users cannot receive

Insert picture description here

The second point: the frequency control of the message, for example: 100 devices, 100 users, 100 devices upload one piece of data per second, which needs to be broadcast to each user in real time, that is, 100*100 = 1W times per second, so it can be summarized Broadcast data every second to all users, etc.

Flow chart of data transmission:

Does not contain business logic, aggregates the messages received by web1, web2, and then broadcasts to web1, web2, and then broadcasts to users

Scenario 2-Only collect positioning devices and put them into storage

Description: Need to store all the data uploaded by the positioning device into the database. There are 7 devices, one piece of data per second, and the individual uses the task function of swoole (post an asynchronous task to the task_worker pool, this function is non-blocking, and the number of worker processes is the same Can be configured) and then call the interface method to store

Server memory alarm problem

Reason: lies in the swoole_server->task function

According to the official introduction, the bottom layer of task uses Unix Socket pipe communication, which is full memory and has no IO consumption. The read and write performance of a single process can reach 1 million/s, and different processes use different channels for communication, which can maximize the use of multi-core.

But if this task is to call the program interface, due to network delay, when the added tasks are greater than the consumed tasks, the memory usage will continue to increase, causing the server's memory to be full.

Solution: Messages control the frequency of incoming tasks. You can define this time and whether it can be delayed according to your own business scenarios. Collect all the data within 1 second and then call the program interface (personal use redis when summarizing). Store in, no need to call interface

Simple code snippets, not complete (for beginners to understand, the official website demo is similar)

function __construct($config) 
{
    
    
    $this->config = $config;


    $this->serv = new Swoole\Server($config['server']['host'], $config['server']['port']);
    // 连接redis
    $this->redis = new Predis\Client($config['redis']);
    $this->storage = new Storage($this->config);

    $this->serv->set([
        'worker_num'      => $this->config['server']['workerNum'],   //工作进程数量
        'daemonize'       => $this->config['server']['daemonize'], //是否作为守护进程
        'task_worker_num' => $this->config['server']['taskWorkerNum'],
    ]);
    $this->serv->on('connect', function ($serv, $fd){
    
    
        $this->onConnect($fd, $serv);
    });
    
    $this->serv->on('receive', function ($serv, $fd, $from_id, $data)  {
    
    
        $this->onReceive($fd, $serv, $data);
    });

    $this->serv->on('Close', function($server, $fd) {
    
    
        $this->onClose($fd, $server);
        
    });
    $this->serv->on('Task', function($server, $task_id, $from_id, $data) {
    
    
        $this->onTask($server, $task_id, $from_id, $data);
        
    });
    $this->serv->on('Finish', function($server, $task_id, $data) {
    
    
        $this->onFinish($server, $task_id, $data);
        
    });

    $this->serv->start();
}

public function onTask($serv, $task_id, $from_id, $data){
    
    
    // insert 方法是通过接口入库
    $this->storage->insert($data);
}
public function onReceive($fd, $serv, $data)
{
    
    
    $this->storage->writeLog('message:'.$data);
    $data = $this->formatData($data, $fd);
    $serv->task($data);
}
public function onClose($fd, $serv) 
{
    
    
    // writeLog 方法是写入log
    $this->storage->writeLog('close fd:'.$fd);
}
public function onFinish($serv, $task_id, $data)
{
    
    
    return '';
}

Scene-IM system

Refer to official github: webim system .
Official wiki: swoole framework wiki

benefit:

  • Encapsulates the model class of the database, the ORM interface of the database
  • Redis package, can achieve multi-instance access
  • The framework has some commonly used methods, like log, etc. (I only use log)
  • webim officially has a demo, you can refer to it

harm:

  • The documentation is particularly incomplete, and a simple implementation will take a long time

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108803510