The RedLock algorithm for distributed locks based on Redis and its implementation in RedLock-Hyperf

Introduction Recently, the project required encapsulating a Redis distributed lock under the Hyperf framework, so I encapsulated the RedLock-Hyperf SDK based on the RedLock algorithm. Currently, in addition to supporting simple object calls, it also supports implementation through AOP annotations within the Hyperf framework. Implementing a distributed lock with Redis is probably not a difficult task for you. Most people use the setnx + expire + del commands to implement a simple distributed lock, but is such a mutex really secure? In this article, we will explore together the common implementations of Redis distributed locks and how the officially recommended RedLcok algorithm by Redis ensures the security of the lock. As a side note, although there are distributed locks implemented with Zookeeper and etcd besides Redis, these latter two options bring certain operational costs for a simple action of distributed...

March 18, 2021 · 4 min · 1586 words · ZhangYong

Why does Hyperf disable short names for Swoole coroutines

In the server requirements section of the official Hyperf documentation, it is mentioned: Swoole PHP extension >= 4.5, with Short Name disabled Furthermore, in the documentation’s FAQ section, one can find a tag titledSwoole Short Name Not Disabled. I would like to inquire why it is imperative for Hyperf to disable Swoole’s coroutine short names. First, let’s examine what Swoole’s coroutine short names are All class names prefixed with Swoole\Coroutine are aliased to Co. Additionally, there are the following mappings: the go() function for creating coroutines, the chan() function for channel operations, and the defer() function for deferred execution. From the above explanation, we understand that Hyperf primarily wishes to prevent the use of these specific functions, but why is their use discouraged? Considering that the go() function is often used in code to resolve blocking issues, does this mean...

January 12, 2021 · 2 min · 525 words · ZhangYong

Hyperf/Crontab Component Source Code Analysis

Pre-Read:Hyperf/Crontab Documents Pre-Read:Hyperf/Process Custom Process Documents Pre-Read:Hyperf Event System Introduction Previously, while working on a project, I used the Hyperf/Crontab component for second-level data cleansing. Recently, as I’ve been working on splitting scheduled tasks, I decided to go through the component source code to deepen my understanding and, at the same time, contemplate how to build a task scheduling feature based on Hyperf/Crontab. At its core, Crontab is a custom process that starts with the Server, so we will introduce it in...

June 2, 2020 · 3 min · 1199 words · ZhangYong

Composer Basic Usage

composer.json This file contains the project’s dependencies and some other metadata { "require": { "monolog/monolog": "1.0.*" } } Version Explanation: ~ indicates that only the last segment of the version number can change (if it’s ~x.y then the last segment is y, if it’s ~x.y.z then the last segment is z) ~1.2.3 means 1.2.3 <= version number < 1.3.0 ~1.2 means 1.2 <= version number < 2.0 ^ indicates that all segments except the major version number can change ^1.2.3 means 1.2.3 <= version number < 2.0.0 Special case for versions starting with 0: ^0.3.0 equals 0.3.0 <= version number < 0.4.0 Note: Not < 1.0.0 Because: according to semantic versioning rules, a major version number starting with 0 indicates that it is an unstable version, and minor version numbers are allowed to be incompatible in an unstable state. So,...

December 19, 2019 · 2 min · 587 words · ZhangYong

Documenting a low-effort operation of debugging an Nginx vHost configuration.

Recently, I’ve been studying the specifics of the Fast-CGI protocol and used TCPDUMP to capture the TCP packets sent from Nginx to PHP-FPM. So, I set up a simple environment on my MAC to run a small demo. I already had Nginx and PHP, just had to configure an Nginx vhost and it should be done, but who knew I’d spend half a day messing with the vhost. Tough times. Since all my previous Nginx configurations were done on virtual machines provided by the company, it was just a matter of copying a conf file from a previous project, changing the root path and server_name, and then reloading. Having read the Nginx manual, I understood the roles of the http, server, and location modules, as well as some configurations, so I decided to write one myself. First, I wrote a...

November 13, 2019 · 2 min · 688 words · ZhangYong