rediscounter

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 4 Imported by: 0

README

Go redis counter package

Build Status Godoc Ref

Package rediscounter provides a simple counter that stores its value in a redis server.

The package documentation is available as godoc

The project includes basic implementations of the package:

  • a console application in folder console/ - readme.
  • a web application in folder webcounter/- readme.

Vagrant environment

The repository includes a Vagrant project that builds 3 VMs with redis server, vault server and Golang installed.

In case you are not familiar with Vagrant, a getting started guide can be found here.

Using the project

The redis VM will have redis server running on port 6379 which will be mapped to the host as well. The redis server will have password authentication configured as well.

The vault VM will have Vault installed and running in dev mode on port 8200 which will be mapped to the host as well.

The client VM will have Golang installed so the application can be built and run. The redis server IP and password will be set in the environment variables $REDIS_ADDR and $REDIS_PASS. The Vault server address and access token will be set as well in $VAULT_ADDR and $VAULT_TOKEN respectively.

Example:

vagrant up # build the VMs
vagrant ssh client # login to the client VM

# commands below are executed on the client VM

# download the counter app source code and all dependencies
go get github.com/slavrd/go-redis-counter/...

# build the console counter app
go build $(go env GOPATH)/src/github.com/slavrd/go-redis-counter/console

# run the app passing the redis password directly
./console -a $REDIS_ADDR -pass $REDIS_PASS

# run the app retrieving the redis password from Vault
./console -a $REDIS_ADDR -v-use

exit # exit from the client VM to the host

vagrant destroy # destroy the vagrant VMs

TODO

  • Console implementation
  • console: add integration with Vault KV secrets engine. Application should be able to retrieve redis password from Vault.
  • Web implementation
  • Make Vagrant mount the project in the $GOPATH of the client VM.
  • Make Vagrant use a "golang" box instead of provisioning the client VM each time.
  • Make go tests warn and stop if redis key is already set. Make it possible to force running the tests with a command flag.
  • webcounter: add a /health check method that tests the redis server connection.
  • webcounter: add a /metrics method which will report how many times each path has been requested.
  • webcounter: add a /crash method which will stop the webserver
  • webcounter: add a /reset method which will reset the counter
  • webcounter: add a /decr method which will decrease the counter by 1. Should be guarded from going below 0
  • webcounter: start even if redis connection is unavailable.
  • webcounter: implement mutual exclusion lock on redis level, so multiple app instances can be used.
  • Box with webcounter running as a service
  • Update box with webcounter service running as a non privileged user
  • webcounter: add integration with Vault KV secrets engine.
  • webcounter: update UI to call the methods with buttons.
  • packer project that creates AWS AMI with webcounter app installed as a service
  • terraform project that deploys webcounter app and its redis server in AWS
  • add network module to the terraform project
  • terraform create AWS subents in different AZs
  • replace publicly accessible webcounter EC2 instances with load balancer
  • terraform remove redis password from EC2 instances user data
  • Vagrant counter box - a Packer project that builds a Vagrant Virtualbox box with webcounter installed as a service.
  • AWS counter AMI - a Packer project that builds an AWS AMI with webcounter installed as a service.
  • AWS redis AMI - a Packer project that build an AWS AMI with Redis server installed.
  • AWS Terraform deployment - a Terraform configuration to deploy the webcounter and redis AMIs in AWS.

Documentation

Overview

Package rediscounter provides a counter that stores its value in a redis server

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisCounter

type RedisCounter struct {
	// contains filtered or unexported fields
}

RedisCounter represents a counter that stores its value in redis

func NewCounter

func NewCounter(raddr, rpass, rkey string, rdb int) (*RedisCounter, error)

NewCounter creates a RedisCounter with the provided connection details.

"raddr" format should be host:port

"rpass" can be set to "" if no password authentication is required.

func (*RedisCounter) DecrBy

func (rc *RedisCounter) DecrBy(a int64) (int64, error)

DecrBy decreases the counter's value by "a" amount and reports the resulting value. The counter's value cannot go below 0.

func (*RedisCounter) Get

func (rc *RedisCounter) Get() (int64, error)

Get returns the current value of the counter

func (*RedisCounter) IncrBy

func (rc *RedisCounter) IncrBy(a int64) (int64, error)

IncrBy increases the counter's value by "a" amount and reports the resulting value

func (*RedisCounter) RedisHealth

func (rc *RedisCounter) RedisHealth() error

RedisHealth checks the redis server connection using PING

func (*RedisCounter) Reset

func (rc *RedisCounter) Reset() (int64, error)

Reset sets the counter value to 0. It will allways return 0 so use error to determine if successful.

Directories

Path Synopsis
Command console starts a console client that implements the rediscounter package.
Command console starts a console client that implements the rediscounter package.
Command webcounter starts a web server that implements the rediscounter package.
Command webcounter starts a web server that implements the rediscounter package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL