Posts

Showing posts from April, 2020

Range Over Ticker In Go With Immediate First Tick

Image
The Go standard library has a really cool type – Ticker . Tickers are used when you want to do something at a regular interval, similar to JavaScript’s setInterval . Here’s an example: package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Second) go func() { for range ticker.C { fmt.Println("Tick") } }() time.Sleep(time.Second * 4) ticker.Stop() fmt.Println("Ticker stopped") } As per the docs , a ticker is a struct that holds a receive-only channel of time.Time objects. type Ticker struct { C <-chan Time // The channel on which the ticks are delivered. } In the example at the beginning of the article, you will notice by running the program that the first tick sent over the channel happens after the first interval of time has elapsed. As such, if you are trying to bu...

Connecting To RabbitMQ In Golang

Image
RabbitMQ is a message broker that’s great for pub-sub systems in a micro-services architecture. At my current day job, we use RabbitMQ in our data ingest pipeline. All the services are written in Go, and they all push data through hundreds of RabbitMQ queues. Let’s take a look at how to efficiently publish and subscribe to Rabbit, while GO ing as fast as possible. Brief Overview On Rabbit The two main entities to be aware of with Rabbit are routing keys and queues . A service publishes a message (JSON in our case) to a routing key. RabbitMQ then copies that message into each queue that is subscribed to that routing key. The subscribing service (the consumer) can pull messages off of a queue one at a time. It is worthwhile to note that a queue can also receive messages from multiple routing keys, but we won’t be diving into that here. Connecting With Go First things first, there is no reason to reinvent the wheel. We will use the amqp package provided ...

(Very) Basic Intro To White-Box Cryptography

Image
White-box cryptography combines methods of encryption and obfuscation to embed secret keys within application code. The goal is to combine code and keys in such a way that the two are indistinguishable to an attacker, and the new “white-box” program can be safely run in an insecure environment. What Does “White-Box” Mean? In penetration testing, white-box testing is where the testers (or attackers) have access to the source code and internal workings of the system. Passing a "white-box" test is harder and requires a higher standard of security than a "black-box" test where the attacker only gets a look at the system from the outside. #whitebox #hacking Click To Tweet White-box cryptography is appropriately named because attackers have access to the compiled code where the keys exist. The difficult problem that it aims to solve is how to keep those keys safe while using them in execution. Kerckhoffs’s Principle Kerckhoffs’s principle...

Using ‘Go Generate’ To Deploy Multi-Process Apps

Image
PSP31475-00, 7HA.02 Gas Turbine, Rotor Instalation, Greenville, SC, USA, DI-3800×5700 In microservice architectures, it is fairly common to have a project that includes different worker types. A Makefile can be used to manage the creation of multiple programs, but the Go toolchain has a great tool that can be used as well, go generate . Here are some examples of how we take advantage of ‘go generate’ at Nuvi : API/Worker – We have an API that allows clients to start/stop long-running jobs, and a worker which accesses the same database to run the jobs. NLP Classifier – We have different processes that share a majority of the same code, but have different quirks depending on if they are classifying sentiment, vulgarity, or subjectivity. In other words, we have one git repository, but from that code, we want to generate ‘ n ‘ number of executables. Repository Structure Our normal single-process repositories have the following structure: main.go foo....

Use Anonymous Structs For JSON Marshalling in Go

Image
Go is a language built for the web. The Go standard library comes with everything we need to stand up a production web server. Today we are going to explore marshaling JSON using anonymous structs. Anonymous structs can help keep API handlers clean and simple. What Is A Struct? Go’s  structs  are typed collections of fields. They’re useful for grouping data together to form records. https://gobyexample.com/structs Structs don’t have behavior, just data. For web developers, it can be helpful (though an oversimplification) to think of most structs as just JSON data. We can declare a struct: type car struct { make string model string mileage int } And we can create and instance of it: newCar := car{ make: "Ford", model: "taurus", mileage: 200000, } What Makes a Struct Anonymous? An anonymous struct is declared in the same statement that initializes an instance of it: newCar := struct { ...

Quantum Programming 101: Superdense Coding Tutorial

Image
By Macauley Coggins What is Superdense coding ? Superdense coding is a quantum communications protocol that allows a user to send 2 classical bits by sending only 1 qubit. The Protocol Step 1: Preparing the Bell Pair First a bell pair consisting of 2 qubits is prepared. Where q0 is the senders qubit and q1 is the receivers qubit. To do this q0 is put in to a superposition of states using a hadamard gate. Then a CNOT operation is performed with q0 being the control and q1 being the target. Step 2: Encode The Information On To Q0 Next the sender has to encode the information they want to send on to q0 by applying certain operations to it. If they want to send 00 then they perform no operation. If they want to send 01 then they perform a Pauli-X operation where q1s state is flipped. If they want to send 10 then they apply a Pauli-Z gate. If they want to send 11 then apply a Pauli-Z gate followed by a Pauli-X gate Step 3: Receiver Decodes the Information Next q0 is sent a...

Announcing Go-TinyTime, Go-TinyDate’s Sister Package

Image
time.Time is the perfect choice for most cases, it even comes in the standard library! The problem is that the time.Time{} struct uses more than 24 bytes of memory under most conditions. Go-TinyTime solves this problem by restricting the available dates to the range between 1970 – 2106, and only supporting UTC timezones. This brings data usage down to just 4 bytes of memory. Go-TinyDate is its sister package that allows for a much larger date range but doesn’t get more than day precision. Between time.Time , go-tinydate , and go-tinytime all of our time problems can be solved using the same API. Don’t forget to the GitHub: https://github.com/lane-c-wagner/go-tinytime How Does It Work? A normal time.Time object takes at least 16 bytes of memory: type Time struct { wall uint64 // 8 bytes ext int64 // b bytes loc *Location // 8 bytes if not nil, plus location memory } If there is a location set (which there usually is), then this can be higher, us...