Skip to main content

Posts

Building Reducer Using Redux Toolkit

Redux Toolkit is one of the libraries that can help us manage our application state which depends on Redux. It allows us to create reducers within our application and separate each reducer from one another based on any criteria that we desired in a specific kind of module. There are some approaches that we can utilize to build reducers in Redux Toolkit. Basic Shape const slice = createSlice({ // ... reducers: { increment: (state) => { state += 1; } } }); On the code above, it seems that we mutate the state, but actually, Redux Toolkit utilizes Immer in the backstage so it will be translated into immutable operation. Then, unlike legacy libraries that require us to manually define the action that will be dispatched to be handled by a specific reducer, Redux Toolkit can generate action creators for us. const { increment } = slice.actions; // "increment" is the action creator Then, we can dispatch the action inside our compone...

Double Invoking In React Application with Strict Mode Enabled

When we use create-react-app to generate our application source code, we will be provided with a base code that enables  React.StrictMode by default. This wrapper component doesn't render an HTML element but it performs useful functionality to verify that our application is safe for production. It checks for legacy or deprecated React functions or APIs, unexpected side-effects, and unsafe lifecycle. We can read the details about those functionalities on its documentation page . To help us spot side-effects, React will double-invoking several functions in our application such as render , constructor , functions passed in useMemo or useReducer , and so on. By double-invoking such functions, we can detect if unexpected results will show up. But, this process will be initiated only in development mode. When we ship our application in a production environment with Strict Mode enabled, double-invoking will never happen. This is why when we render a component that contains useEffect ...

Tokyo - Yui

 

Limiting Bitrate and Network Throttling

We may limit incoming or outcoming data rates to/from our infrastructure to maintain the stability of our service for customers. Bitrate limitation is an action to limit the number of bits that can be passed through a transmission channel in a period of time. Network throttling is an intentional action to slow down transmission speed in a network channel. It is not only about limiting bitrate but also limiting the allowed number of requests in a period of time. There are several tools and techniques that can be used to apply bitrate limitation and network throttling. Wondershaper It is an easy-to-use tool for Linux and is already in the package repository. It can limit the bit rate that can be achieved by network interfaces in the system. We can install it by running the following command. apt install wondershaper We can choose an interface to have a limitation either or both on download and upload. wondershaper <interface-name> <download-rate-in-bps> <upload-...

Handle File Upload with Express and Multer

Express undoubtedly has become a popular framework for building web applications based on Node.js. It is shipped with support for handling file uploading using middleware that takes the user requests, parses the contents for the files, and provides the next handler with the files information. The following snippet shows a basic example of handling files uploading in Express using Multer. const multer = require('multer'); const storage = multer.diskStorage({ destination: (req, file, cb) => { // store files to "uploads/" directory cb(null, 'uploads/'); }, }); const upload = multer({ storage }); // initiate an upload handler that can accept multiple fields and multiple files const uploadHandler = upload.fields([ { name: 'galleryImages', maxCount: 10 }, { name: 'userFiles', maxCount: 2 }, ]); app.post('/upload', uploadHandler, (req, res) => { res.json(req.files); }); The sample above can be used if we just...

Object Storage Comparison of DigitalOcean, Linode, and UpCloud

In this post, I want to show a test result of object storage services provided by DigitalOcean, Linode, and UpCloud. The result shows the average durations required to download/upload files from/to their cloud infrastructures. But, before we go into the test detail, let us overview the user features provided by each cloud provider. DigitalOcean has the most advanced features compared to the rest. By utilizing its user dashboard, we can set a custom domain for the CDN of object storage, customize permission per object, and custom CORS headers. UpCloud has a more straightforward interface but with a unique concept for creating separate bucket domains (directories) in a single object storage subscription. Linode has the simplest features in its interface but it provides CLI tools for managing the buckets and stored objects. Now, let us go back to the main topic. For the test, I deploy an AWS EC2 server based in Tokyo as a tester node. Then, I set separate object storages on DigitalOcean, ...

Zoro vs Mihawk

An old fight and the first encounter with Mihawk. " Scars on the back are the swordman's shame "

Managing MongoDB Records Using NestJS and Mongoose

NestJS is a framework for developing Node.js-based applications. It provides an additional abstraction layer on top of Express or other HTTP handlers and gives developers a stable foundation to build applications with structured procedures. Meanwhile, Mongoose is a schema modeling helper based on Node.js for MongoDB. There are several main steps to be performed for allowing our program to handle MongoDB records. First, we need to add the dependencies which are @nestjs/mongoose , mongoose , and @types/mongoose . Then, we need to define the connection configuration on the application module decorator. import { MongooseModule } from '@nestjs/mongoose'; @Module({ imports: [ MongooseModule.forRoot('mongodb://localhost:27017/mydb'), ], controllers: [AppController], providers: [AppService], }) Next, we create the schema definition using helpers provided by NestJS and Mongoose. The following snippet is an example with a declaration of index setting and an o...

Backup and Restore PostgreSQL Database

PostgreSQL has been shipped with a lot of tools for ease of managing the database. Some actions that we typically perform in managing databases are backing up a database and restoring it to another database server. In PostgreSQL, we can utilize pg_dump to back up the data from a database and psql or pg_restore to restore the data into a new database. Generating A Backup File There are several available formats for the exported data: plain, directory, compressed, and custom. The plain format is using plain SQL syntax to export the data. To make the tables are exported separately, we can utilize directory format. The custom format is utilizing a built-in compression mechanism and results in an optimized binary formatted file. It is suitable for a large database. If we use a custom format, we can only restore it using pg_restore , but we have the ability to selectively choose desired tables to be restored. The following command is used to generate a plain formatted file with th...

Resize VirtualBox LVM Storage

VirtualBox is a free solution to host virtual machines on your computer. It provides configuration options for many components on our machine such as memory, storage, networking, etc. It also allows us to resize our machine storage after its operating system is installed. LVM is a volume manager in a Linux platform that helps us to allocate partitions in the system and configure the storage size that will be utilized for a specific volume group. There are some points to be noticed when we work with LVM on VirtualBox to resize our storage. These are some steps that need to be performed. 1. Stop your machine before resizing the storage. 2. Set new storage size using GUI by selecting " File > Virtual Media Manager > Properties " then find the desired virtual hard disk name that will be resized. OR , by running a CLI program located in " Program Files\Oracle\VirtualBox\VBoxManage.exe ".  cd "/c/Program Files/Oracle/VirtualBox" ./VBoxManage.exe list...

Best Action & Thriller Movies

Here are some latest action and thriller movies that I think very recommended to watch.  Nobody (2021) A guy who seems like a special agent veteran has to back to his old job that is getting rid of some bandits. In the beginning, we see an ordinary man who runs his boring routines with his family. Until a couple of robbers broke into his house to take some worth in it but accidentally stole his daughter's kitty bracelet. Yeah, just because of the cheap bracelet and getting trouble with drunk guys on the way back home, he has to face bigger trouble. This movie is written by one of John Wick's writers, maybe that's why the main guy seems to be so badass. Don't Breathe 1 (2016) & 2 (2021) A war veteran who is blind and lives alone is forced to be a horrific man. Some intruders who broke into his house, make everything change. In 2016, the intruders want his treasure but end up finding something creepy in the basement. In 2021, he has to fight some bad guys who want to ...

Redirect A Domain or Sub-domain To Another Domain in Amazon Route 53

Amazon Route 53 is used for registering domains and storing DNS records. Basically, it cannot handle HTTP requests redirection which is commonly handled by web or proxy servers.  So, we need to implement a kind of web server in front of our service to make redirection. There are two methods that we can try to achieve our need for redirection. First, we can utilize Nginx (or other tools) as a proxy to our service with an additional configuration block for redirection. Second, if our service is hosted on Amazon EC2, we can activate the load balancer service and add a listener for redirecting specific requests. Nginx as Proxy Server The configuration for redirection is generally as follows. server { listen 80; server_name old_domain.com; return 301 http://new_domain.com$request_uri; } EC2 Load Balancer This method utilizes the load balancer feature in Amazon EC2. In the load balancer listener setting, we need to add a rule to redirect a specific request based on ...