This snippet of code got me off guard. Having a somewhat decent experience with golang I could not explain what is it doing and it took me embarrassingly a lot of time to figure out a sound explanation for what was going on. After I figured out the answer, I think that my sleep deprivation just played tricks on me. Still let’s dig into this. Down below is the same snippet but with a slice of integers for simplicity: ...
Case of a leaking timer in go
It was only an accident that I read a post on ArangoDB site and found the same leak in one of our projects at work. So this is going to be quite short, but nevertheless I want for have it in form of a blogpost. Here is how leak looked like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package main import ( "fmt" "runtime" "time" ) func main() { messags := make(chan string) for { select { case msg := <- messages: // do smth with msg fmt.Println(msg) return msg case <-time.After(time.Second * 3): // do smth else fmt.Println("3 sec timer fired") case <-time.After(time.Second * 30): fmt.Println("30 sec timer fired") return } } } What is wrong with it? There is a lot: ...
VIM cheat sheet
These are my notes on “Mastering VIM” by Ruslan Osipov. Also contains my own knowledge. dd — delete line cc — delete line and go into INSERT mode Movements uppercase is for “words separated by whitespace” e or E — move to the End of the word w or W — move between Words b or B — move Back _ jump to the beginning of the line :N where N is a number line. Will move you to that line number gi places you in INSERT mode you were last time zz move current line the the center s delete single character and enter INSERT mode ...
SOLID Go
SOLID is a famous cargo-cult that is used to poke “bad” code during code review. Jokes aside there are some solid, pun intended, ideas within SOLID. This post is yet another attempt to dismantle this set of principles and understand them better. The SOLID stands for(pasted from wiki): S ingle-responsibility principle: “There should never be more than one reason for a class to change.“In other words, every class should have only one responsibility O pen–closed principle: “Software entities … should be open for extension, but closed for modification.” L iskov substitution principle: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.“See also design by contract. I nterface segregation principle: “Many client-specific interfaces are better than one general-purpose interface.” D ependency inversion principle: “Depend upon abstractions, [not] concretions.” S single responsibility Out of all these, only first one makes sense right away. If you are not a complete beginner, you won’t create a struct that does shipping calculation, checks weather condition, sends push notification and orders pizza. Don’t you? ...
Notes on Garbage Collection in Golang
Garbage Collection is a process of freeing memory that is allocated and contains some data that is not being used. Notes: by doing escape analysis GC mechanism decides what goes to heap and what stays on stack use -gcflags '-m' flag to get escape analysis info, e.g. go run -gcflags '-m' main.go another way to look into what GC is doing during runtime is to run program with GODEBUG=gctrace=1 GC runs consurrently with main program running main running program is called “mutator” Golang Garbage Collection uses “tricolor algorithm” otherwise known as tricolor mark and sweep algorithm. GC represents data in the heap as graph of elements/objects channels are also garbage collected when they become unreachable, even if channel is not closed Sources: ...
Simple case of profiler usage in golang
So I was solving Construct Binary Tree from Inorder and Postorder Traversal and I got myself more less working solution that got accepted. But it was not the fastest one. But then I though — how do I profile buildTree function and see what actually takes time in my program? Golang has an excellent set of tool available, just check an output of go tool. One of the items in a list you’ll see is pprof which is a Golang profiler. ...
Notes on System Design
These are hectic notes on a book “System design interview” by Alex Xu. chapter 1 read-through cache strategy — check if we have a response cached, if we do return it, if not then fetch data from a database, cache it and return result GeoDNS is a patch for BIND DNS server software that allows for geographical split horizon A few words about caching strategy. Your caching strategy depends on your data access patterns. Is your system write heavy, read heavy, results are always should be unique ? ...
Golang concurrency tricks and patterns
Concurrency is the composition of independently executing computations. Goroutines are not threads, but it is not wrong to think about goroutines as threads. In fact in runtime goroutines are multiplexed onto threads that are created as needed in order to make sure no goroutine ever blocks. Generator Multiplexer nil channel trick Worker pool Confinement Error handling Goroutines Generator. Function that returns never closed channel. Basically generate func returns unbuffered channel that is never closed and hence returns ever increasing counter. ...
Notes on 'Go in action' book
Slice takes 24 bit of memory — 8 bit for pointer, 8 for len and 8 for capacity. Passing a slice between functions is okay, since it is copied BUT in the copied version we still have the correct pointer. Reference types in Go: string slice map channel interface function The decision to use value or pointer receiver should not be based on whether method is mutating given value or not. Decision should be based on a nature of a given type. e.g. time is always a copy, you can’t really change time. A File can be changed so it should be passed as a reference. See how it is implemented in core library. ...
Notes on Building Microservices by Sam Newman
Chapter 3 notes. How to model services. What makes a good service: loose coupling high cohesion Loose coupling means a change to one service should not require a change in another service. Tight coupling might means “chatty” communication, or wrong integration style was chosen that binds services tightly. High cohesion — we want related behavior to be in the same service, and unrelated behavior to live in another service. Bounded context helps with those two aspects of a good service. Todo: to learn more about bounded context. ...