Cloud-oriented Life

Cloud Native Technology Improves Lives

sync.Once

Once is an object that will perform exactly one action.

.Do calls the function f if and only if .Do is being called for the first time for this instance of Once. In other words, given var once Once if once.Do(f) is called multiple times, only the first call will invoke f,
even if f has a different value in each invocation.

A new instance of Once is required for each function to execute.

Read more »

sync.WaitGroup

WaitGroup is used to wait for all the goroutines launched here to finish since the main function actually terminates before the goroutine gets a chance to execute by defatult.

WaitGroups essentially allow us to tackle this problem by blocking until any goroutines within that WaitGroup have successfully executed.

We first call .Add(n) on our WaitGroup to set the number n of goroutines we want to wait for, and subsequently, we call .Done() within any goroutine to signal the end of its’ execution.

Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.

Read more »

log

Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined ‘standard’ Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.

Read more »

sync.Mutex & sync.RWMutex

Mutex and RWMutex are not associated with goroutines, but RWMutex is obviously more suitable for scenarios with more reads and less writes. For read performance only, RWMutex is higher than Mutex, because multiple reads of rwmutex can coexist.

Read more »

sync.Pool

A Pool is a set of temporary objects that may be individually saved and retrieved.

When there is an expensive object you have to create it frequently, it can be very beneficial to use sync.Pool.

Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated.

A Pool is safe for use by multiple goroutines simultaneously.

Pool’s purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector. That is, it makes it easy to build efficient, thread-safe free lists. However, it is not suitable for all free lists.

Read more »

premailer-rails

premailer-rails is a drop in solution for styling HTML emails with CSS without having to do the hard work yourself.

Styling emails is not just a matter of linking to a stylesheet. Most clients, especially web clients, ignore linked stylesheets or <style> tags in the HTML. The workaround is to write all the CSS rules in the style attribute of each tag inside your email. This is a rather tedious and hard to maintain approach.

Read more »

sync/atomic

Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.

These functions require great care to be used correctly. Except for special, low-level applications, synchronization is better done with channels or the facilities of the sync package. Share memory by communicating; don’t communicate by sharing memory.

Read more »

Singleton Pattern in Dart

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one “single” instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton.

You should be aware of Singletons in multi-threaded applications. If they hold some kind of mutable data, it could lead to unexpected results, so the synchronization mechanism should be considered.

Since we are talking about the Dart programming language in this series, you should know that Dart is a single-threaded programming language and its code runs in a little isolated space on the machine, called isolate. Hence, you should not worry about the thread-safety when implementing Singletons in Dart as long as you do not create a new separate isolate from the code by yourself.

Read more »
0%