Go Runtime Implementations: Defer, Panic and Recover

Code Path: src/runtime/panic.go Ref: Defer, Panic, and Recover Defer Two methods of defer Copied from the comment: The older way involves creating a defer record at the time that a defer statement is executing and adding it to a defer chain. This chain is inspected by the deferreturn call at all function exits in order to run the appropriate defer calls. A cheaper way (which we call open-coded defers) is used for functions in which no defer statements occur in loops....

June 25, 2023

Set Up OpenLDAP and phpldapadmin on Ubuntu 22.04

Install LDAP Before install ldap, set-up a valid FQDN for your hostname: Edit /etc/hostname, for example void.kassiansun.com Restart the host If you’ve installed ldap before, purge them all: sudo apt-get remove --purge slapd ldap-utils -y Now we can install the ldap packages: sudo apt-get install slapd ldap-utils -y During the installation, it will prompt to set the default password. Test that you now have a valid LDAP tree: # Output: # dn: # namingContexts: dc=kassiansun,dc=com ldapsearch -H ldap://localhost -x -LLL -s base -b "" namingContexts Clean-Up Old apache2 and php installation sudo apt-get remove --purge apache2 phpldapadmin php* If you’re not using apache or php on your machine, clean them all so we can get started from the scratch....

June 16, 2023

Redis Internal: List Types

The Generic Set Implementation Code Path: src/t_set.c It’s a wrapper around the actual set type. The set can be one of the following types: An intset. Even if the member is a string, Redis will try to convert it to an integer with string2ll. A listpack. Used for small sets. A dict. Used for large sets. Listpack Implementation The same implementation of hash type. Dict Implementation The same implementation of hash type....

January 18, 2023

Redis Internal: List Types

The Generic List Implementation Code Path: src/t_list.c It’s a Redis command wrapper of listpack and quicklist. Listpack Implementation The same implementation of hash type. Quicklist Implementation Code Path: src/quicklist.c If the size of the list exceeds a certain number, Redis will try to convert a listpack to quicklist. A quicklist is a double-linked list of listpack: If the data is too large, it will store the data as a “plain” node, without encoding it as listpack....

January 3, 2023

Redis Internal: Hash Types

The Generic Hash Implementation Code Path: src/t_hash.c The generic hash type is a wrapper around listpack and dict, it will switch between two different implementation and call the routines accordingly. Listpack Implementation Code Path: src/listpack.c Listpack is a densed storage of a series of keys and values, it supports storing both integers and strings. If the hash size is lower than hash_max_listpack_value, it will be saved as a listpack. If the hash size is higher than hash_max_listpack_value, it will create a dict and copy the data from listpack to the dict....

November 26, 2022

Go Runtime Implementations: Map

Code Path: src/runtime/map.go makemap Each hmap has a hash0 as seed hash. When the compiler generates the typing information for a map, the hasher function will be calculated with genhash function (code path: src/cmd/compile/internal/reflectdata/alg.go). If the map is not escaped, the hmap will be allocated by the compiler on the stack. Then makemap will allocate an array of bucket, the type of bucket is generated at compiling stage by MapBucketType (code path: src/cmd/compile/internal/reflectdata/reflect....

November 23, 2022

Redis Architecture

Start-Up Procedures spt_init: This procedure initializes the process’s name and deep copy command-line arguments & environments. tzset. zmalloc_set_oom_handler init_genrand64 with timestamp and pid number. Code Path: mt19937-64.c crc64_init. Code Path: crc64.c umask dictSetHashFunctionSeed with a random 16-byte seed. Code Path: dict.c initServerConfig ACLInit. Code Path: acl.c moduleInitModulesSystem. Code Path: module.c connTypeInitialize. Code Path: connection.c initSentinelConfig and initSentinel if the server was started in sentinel mode. Code Path: sentinel.c Run redis_check_aof_main or redis_check_rdb_main and exit....

November 10, 2022

Go Runtime Implementations: Timer Scheduling

Code Path: src/runtime/time.go time.startTimer (implemented as addtimer) Each p has a timers list. When a user is adding a new timer, it will first try to clean up the timers list, then adds the new timer. Each timers list is a heap sorted by timer.when, and will be updated during add/delete timers. cleantimers cleantimers only checks the head of the timers list. Check if it’s timerDeleted, delete it from the timers list, and update p’s timer0When....

November 7, 2022

Go Runtime Implementations: Network Polling

Code Path: src/runtime/netpoll.go Network polling is platform-dependent, the following is of BSD family operating systems (based on kqueue). netpollinit Initialize a kqueue descriptor, and set CLOSEXEC flag on it. Initialize a non-blocking pipe, and use the reader/writer for netpollBreak. netpollBreak will try to write to the pipe and interrupt the kevent. netpoll netpoll is used to check the connections and return a list of g ready to run. It can block for delay ns at most....

November 5, 2022

Go Runtime Implementations: Select

Code Path: src/runtime/select.go IR stage IR will walk a select statement and generate the code accordingly: If there’s no case, replace the select statement with runtime.block, the current goroutine will be blocked forever. If there’s only one case, extract the send or receive operation from the case statement. Otherwise, convert case values to addresses. If there’s only one case with one default, replace it with non-block calls selectnbsend or selectnbrecv....

November 4, 2022