Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
add support for multiple replicas in statefulset of the irc-reader
Browse files Browse the repository at this point in the history
  • Loading branch information
broadeditz committed Oct 5, 2023
1 parent d3da530 commit e47262c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/irc-reader/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var OnChange func()

type Config struct {
LogLevel string
Replicas int

RateLimit struct {
Join int64
Expand Down
1 change: 0 additions & 1 deletion internal/irc-reader/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (c *Controller) watchChanges(nc *nats.Conn) {
if !bitwise.Has(channel.Flags, bitwise.JOIN_IRC) {
return
}
println("joining: " + channel.Username)
c.joinChannel(channel)
case database.Update:
// TODO: implement
Expand Down
19 changes: 19 additions & 0 deletions internal/irc-reader/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package irc_reader

import (
"context"
"os"
"strconv"
"strings"

"github.com/nats-io/nats.go"
"github.com/redis/go-redis/v9"
Expand All @@ -17,6 +20,8 @@ type Controller struct {
jetStream nats.JetStreamContext
twitch *manager.IRCManager

shardID int

// limit amount of workers for joining channels
joinSem chan struct{}
}
Expand All @@ -29,6 +34,9 @@ func New(cfg *config.Config) *Controller {
}

func (c *Controller) Init() error {
if c.cfg.Replicas > 1 {
c.shardID = getShardID()
}
nc, err := nats.Connect(c.cfg.Nats.URL)
if err != nil {
return err
Expand Down Expand Up @@ -84,6 +92,17 @@ func (c *Controller) Init() error {
return nil
}

func getShardID() int {
env := os.Getenv("HOSTNAME")
split := strings.Split(env, "-")
if len(split) == 0 {
return 0
}
id := split[len(split)-1]
result, _ := strconv.Atoi(id)
return result
}

func (c *Controller) Shutdown() {
wg := c.twitch.Shutdown()
wg.Wait()
Expand Down
18 changes: 16 additions & 2 deletions internal/irc-reader/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ func (c *Controller) joinChannels(channels []types.Channel) {
}

func (c *Controller) joinChannel(channel types.Channel) {
if !c.shouldJoin(channel.ID) {
return
}
c.joinSem <- struct{}{}
ch := channel
go func() {
// TODO: filter out channels based on user ID & shard ID, so we can spread the load across kubernetes statefulset

// make sure the channel is flagged to be joined
if !bitwise.Has(ch.Flags, bitwise.JOIN_IRC) {
return
}

zap.S().Infof("joining channel: %v", ch.Username)
err := c.twitch.Join(ch.Username, ch.Weight)
if err != nil {
zap.L().Error(
Expand All @@ -93,6 +95,18 @@ func (c *Controller) joinChannel(channel types.Channel) {
}()
}

func (c *Controller) shouldJoin(userID int64) bool {
if c.cfg.Replicas < 2 {
return true
}

if int(userID)%c.cfg.Replicas == c.shardID {
return true
}

return false
}

// parses out the channel name from a PRIVMSG,
// don't use on any other type of message seen as though there's no slice length checks
func parseChannel(in string) string {
Expand Down

0 comments on commit e47262c

Please sign in to comment.