Skip to content

Commit

Permalink
replacing zap with zerolog, redis cache benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Dec 5, 2023
1 parent f311f22 commit e24bbb5
Show file tree
Hide file tree
Showing 28 changed files with 529 additions and 505 deletions.
23 changes: 8 additions & 15 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"time"

"github.com/Kyagara/equinox/cache"
"go.uber.org/zap/zapcore"
"github.com/rs/zerolog"
)

// Configuration for the Equinox client.
type EquinoxConfig struct {
// Riot API Key.
Key string
// Log level.
LogLevel LogLevel
// Zerolog log level.
LogLevel zerolog.Level
// http.Client used internally.
HTTPClient *http.Client
// Retry a request n times if it returns a 429 status code.
Expand All @@ -22,26 +22,19 @@ type EquinoxConfig struct {
Cache *cache.Cache
}

func (c *EquinoxConfig) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddInt("retry-if-429", c.Retry)
encoder.AddDuration("http-client-timeout", c.HTTPClient.Timeout)
func (c EquinoxConfig) MarshalZerologObject(encoder *zerolog.Event) {
encoder.Int("retry-if-429", c.Retry).Dur("http-client-timeout", c.HTTPClient.Timeout)
if c.Cache.TTL > 0 {
cache := CacheConfig{Store: string(c.Cache.StoreType), TTL: c.Cache.TTL}
err := encoder.AddObject("cache", cache)
if err != nil {
return err
}
encoder.Object("cache", cache)
}
return nil
}

type CacheConfig struct {
TTL time.Duration
Store string
}

func (c CacheConfig) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("store", c.Store)
encoder.AddDuration("cache-ttl", c.TTL)
return nil
func (c CacheConfig) MarshalZerologObject(encoder *zerolog.Event) {
encoder.Str("store", c.Store).Dur("cache-ttl", c.TTL)
}
19 changes: 2 additions & 17 deletions api/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ package api
import (
"net/http"

"go.uber.org/zap"
"github.com/rs/zerolog"
)

// EquinoxRequest represents a request to the Riot API and CDNs, its a struct that contains all information about a request.
type EquinoxRequest struct {
Logger *zap.Logger
Logger zerolog.Logger
MethodID string
Route any
BaseURL string
Expand All @@ -37,21 +37,6 @@ const (
C_DRAGON_BASE_URL_FORMAT = "https://cdn.communitydragon.org%s"
)

type LogLevel int8

const (
// NOP_LOG_LEVEL won't log anything, this is the default behaviour for the Default client.
NOP_LOG_LEVEL LogLevel = iota - 2
// DEBUG_LOG_LEVEL will log everything.
DEBUG_LOG_LEVEL
// INFO_LOG_LEVEL will log the requests being made and if they were successful.
INFO_LOG_LEVEL
// WARN_LOG_LEVEL will log when a request was rate limited.
WARN_LOG_LEVEL
// ERROR_LOG_LEVEL will log every error.
ERROR_LOG_LEVEL
)

// Regional routes, used in tournament services, Legends of Runeterra (LoR), and some other endpoints.
type RegionalRoute string

Expand Down
13 changes: 6 additions & 7 deletions clients/cdragon/champion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/Kyagara/equinox/api"
"github.com/Kyagara/equinox/internal"
"go.uber.org/zap"
)

type ChampionEndpoint struct {
Expand Down Expand Up @@ -122,16 +121,16 @@ type ChampionData struct {
// Retrieves more information about a champion, includes skins, spells and tips.
func (e *ChampionEndpoint) ByName(ctx context.Context, version string, champion string) (*ChampionData, error) {
logger := e.internal.Logger("CDragon_Champion_ByName")
logger.Debug("Method started execution")
logger.Debug().Msg("Method started execution")
equinoxReq, err := e.internal.Request(ctx, logger, api.C_DRAGON_BASE_URL_FORMAT, http.MethodGet, "", fmt.Sprintf(ChampionURL, version, champion), "", nil)
if err != nil {
logger.Error("Error creating request", zap.Error(err))
logger.Error().Err(err).Msg("Error creating request")
return nil, err
}
var data ChampionData
err = e.internal.Execute(ctx, equinoxReq, &data)
if err != nil {
logger.Error("Error executing request", zap.Error(err))
logger.Error().Err(err).Msg("Error executing request")
return nil, err
}
return &data, nil
Expand All @@ -140,16 +139,16 @@ func (e *ChampionEndpoint) ByName(ctx context.Context, version string, champion
// Retrieves more information about a champion, includes skins, spells and tips.
func (e *ChampionEndpoint) ByID(ctx context.Context, version string, id int) (*ChampionData, error) {
logger := e.internal.Logger("CDragon_Champion_ByID")
logger.Debug("Method started execution")
logger.Debug().Msg("Method started execution")
equinoxReq, err := e.internal.Request(ctx, logger, api.C_DRAGON_BASE_URL_FORMAT, http.MethodGet, "", fmt.Sprintf(ChampionURL, version, id), "", nil)
if err != nil {
logger.Error("Error creating request", zap.Error(err))
logger.Error().Err(err).Msg("Error creating request")
return nil, err
}
var data ChampionData
err = e.internal.Execute(ctx, equinoxReq, &data)
if err != nil {
logger.Error("Error executing request", zap.Error(err))
logger.Error().Err(err).Msg("Error executing request")
return nil, err
}
return &data, nil
Expand Down
13 changes: 6 additions & 7 deletions clients/ddragon/champion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/Kyagara/equinox/api"
"github.com/Kyagara/equinox/internal"
"go.uber.org/zap"
)

type ChampionEndpoint struct {
Expand Down Expand Up @@ -145,16 +144,16 @@ type Image struct {
// Get all champions basic information, includes stats, tags, title and blurb.
func (e *ChampionEndpoint) AllChampions(ctx context.Context, version string, language Language) (map[string]Champion, error) {
logger := e.internal.Logger("DDragon_Champion_AllChampions")
logger.Debug("Method started execution")
logger.Debug().Msg("Method started execution")
equinoxReq, err := e.internal.Request(ctx, logger, api.D_DRAGON_BASE_URL_FORMAT, http.MethodGet, "", fmt.Sprintf(ChampionsURL, version, language), "", nil)
if err != nil {
logger.Error("Error creating request", zap.Error(err))
logger.Error().Err(err).Msg("Error creating request")
return nil, err
}
var data ChampionsData
err = e.internal.Execute(ctx, equinoxReq, &data)
if err != nil {
logger.Error("Error executing request", zap.Error(err))
logger.Error().Err(err).Msg("Error executing request")
return nil, err
}
return data.Data, nil
Expand All @@ -163,16 +162,16 @@ func (e *ChampionEndpoint) AllChampions(ctx context.Context, version string, lan
// Retrieves more information about a champion, includes skins, spells and tips.
func (e *ChampionEndpoint) ByName(ctx context.Context, version string, language Language, champion string) (*FullChampion, error) {
logger := e.internal.Logger("DDragon_Champion_ByName")
logger.Debug("Method started execution")
logger.Debug().Msg("Method started execution")
equinoxReq, err := e.internal.Request(ctx, logger, api.D_DRAGON_BASE_URL_FORMAT, http.MethodGet, "", fmt.Sprintf(ChampionURL, version, language, champion), "", nil)
if err != nil {
logger.Error("Error creating request", zap.Error(err))
logger.Error().Err(err).Msg("Error creating request")
return nil, err
}
var data FullChampionData
err = e.internal.Execute(ctx, equinoxReq, &data)
if err != nil {
logger.Error("Error executing request", zap.Error(err))
logger.Error().Err(err).Msg("Error executing request")
return nil, err
}
c := data.Data[champion]
Expand Down
7 changes: 3 additions & 4 deletions clients/ddragon/realms.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/Kyagara/equinox/api"
"github.com/Kyagara/equinox/internal"
"go.uber.org/zap"
)

type RealmEndpoint struct {
Expand Down Expand Up @@ -38,16 +37,16 @@ type RealmData struct {

func (e *RealmEndpoint) ByName(ctx context.Context, realm Realm) (*RealmData, error) {
logger := e.internal.Logger("DDragon_Realm_ByName")
logger.Debug("Method started execution")
logger.Debug().Msg("Method started execution")
equinoxReq, err := e.internal.Request(ctx, logger, api.D_DRAGON_BASE_URL_FORMAT, http.MethodGet, "", fmt.Sprintf(RealmURL, realm), "", nil)
if err != nil {
logger.Error("Error creating request", zap.Error(err))
logger.Error().Err(err).Msg("Error creating request")
return nil, err
}
var data RealmData
err = e.internal.Execute(ctx, equinoxReq, &data)
if err != nil {
logger.Error("Error executing request", zap.Error(err))
logger.Error().Err(err).Msg("Error executing request")
return nil, err
}
return &data, nil
Expand Down
Loading

0 comments on commit e24bbb5

Please sign in to comment.