mediasoup study notes [1] worker

If you have been in contact with nodejs before, you should have some understanding of cluster. Whether it is expressjs, eggjs and other frameworks, multi-process solutions are enabled to improve performance. Of course mediasoup is no exception.

Today, let’s recall the node module cluster.
The example given on the official website can be clear when you open the code.
Here, the main process and the child process are distinguished. The main process is not responsible for the business code part, but is only responsible for the monitoring of the child process.
Cluster fork is to start a process to execute the non-master code part again;

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    
    
  console.log(`Master ${
      
      process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    
    
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    
    
    console.log(`worker ${
      
      worker.process.pid} died`);
  });
} else {
    
    
//something
  console.log(process.pid)
}

The method of communication between processes, or the method of sharing data, of course, the real method is database or redis, etc.

Take a practical example, the user enters the room, the current number of people in the room.
For example, if four people enter the same room, they are probably not processed by the same process. This requires a place to centrally organize the room and the number of people in the room.
You can see the following code

let users = new Map()
if(cluster.isMaster){
    
    
	const worker = cluster.fork()
	worker.send('other something');
	worker.on('message', function(message){
    
    
	   switch(message.action){
    
    
			case 'join':
			   users.set(messages.data.userId, message.data)
			   break;
			case 'leave':
			   users.delete(message.data.userId);
			   break;
			default:
			   log('unknow action name')
		}
	})
} else {
    
    

   //这里是业务代码
   users是可以直接获取的读取的,
   process.on('message', reciveMessageHandle) //接收主进程的消息
   //当房间有人进来的时候
   process.send({
    
    action:'join',data:{
    
    userId}});

}

It should be easier to understand after reading the code. Of course, this is just a demonstration of the demo code.
Under the expansion, you can further consider, the daemon process will never hang up (of course a bit exaggerated); the main process listens to the abnormal exit of the child process and immediately fork. Of course this is a violent way, but you can add it to the method. Alarm notification (SMS, email), etc.

mediasoup createWorker related parameters

  • logLevel: "debug", "warn", "error" and "none", //not much to say about this
  • logTags :‘info’,‘ice’,‘dtls’,‘rtp’,‘srtp’,‘rtcp’,‘rtx’,‘bwe’,‘score’,‘simulcast’,‘svc’,‘sctp’
  • rtcMinPort: minimum port
  • rtcMaxPort: Maximum port
  • dtlsCertificateFile, dtlsPrivateKeyFile certificate related
  • appData
const mediasoup = require('mediasoup');
const worker = mediasoup.createWorker({
    
    ...options})

#2020 Learn something during the epidemic#

Guess you like

Origin blog.csdn.net/uk_51/article/details/104628175