Posted on Leave a comment

[Java Concurrency] 01: Thread & Process

What is a thread?

Thread is the smallest execution unit that can be schedule by the operating system.

A process is a running program. When you start a program, you create a process.
In one process, there can be more than one thread.

Threads in a process share an environment created by the process.

multi threads in a process sharing memory

System vs user-defined threads


All java applications are multi-threaded. There are threads created by the developer and threads created by the system.
Garbage collection is one example of thread created by the system. In the other hand, if your application doing something, for example processing image, there is a thread for that task. It is the one created by you, the developer.

Thread scheduler

Most of the time, there are more tasks that the number of CPU core. Thus, it is neccessary for the CPU to schedule
when to run which task. This scheduling is handled by the thread scheduler.

Context switching

Context switching happens when one CPU core has to stop running one task and switch to running other task. One possible
reason is, for example, task A is allocated a time segment of 10ms. After 10ms, even if task A hasn’t finished yet, the
thread scheduler need to switch to running other task.
Context switching is not free. It costs CPU resources to stop executing one task and start running other.

Thread priority

One thread can have priority from 1 to 10. The thread scheduler take this number into consideration to decide which
task to run.

In Java programming, by default, a thead has priority 5.

Consider this code:

public class D_Thread {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> System.out.println("Current thread priority: " + Thread.currentThread().getPriority()));
        t1.start();
    }
}

This is the result in console:

Posted on Leave a comment

Configure HAProxy to Accept Preflight Requests

Recently I developed an API with Spring Boot. All the tests on the local environment went well with Postman. However, when deploying to prod through HAProxy, I got this error:

There are also other errors regarding missing headers, origin…

There are some solutions available, such as add code to my Spring Boot code base. I don’t like this method since it’s not pure.

Why adding non-business code in API.

That’s why I need to find another solution.

It turned out, the solution is simple.

First, add a new backend only for serving CORS in HAProxy:

backend cors_backend
  http-after-response set-header Access-Control-Allow-Origin "*"
  http-after-response set-header Access-Control-Allow-Headers "*"
  http-after-response set-header Access-Control-Max-Age "31536000"
  http-request return status 200

Now, in the frontend part, add a check for OPTIONS request and direct that request to this backend:

frontend your_fe
  ...
   acl is_options method OPTIONS
   
...

  use_backend cors_backend if is_options
  # put your actual backend below this

Of course, in your actual backend, you need to set the CORS headers:

backend your_main_backend
  ...

  # START CORS
  http-response add-header Access-Control-Allow-Origin "*"
  http-response add-header Access-Control-Allow-Headers "*"
  http-response add-header Access-Control-Max-Age 3600
  http-response add-header Access-Control-Allow-Methods "GET, DELETE, OPTIONS, POST, PUT, PATCH"
  # END CORS

  server w1 server:8080

And that’s all you need. Now you can work with your API CORS-free

Posted on Leave a comment

Fix Angular 404 on reload Nginx

To keep it short, here is the code you need to replace in /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

You may want to update server_name to your domain name if you are not deploying in docker. Otherwise, this config works with docker.

Give it a try, you’ll see that the 404 page is gone when you refresh your site.