Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сделать загрузку капчи асинхронной #115

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

BoomEaro
Copy link

@BoomEaro BoomEaro commented Jun 13, 2022

  1. Капча загружается параллельно с загрузкой банжикорда и с пониженным приоритетом.
  2. Каждую секунду при генерации капчи, генератор обновляет список капчи, что позволит игрокам заходит на сервер во время генерации.
  3. Добавлена команда /botfilter generate для принудительной ручной регенерации капчи.
  4. Капча сама регенерируется каждые 6 часов. (возможно редко, это стоит еще изучить)
  5. Сообщение о генерации капчи показываются только когда банжикорд был полностью загружен, что исключает путаницу логов.
  6. На каждый поток используется свой рандомный генератор (ThreadLocalRandom)

@@ -113,6 +113,9 @@ public class BungeeCord extends ProxyServer
* Current operation state.
*/
public volatile boolean isRunning;
//BotFilter
@Getter
private volatile boolean enabled;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why volatile? I don't see the use of it in this scenario

{
CaptchaGeneration.generateImages();
} catch ( CaptchaGenerationException ignored )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't just ignore Exceptions. Either provide a config option for that or print a message every time

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't just ignore Exceptions. Either provide a config option for that or print a message every time

The main reason why the exception is ignored here is that when the user executes the /botfilter reload command, it may happen that at that moment the captcha is already in the process of being generated (for example, execute the /botfilter reload command twice), in this case it makes no sense to show the stacktrace to the user in which has no information at all.
I think this problem can be solved if you forcibly stop the generation and start it again

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

@@ -52,6 +55,16 @@ public void execute(CommandSender sender, String[] args)
{
export( sender, args );
sender.sendMessage( "§aКоманда выполнена" );
} else if ( args[0].equalsIgnoreCase( "generate" ) )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what is the command? The captchas are being generated from the startup anyway

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what is the command? The captchas are being generated from the startup anyway

Custom ability to regenerate captcha at any time. There is an option for automatic regeneration, so I thought it would be nice to manually regenerate when needed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you should really add this. Some people might don't get it and just spam the command because nothing happens. You could at least provide some type of boolean so that you can't spam it until it is done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you should really add this. Some people might don't get it and just spam the command because nothing happens. You could at least provide some type of boolean so that you can't spam it until it is done.

There is an exception interception, and an error message output to the user if the captcha is already in the process of being generated. The command is only for the console, players cannot execute it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, sorry for that haha

private static final int PACKETID_18 = 0x34;
private static final int PACKETID_19and119 = 0x24;

private static final int PACKETID_113and114and116 = 0x26;
private static final int PACKETID_115and117 = 0x27;
private static final int PACKETID_1162 = 0x25;
private final Random random = new Random();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not ThreadLocalRandom

{
Thread.sleep( 1000L );
} catch ( InterruptedException ex1 )
List<Font> fonts = Arrays.asList(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to make the Fonts configurable in the config

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to make the Fonts configurable in the config

This is a good idea, however, this pull request is specifically about asynchronous captcha generation, adding other features, as I understand it, should be in other pull requests, since the idea with asynchronous captcha may not be liked by the author of the project.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it was just a recommendation :P


//Окончательно устанавливаем оставшиеся капчи
PacketUtils.captchas.setCaptchas( new ArrayList<>( holders ) );
System.gc();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't all the GC tbh

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't all the GC tbh

The captcha generation behavior is identical to how it was, including the call to System.gc(), and there is nothing bad about it, in fact, a lot of memory is consumed during generation, it's not a problem to always call the garbage collector at the end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoJokeFNA
Copy link

And could you provide some testing results? Like before and after (time in ms)? This would be great.

@BoomEaro
Copy link
Author

And could you provide some testing results? Like before and after (time in ms)? This would be great.

Captcha generation performance has not changed, the only advantage is that you do not have to wait for captcha generation at the very beginning before players can join to BungeeCord.
The purpose of this pull request is not to speed up the generation, but to do it in a different thread

public class CaptchaPainter
{

private final int width = 128;
private final int height = 128;
private final Color background = Color.WHITE;
private final Random rnd = new Random();
private final Random rnd;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just throw NPE's, please fix it!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just throw NPE's, please fix it!

The class is annotated lombok with @requiredargsconstructor annotation
It generates a constructor where there is Random

New CaptchaPainter instances are created in the CaptchaGenerationTask class always with Random instances inside

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants