Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 6, 2021 11:38
Show Gist options
  • Save percybolmer/8eb74a28c443efefaae0a38e508291aa to your computer and use it in GitHub Desktop.
Save percybolmer/8eb74a28c443efefaae0a38e508291aa to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/json"
"math/rand"
"time"
"github.com/go-redis/redis/v8"
)
// User is a struct representing newly registered users
type User struct {
Username string
Email string
}
// MarshalBinary encodes the struct into a binary blob
// Here I cheat and use regular json :)
func (u *User) MarshalBinary() ([]byte, error) {
return json.Marshal(u)
}
// UnmarshalBinary decodes the struct into a User
func (u *User) UnmarshalBinary(data []byte) error {
if err := json.Unmarshal(data, &u); err != nil {
return err
}
return nil
}
// Names Some Non-Random name lists used to generate Random Users
var Names []string = []string{"Jasper", "Johan", "Edward", "Niel", "Percy", "Adam", "Grape", "Sam", "Redis", "Jennifer", "Jessica", "Angelica", "Amber", "Watch"}
// SirNames Some Non-Random name lists used to generate Random Users
var SirNames []string = []string{"Ericsson", "Redisson", "Edisson", "Tesla", "Bolmer", "Andersson", "Sword", "Fish", "Coder"}
// EmailProviders Some Non-Random email lists used to generate Random Users
var EmailProviders []string = []string{"Hotmail.com", "Gmail.com", "Awesomeness.com", "Redis.com"}
func main() {
// Create a new Redis Client
redisClient := redis.NewClient(&redis.Options{
Addr: "redis:6379", // We connect to host redis, thats what the hostname of the redis service is set to in the docker-compose
Password: "superSecret", // The password IF set in the redis Config file
DB: 0,
})
// Ping the Redis server and check if any errors occured
err := redisClient.Ping(context.Background()).Err()
if err != nil {
// Sleep for 3 seconds and wait for Redis to initialize
time.Sleep(3 * time.Second)
err := redisClient.Ping(context.Background()).Err()
if err != nil {
panic(err)
}
}
// Generate a new background context that we will use
ctx := context.Background()
// Loop and randomly generate users on a random timer
for {
// Publish a generated user to the new_users channel
err := redisClient.Publish(ctx, "new_users", GenerateRandomUser()).Err()
if err != nil {
panic(err)
}
// Sleep random time
rand.Seed(time.Now().UnixNano())
n := rand.Intn(4)
time.Sleep(time.Duration(n) * time.Second)
}
}
// GenerateRandomUser creates a random user, dont care too much about this.
func GenerateRandomUser() *User {
rand.Seed(time.Now().UnixNano())
nameMax := len(Names)
sirNameMax := len(SirNames)
emailProviderMax := len(EmailProviders)
nameIndex := rand.Intn(nameMax-1) + 1
sirNameIndex := rand.Intn(sirNameMax-1) + 1
emailIndex := rand.Intn(emailProviderMax-1) + 1
return &User{
Username: Names[nameIndex] + " " + SirNames[sirNameIndex],
Email: Names[nameIndex] + SirNames[sirNameIndex] + "@" + EmailProviders[emailIndex],
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment