查找应用未使用的空闲端口-查找一些算法 [英] Find free port not in use for apps - find some algorithm

查看:57
本文介绍了查找应用未使用的空闲端口-查找一些算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中使用以下API销毁了免费端口,并将其提供给应用程序以运行

  portscanner.findAPortNotInUse(3000,65000,'127.0.0.1',function(error,port){console.log('AVAILABLE PORT AT:'+端口)}) 

https://github.com/baalexander/node-portscanner

此免费端口已提供给应用程序使用和正常运行.问题是,如果我为 application A 提供了一个免费端口,而该应用程序尚未被占用(有时会花费一些时间...),并且还会有其他 application B并请求一个免费端口,以便它为 APP B提供应用A的端口导致问题的原因...有什么优雅的方法可以解决吗?

我的应用程序没有状态,因此无法保存到哪个应用程序获取哪个端口...

有一种解决方案,我们可以将范围随机化,但这并不可靠...

在我的应用程序中,我正在获取应提供免费端口以运行的应用程序的URL.

更新

不能使用一些经纪人或其他东西可以在外部进行控制,我需要找到一些算法(也许有一些智能随机数)来帮助我做到这一点在内部,即我的程序就像单例,我需要一些技巧来在 50000到65000 之间分配端口,以减少提供给应用程序的端口的冲突量/p>

更新2

我已决定尝试以下方法,您认为呢?

使用lodash https://lodash.com/docs/4.17.2#random确定with循环之间的端口,这些循环可为以下范围提供3(或更多,如果有意义的话)数字

  portscanner.findAPortNotInUse([50001,60000,600010],'127.0.0.1',function(err,port){if(err){console.log(错误!!!->" + err);}别的 {console.log('Port Not in Use'+ port);}//循环使用var aa = _.random(50000,65000); 

然后如果我在端口中输入了错误,即所有3个端口都被占用,请针对其他3个随机数再次运行此过程.欢迎提出建议!!!!我试图找到一种避免碰撞的方法 ...

解决方案

我只是接受一个事实,那就是分布式系统中可能会出现问题,如果由于某种原因而失败,则重试该操作(即获得一个免费端口)第一次尝试.

幸运的是,有很多npm模块已经为您完成了此任务,例如重试.

使用此模块,您可以重试异步操作,直到成功为止,并配置等待策略,以及应最大尝试重试多少次,依此类推……

要提供一个代码示例,基本上可以归结为以下内容:

  const operation = retry.operation();operation.attempt(currentAttempt => {findAnUnusedPortAndUseIt(err => {如果(operation.retry(err)){返回;}callback(err?operation.mainError():null);});}); 

此解决方案的优点是:

  • 无需锁定即可工作,也就是说,如果一切正常,它是高效的并且资源使用率较低.
  • 没有中央经纪人或类似的东西就可以工作.
  • 适用于任何规模的分布式系统.
  • 使用一种模式,您可以在分布式系统中重复使用该模式来解决各种问题.
  • 使用经过战斗力测试和可靠的npm模块,而不是手写所有这些东西.
  • 不需要您大修改代码,只需添加几行即可.

希望这会有所帮助:-)

I use the following API in my program to detrmine free port and provide it to application to run

portscanner.findAPortNotInUse(3000, 65000, '127.0.0.1', function(error, port) {
  console.log('AVAILABLE PORT AT: ' + port)
})

https://github.com/baalexander/node-portscanner

This free port are given to application for use and working OK. The problem is that if I provide a free port to application A and the application is doesn't occupied it yet(sometimes it takes some time...) and there is coming other application B and request a free port so it give to APP B the port of app A Which cause to problem... is there any elegant way to solve it?

my application doesn't have state so it cannot save to which app get which port...

There is solution that we can randomize the range but this is not robust ...

In my application Im getting the URL of the app that I should provide the free port to run.

update

I cannot use some broker or someting else that will controll this outside I need to find some algorithm (maybe with some smart random ) that can help me to do it internally i.e. my program is like singleton and I need some trick how to give port between 50000 to 65000 that will reduce the amount of collision of port that was provided to the apps

update 2

I've decided to try something like the following what do you think ?

using lodash https://lodash.com/docs/4.17.2#random to determine ports between with loops that provide 3(or more if that make sense) numbers for ranges like following

portscanner.findAPortNotInUse([50001, 60000, 600010], '127.0.0.1', function(err, port) {
    if(err) {
        console.log("error!!!-> " +err);
    }else {
        console.log('Port Not in Use ' + port);
    }

//using that in a loop 
var aa = _.random(50000, 65000); 

Then If I got false in the port i.e. all 3 port are occupied ,run this process again for 3 other random number.comments suggestion are welcomed!!! I try to find some way to avoid collision as much as possible...

解决方案

I would simply accept the fact that things can go wrong in a distributed system and retry the operation (i.e., getting a free port) if it failed for whatever reason on the first attempt.

Luckily, there are lots of npm modules out there that do that already for you, e.g. retry.

Using this module you can retry an asynchronous operation until it succeeds, and configure waiting strategies, and how many times it should be retried maximally, and so on…

To provide a code example, it basically comes down to something such as:

const operation = retry.operation();

operation.attempt(currentAttempt => {
  findAnUnusedPortAndUseIt(err => {
    if (operation.retry(err)) {
      return;
    }

    callback(err ? operation.mainError() : null);
  });
});

The benefits of this solution are:

  • Works without locking, i.e. it is efficient and makes low usage of resources if everything is fine.
  • Works without a central broker or something like that.
  • Works for distributed systems of any size.
  • Uses a pattern that you can re-use in distributed systems for all kinds of problems.
  • Uses a battle-tested and solid npm module instead of handwriting all these things.
  • Does not require you to change your code in a major way, instead it is just adding a few lines.

Hope this helps :-)

这篇关于查找应用未使用的空闲端口-查找一些算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆