Skip to content

Commit

Permalink
feat: add redis sentiel url env
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Dec 6, 2023
1 parent b808571 commit 3e2d724
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 53 deletions.
29 changes: 27 additions & 2 deletions pkg/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,33 @@ type redisCache struct {
ctx context.Context
}

func NewRedisCache(opts *redis.Options) (Cache, error) {
rdb := redis.NewClient(opts)
type RedisOpts struct {
URL string
SentinelURLs []string
MasterName string
}

func NewRedisCache(opts RedisOpts) (Cache, error) {
var rdb *redis.Client
if opts.MasterName == "" {
redisURL := opts.URL
if redisURL == "" {
return nil, fmt.Errorf("redis url is not set")
}
redisOpt, err := redis.ParseURL(redisURL)
if err != nil {
return nil, err
}
rdb = redis.NewClient(redisOpt)
} else {
if len(opts.SentinelURLs) == 0 {
return nil, fmt.Errorf("redis sentinel url is not set")
}
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
SentinelAddrs: opts.SentinelURLs,
MasterName: opts.MasterName,
})
}
ctx := context.Background()

// ping to test connection
Expand Down
9 changes: 7 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ type Config struct {
DiscordToken string
ApplicationID string

RedisURL string
RedisURL string
RedisSentinelURL string
RedisMasterName string

MochiGuildID string
MochiLogChannelID string
Expand Down Expand Up @@ -259,7 +261,10 @@ func generateConfigFromViper(v *viper.Viper) Config {
InDiscordWalletMnemonic: v.GetString("IN_DISCORD_WALLET_MNEMONIC"),
VaultMnemonic: v.GetString("VAULT_MNEMONIC"),
SolanaVaultMnemonic: v.GetString("SOLANA_VAULT_MNEMONIC"),
RedisURL: v.GetString("REDIS_URL"),

RedisURL: v.GetString("REDIS_URL"),
RedisSentinelURL: v.GetString("REDIS_SENTINEL_URL"),
RedisMasterName: v.GetString("REDIS_MASTER_NAME"),

MochiGuildID: v.GetString("MOCHI_GUILD_ID"),
MochiLogChannelID: v.GetString("MOCHI_LOG_CHANNEL_ID"),
Expand Down
13 changes: 6 additions & 7 deletions pkg/entities/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package entities

import (
"errors"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis/v8"
"github.com/go-rod/rod"

"github.com/defipod/mochi/pkg/cache"
Expand Down Expand Up @@ -77,12 +77,11 @@ func Init(cfg config.Config, log logger.Logger) error {
log.Infof("Connected to discord: %s", u.Username)

// *** cache ***
redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Fatal(err, "failed to init redis")
}

cache, err := cache.NewRedisCache(redisOpt)
cache, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
log.Fatal(err, "failed to init redis cache")
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/handler/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"encoding/json"
"fmt"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"

"github.com/defipod/mochi/pkg/cache"
"github.com/defipod/mochi/pkg/config"
Expand All @@ -35,13 +35,11 @@ func Test_HandleDiscordWebhook(t *testing.T) {
RedisURL: "redis://localhost:6379/0",
}

redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
t.Error(err)
return
}

cache, err := cache.NewRedisCache(redisOpt)
cache, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
t.Error(err)
return
Expand Down
15 changes: 7 additions & 8 deletions pkg/job/watch_coin_price_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package job
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"

"github.com/defipod/mochi/pkg/cache"
Expand Down Expand Up @@ -42,15 +40,16 @@ func (m watchCoinPriceChangePayload) MarshalBinary() ([]byte, error) {

func NewWatchCoinPriceChange(e *entities.Entity, l logger.Logger) Job {
cfg := config.LoadConfig(config.DefaultConfigLoaders())
redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Fatal(err, "[WatchCoinPriceChanges] failed to init redis")
}

cache, err := cache.NewRedisCache(redisOpt)
cache, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
log.Fatal(err, "[WatchCoinPriceChanges] failed to init redis cache")
l.Fatal(err, "[NewWatchCoinPriceChange] failed to init redis cache")
}

return &watchCoinPriceChanges{
entity: e,
log: l,
Expand Down
16 changes: 7 additions & 9 deletions pkg/job/watch_dm_notify_price_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package job

import (
"encoding/json"
"strings"
"time"

"github.com/go-redis/redis/v8"

"github.com/defipod/mochi/pkg/cache"
"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/entities"
Expand All @@ -26,14 +25,13 @@ func NewWatchDMNotifyPriceAlert(e *entities.Entity, l logger.Logger) Job {
cfg := config.LoadConfig(config.DefaultConfigLoaders())

// init redis
redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
l.Fatal(err, "[WatchDMNotifyPriceAlert] failed to init redis")
}

cache, err := cache.NewRedisCache(redisOpt)
cache, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
l.Fatal(err, "[WatchDMNotifyPriceAlert] failed to init redis cache")
l.Fatal(err, "[NewWatchDMNotifyPriceAlert] failed to init redis cache")
}

service, err := service.NewService(cfg, l)
Expand Down
17 changes: 8 additions & 9 deletions pkg/job/watch_key_price_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"time"

"github.com/consolelabs/mochi-typeset/typeset"
"github.com/shopspring/decimal"

"github.com/defipod/mochi/pkg/cache"
"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/entities"
"github.com/defipod/mochi/pkg/kafka/message"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/model"
"github.com/defipod/mochi/pkg/request"
"github.com/go-redis/redis/v8"
"github.com/shopspring/decimal"
)

type watchKeyPriceChanges struct {
Expand All @@ -32,14 +32,13 @@ const (
func NewWatchKeyPriceChange(e *entities.Entity, l logger.Logger) Job {
cfg := config.LoadConfig(config.DefaultConfigLoaders())

redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Fatal(err, "[WatchCoinPriceChanges] failed to init redis")
}

c, err := cache.NewRedisCache(redisOpt)
c, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
log.Fatal(err, "[WatchCoinPriceChanges] failed to init redis cache")
log.Fatal(err, "[NewWatchKeyPriceChange] failed to init redis cache")
}

return &watchKeyPriceChanges{
Expand Down
14 changes: 6 additions & 8 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package service

import (
"fmt"

"github.com/go-redis/redis/v8"
"strings"

"github.com/defipod/mochi/pkg/cache"
"github.com/defipod/mochi/pkg/config"
Expand Down Expand Up @@ -80,12 +79,11 @@ func NewService(
return nil, fmt.Errorf("failed to init discord: %w", err)
}

redisOpt, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
log.Fatal(err, "failed to init redis")
}

cache, err := cache.NewRedisCache(redisOpt)
cache, err := cache.NewRedisCache(cache.RedisOpts{
URL: cfg.RedisURL,
SentinelURLs: strings.Split(cfg.RedisSentinelURL, ","),
MasterName: cfg.RedisMasterName,
})
if err != nil {
log.Fatal(err, "failed to init redis cache")
}
Expand Down

0 comments on commit 3e2d724

Please sign in to comment.