summaryrefslogtreecommitdiff
path: root/docs/content/implementation/concepts.tex
blob: 6ec2281a4e474364bcb21a809e427aa25682a8a7 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
\section{Concepts}

This chapter describes high level concepts which are used in the implementation of the components. The short explanations aim to support the understanding of the reader to faster and better understand the implementation of the components.

\subsection{Consumers and Producers}

The two terms consumer and producer are used through the entire documentation. They describe the role of a component in an interaction with another component. The consumer is the component askig or requesting a producer to gather information or trigger some action. The Producer on the other hand is the component who receives information or call for action of a consumer. 

\subsection{Long-Polling}
\label{sec-concepts-long-poll}

Long-Polling is the concept of not closing a connection until a a response can be delivered or a given duration exceeds. This allows a consumer to ask for information which it assumes will arrive in the future at the producer. The producer therefore will not close the request of the consumer but instead keep the connection open and respond with the response, once it is available. The consumer and the producer can both close the connection after a certain amount of time, which is called the timeout. This can also happen if the wanted result of the producer cannot be returned to the consumer.

\subsection{Publish-Subscribe Pattern}
\label{sec-concepts-pub-sub}

The concept of publishers and subscribers is used heavily in the implementation. It allows decoupling different steps of the process and allows different steps to be handled and executed in their own processes. Publishers can also be called notifiers or similar, while the subscriber can also be called listener or similar.

The communication of publishers and subscribers happends through channels. A publisher will publish to a certain channel when a defined state is reached. The subscriber who is subscribed or listens to this channel will capture the message sent through the channel by the publisher and start processing it.

The publish-subscribe scheme enables loose coupling and therefore helps to improve the performance of individual processes, because they cannot be hindered by others.

\subsection{Go Language}

Go comes with handy features to implement the concepts like pub/sub \autoref{sec-concepts-pub-sub} or long polling \autoref{sec-concepts-long-poll}.

\subsubsection{Contexts}

Go standard library contians a package called \textit{context}. You will stumble over this package all the time, even when using third party libraries or when writing your own code. The \textit{context} package allows to control the lifetime and cancellation activities for function and allows concurrent running threads to be executed within the same context. For example if you have a function which can sort a list concurrently and will fail if any thread has a failure, supplying each concurrent execution of the function the same context allows to leave the function early if the context is left by propagating the done signal through the \textit{Done} channel which is part of each context. A context also defines the \textit{cancellation function}. This function shall be called, for each context when it becomes obsolete. The cancellation function will execute cleanup activities related to the specific context. It is a best practice to defer the cancellation function right after the creation of the context \cite{golang-contexts-and-structs}. 

\subsubsection{Go Routines}

In concurrent programs it is a challenge to keep up with the complexity which they add to the code. Also one has to take care of interprocess communication and if memory is accessed in shared manner by the program, the access to the data stored should be mutual exclusive. Go therefore comes with the concept of Goroutines. They are designed to be very cheap, lightweight threads. They share the same address space and are just executed besides each other as simple functions. Also Go encourages the use of channels to communicate between different goroutines. The use of channels makes locking memory for concurrent access obsolete and therefore removes possible concurrency problems by making them impossible by design \cite{golang-share-by-communicating}. 

\subsubsection{Memory safety}

Even when Go is a low level language which compiles to native bytecode directly, it implements a garbage collector and memory management which takes care of allocating and releasing memory as needed. This reduces the risk of memory leaks to bugs in the memory management and the garbage collector.