refactor: don't rate limit requests to TWHelp (#26)
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #26
This commit is contained in:
Dawid Wysokiński 2022-10-27 11:48:27 +00:00
parent eec0909c78
commit 0a0c0b2acd
4 changed files with 17 additions and 30 deletions

View File

@ -48,7 +48,7 @@ func New() *cli.Command {
choiceSvc := service.NewChoice(client)
groupSvc := service.NewGroup(groupRepo, client)
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client, cfg.EnnoblementsMaxConcurrentRequests)
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client)
bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc)
if err != nil {
@ -77,8 +77,7 @@ func New() *cli.Command {
}
type botConfig struct {
Token string `envconfig:"TOKEN" required:"true"`
EnnoblementsMaxConcurrentRequests int `envconfig:"ENNOBLEMENTS_MAX_CONCURRENT_REQUESTS" default:"4"`
Token string `envconfig:"TOKEN" required:"true"`
}
func newBotConfig() (botConfig, error) {

View File

@ -30,26 +30,23 @@ type GroupReader interface {
}
type Monitor struct {
repo MonitorRepository
client TWHelpClient
groupSvc GroupReader
ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements
ennoblementsMaxConcurrentRequests int // ennoblementsMaxConcurrentRequests is used by Monitor.fetchEnnoblements
repo MonitorRepository
client TWHelpClient
groupSvc GroupReader
ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements
}
func NewMonitor(
repo MonitorRepository,
groupSvc GroupReader,
client TWHelpClient,
ennoblementsMaxConcurrentRequests int,
) *Monitor {
return &Monitor{
repo: repo,
client: client,
groupSvc: groupSvc,
ennoblementsMaxConcurrentRequests: ennoblementsMaxConcurrentRequests,
ennoblementsSince: make(map[string]time.Time),
repo: repo,
client: client,
groupSvc: groupSvc,
ennoblementsSince: make(map[string]time.Time),
}
}
@ -218,8 +215,6 @@ func (m *Monitor) fetchEnnoblements(ctx context.Context, groups []domain.Group)
var wg sync.WaitGroup
ch := make(chan listEnnoblementsResult)
reqLimiter := make(chan struct{}, m.ennoblementsMaxConcurrentRequests)
defer close(reqLimiter)
ennoblementsSince := m.ennoblementsSince
skip := make(map[string]struct{}, len(ennoblementsSince))
@ -242,11 +237,6 @@ func (m *Monitor) fetchEnnoblements(ctx context.Context, groups []domain.Group)
wg.Add(1)
go func(g domain.Group, since time.Time) {
reqLimiter <- struct{}{}
defer func() {
<-reqLimiter
}()
res := listEnnoblementsResult{
versionCode: g.VersionCode,
serverKey: g.ServerKey,

View File

@ -56,7 +56,7 @@ func TestMonitor_Create(t *testing.T) {
groupID := uuid.NewString()
monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
monitor, err := service.NewMonitor(repo, groupSvc, client).
Create(context.Background(), groupID, uuid.NewString(), tribe.Tag)
assert.NoError(t, err)
assert.NotEmpty(t, monitor.ID)
@ -72,7 +72,7 @@ func TestMonitor_Create(t *testing.T) {
groupSvc := &mock.FakeGroupReader{}
groupSvc.GetReturns(domain.Group{}, domain.GroupNotFoundError{ID: groupID})
monitor, err := service.NewMonitor(nil, groupSvc, nil, 10).
monitor, err := service.NewMonitor(nil, groupSvc, nil).
Create(context.Background(), groupID, uuid.NewString(), "tag")
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
ID: groupID,
@ -101,7 +101,7 @@ func TestMonitor_Create(t *testing.T) {
}, nil
})
monitor, err := service.NewMonitor(repo, groupSvc, nil, 10).
monitor, err := service.NewMonitor(repo, groupSvc, nil).
Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG")
assert.ErrorIs(t, err, domain.MonitorLimitReachedError{
Current: maxMonitorsPerGroup,
@ -148,7 +148,7 @@ func TestMonitor_Create(t *testing.T) {
tag := "TAG"
monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
monitor, err := service.NewMonitor(repo, groupSvc, client).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tag,
@ -185,7 +185,7 @@ func TestMonitor_Create(t *testing.T) {
}
client.GetTribeByTagReturns(tribe, nil)
monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
monitor, err := service.NewMonitor(repo, groupSvc, client).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tribe.Tag,
@ -598,7 +598,7 @@ func TestMonitor_Execute(t *testing.T) {
return monitors[groupID], nil
})
notifications, err := service.NewMonitor(repo, groupSvc, client, 10).
notifications, err := service.NewMonitor(repo, groupSvc, client).
Execute(context.Background())
assert.NoError(t, err)
expectedNotifications := []domain.EnnoblementNotification{

View File

@ -34,8 +34,6 @@ spec:
key: token
- name: TWHELP_URL
value: "https://tribalwarshelp.com"
- name: BOT_ENNOBLEMENTS_MAX_CONCURRENT_REQUESTS
value: "4"
livenessProbe:
exec:
command: [ "cat", "/tmp/healthy" ]