Allons-y !

ยท

4 min read

I fancy myself as a bit of a polyglot when it comes to programming languages. I have used a few over the years. More recently, I have been playing with Go and I like what I see. The design decisions resonate with me - smaller surface area, structs with functions and interfaces, concurrency, cross-compilation, robust standard library and many such. Pretty soon, it will have generics too. While the benefits of generics could be a debatable topic, I think it certainly helps in one area - standard, robust implementation of most commonly used collections. Look the vast (and more often than not, sprawling ๐Ÿ˜ƒ) Java collections API and you'll get my point.

When it comes to practising, I don't get a chance to use it in my day job. There is only so much you can do by reading books, blogs etc. and playing with small code snippets. I was thinking about how to get more familiar with the language and had an epiphany ๐Ÿ˜‡ - why not walk through a production-grade Go codebase to understand the nitty-gritty of the practical usage and learn about common idioms.

That's where NATS.io comes in. In its own words:

#NATSio is a secure connective technology for modern distributed systems at the edge and in the cloud. #CNCF

I had been using it more and more recently and find it a great little, very fast message broker. It's written in Go and as such, a perfect candidate for broadening my Go and distributed systems knowledge.

Plan

Let's get the lay of the land ๐Ÿง.

~/work/ws/github/nats-server on ๎‚  project via ๐Ÿน v1.17.1 took 35s
โฏ scc . --by-file --ci --exclude-dir vendor -i go --not-match test.go --sort name --no-cocomo --no-complexity
-------------------------------------------------------------------------------
Language                     Files       Lines     Blanks    Comments      Code
-------------------------------------------------------------------------------
Go                              69       78689       7860       10873     59956
-------------------------------------------------------------------------------
conf/fuzz.go                                24          3          13         8
conf/lex.go                               1214         94         159       961
conf/parse.go                              437         49          61       327
internal/ldap/dn.go                        305         14          15       276
logger/log.go                              321         32          31       258
logger/syslog.go                           132         19          28        85
logger/syslog_windows.go                   112         18          23        71
main.go                                    122         12          19        91
server/accounts.go                        4220        373         505      3342
server/auth.go                            1135         80         169       886
server/ciphersuites.go                     103          7          16        80
server/client.go                          5233        541         960      3732
server/const.go                            211         54          90        67
server/consumer.go                        3147        330         405      2412
server/dirstore.go                         706         50          54       602
server/disk_avail.go                        38          4          16        18
server/disk_avail_openbsd.go                37          4          15        18
server/disk_avail_windows.go                21          3          14         4
server/errors.go                           326         70         100       156
server/errors_gen.go                       240         37           2       201
server/events.go                          2189        185         288      1716
server/filestore.go                       5498        631         680      4187
server/fuzz.go                              47          7          13        27
server/gateway.go                         3169        239         701      2229
server/jetstream.go                       2172        231         220      1721
server/jetstream_api.go                   3585        470         356      2759
server/jetstream_cluster.go               5033        537         450      4046
server/jetstream_errors.go                 102         18           5        79
~jetstream_errors_generated.go            1706        324         226      1156
server/jetstream_events.go                 257         38          48       171
server/jwt.go                              224         11          26       187
server/leafnode.go                        2555        244         492      1819
server/log.go                              244         36          45       163
server/memstore.go                         855        101          87       667
server/monitor.go                         2664        219         254      2191
server/monitor_sort_opts.go                150         33          32        85
server/mqtt.go                            4013        337         743      2933
server/nkey.go                              46          6          18        22
server/ocsp.go                             805         97          85       623
server/opts.go                            4662        263         394      4005
server/parser.go                          1270         30          63      1177
server/pse/pse_darwin.go                    86         11          27        48
server/pse/pse_freebsd.go                   85          8          61        16
~rver/pse/pse_freebsd_amd64.go              91         13          42        36
server/pse/pse_linux.go                    127         20          25        82
server/pse/pse_openbsd.go                   36          3          15        18
server/pse/pse_rumprun.go                   25          4          14         7
server/pse/pse_solaris.go                   23          3          13         7
server/pse/pse_windows.go                  280         35          52       193
server/raft.go                            3303        350         298      2655
server/reload.go                          1822        192         305      1325
server/ring.go                              77          9          23        45
server/route.go                           2031        213         380      1438
server/sendq.go                            128         14          14       100
server/server.go                          3500        385         610      2505
server/service.go                           28          4          16         8
server/service_windows.go                  130         17          31        82
server/signal.go                           177         14          28       135
server/signal_windows.go                   105         14          16        75
server/store.go                            441         43          56       342
server/stream.go                          3566        379         378      2809
server/sublist.go                         1534        136         206      1192
server/sysmem/mem_bsd.go                    20          3          13         4
server/sysmem/mem_darwin.go                 20          3          13         4
server/sysmem/mem_linux.go                  27          4          13        10
server/sysmem/mem_windows.go                46          5          14        27
server/sysmem/sysctl.go                     31          4          14        13
server/util.go                             223         23          60       140
server/websocket.go                       1397        100         215      1082
-------------------------------------------------------------------------------
Total                           69       78689       7860       10873     59956
-------------------------------------------------------------------------------
Processed 2195116 bytes, 2.195 megabytes (SI)
-------------------------------------------------------------------------------

So, 69 source files and 59956 lines of code, that's it ๐Ÿ˜ƒ!

Well, it won't be optimal going over each and every line. I'll divide it into major areas and will go over relevant source code in subsequent posts.

That's enough of an introduction ๐Ÿ˜ƒ. I'm pretty excited about this. Stay tuned for more!

ย