refactor: don't rate limit requests to TWHelp (#26)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #26
This commit is contained in:
parent
eec0909c78
commit
0a0c0b2acd
|
@ -48,7 +48,7 @@ func New() *cli.Command {
|
||||||
|
|
||||||
choiceSvc := service.NewChoice(client)
|
choiceSvc := service.NewChoice(client)
|
||||||
groupSvc := service.NewGroup(groupRepo, 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)
|
bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,7 +78,6 @@ func New() *cli.Command {
|
||||||
|
|
||||||
type botConfig struct {
|
type botConfig struct {
|
||||||
Token string `envconfig:"TOKEN" required:"true"`
|
Token string `envconfig:"TOKEN" required:"true"`
|
||||||
EnnoblementsMaxConcurrentRequests int `envconfig:"ENNOBLEMENTS_MAX_CONCURRENT_REQUESTS" default:"4"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBotConfig() (botConfig, error) {
|
func newBotConfig() (botConfig, error) {
|
||||||
|
|
|
@ -35,20 +35,17 @@ type Monitor struct {
|
||||||
groupSvc GroupReader
|
groupSvc GroupReader
|
||||||
ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements
|
ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements
|
||||||
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements
|
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements
|
||||||
ennoblementsMaxConcurrentRequests int // ennoblementsMaxConcurrentRequests is used by Monitor.fetchEnnoblements
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMonitor(
|
func NewMonitor(
|
||||||
repo MonitorRepository,
|
repo MonitorRepository,
|
||||||
groupSvc GroupReader,
|
groupSvc GroupReader,
|
||||||
client TWHelpClient,
|
client TWHelpClient,
|
||||||
ennoblementsMaxConcurrentRequests int,
|
|
||||||
) *Monitor {
|
) *Monitor {
|
||||||
return &Monitor{
|
return &Monitor{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
client: client,
|
client: client,
|
||||||
groupSvc: groupSvc,
|
groupSvc: groupSvc,
|
||||||
ennoblementsMaxConcurrentRequests: ennoblementsMaxConcurrentRequests,
|
|
||||||
ennoblementsSince: make(map[string]time.Time),
|
ennoblementsSince: make(map[string]time.Time),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -218,8 +215,6 @@ func (m *Monitor) fetchEnnoblements(ctx context.Context, groups []domain.Group)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
ch := make(chan listEnnoblementsResult)
|
ch := make(chan listEnnoblementsResult)
|
||||||
reqLimiter := make(chan struct{}, m.ennoblementsMaxConcurrentRequests)
|
|
||||||
defer close(reqLimiter)
|
|
||||||
ennoblementsSince := m.ennoblementsSince
|
ennoblementsSince := m.ennoblementsSince
|
||||||
skip := make(map[string]struct{}, len(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)
|
wg.Add(1)
|
||||||
go func(g domain.Group, since time.Time) {
|
go func(g domain.Group, since time.Time) {
|
||||||
reqLimiter <- struct{}{}
|
|
||||||
defer func() {
|
|
||||||
<-reqLimiter
|
|
||||||
}()
|
|
||||||
|
|
||||||
res := listEnnoblementsResult{
|
res := listEnnoblementsResult{
|
||||||
versionCode: g.VersionCode,
|
versionCode: g.VersionCode,
|
||||||
serverKey: g.ServerKey,
|
serverKey: g.ServerKey,
|
||||||
|
|
|
@ -56,7 +56,7 @@ func TestMonitor_Create(t *testing.T) {
|
||||||
|
|
||||||
groupID := uuid.NewString()
|
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)
|
Create(context.Background(), groupID, uuid.NewString(), tribe.Tag)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotEmpty(t, monitor.ID)
|
assert.NotEmpty(t, monitor.ID)
|
||||||
|
@ -72,7 +72,7 @@ func TestMonitor_Create(t *testing.T) {
|
||||||
groupSvc := &mock.FakeGroupReader{}
|
groupSvc := &mock.FakeGroupReader{}
|
||||||
groupSvc.GetReturns(domain.Group{}, domain.GroupNotFoundError{ID: groupID})
|
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")
|
Create(context.Background(), groupID, uuid.NewString(), "tag")
|
||||||
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
|
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
|
||||||
ID: groupID,
|
ID: groupID,
|
||||||
|
@ -101,7 +101,7 @@ func TestMonitor_Create(t *testing.T) {
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
monitor, err := service.NewMonitor(repo, groupSvc, nil, 10).
|
monitor, err := service.NewMonitor(repo, groupSvc, nil).
|
||||||
Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG")
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG")
|
||||||
assert.ErrorIs(t, err, domain.MonitorLimitReachedError{
|
assert.ErrorIs(t, err, domain.MonitorLimitReachedError{
|
||||||
Current: maxMonitorsPerGroup,
|
Current: maxMonitorsPerGroup,
|
||||||
|
@ -148,7 +148,7 @@ func TestMonitor_Create(t *testing.T) {
|
||||||
|
|
||||||
tag := "TAG"
|
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)
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
|
||||||
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
|
@ -185,7 +185,7 @@ func TestMonitor_Create(t *testing.T) {
|
||||||
}
|
}
|
||||||
client.GetTribeByTagReturns(tribe, nil)
|
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)
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag)
|
||||||
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
||||||
Tag: tribe.Tag,
|
Tag: tribe.Tag,
|
||||||
|
@ -598,7 +598,7 @@ func TestMonitor_Execute(t *testing.T) {
|
||||||
return monitors[groupID], nil
|
return monitors[groupID], nil
|
||||||
})
|
})
|
||||||
|
|
||||||
notifications, err := service.NewMonitor(repo, groupSvc, client, 10).
|
notifications, err := service.NewMonitor(repo, groupSvc, client).
|
||||||
Execute(context.Background())
|
Execute(context.Background())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
expectedNotifications := []domain.EnnoblementNotification{
|
expectedNotifications := []domain.EnnoblementNotification{
|
||||||
|
|
|
@ -34,8 +34,6 @@ spec:
|
||||||
key: token
|
key: token
|
||||||
- name: TWHELP_URL
|
- name: TWHELP_URL
|
||||||
value: "https://tribalwarshelp.com"
|
value: "https://tribalwarshelp.com"
|
||||||
- name: BOT_ENNOBLEMENTS_MAX_CONCURRENT_REQUESTS
|
|
||||||
value: "4"
|
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
exec:
|
exec:
|
||||||
command: [ "cat", "/tmp/healthy" ]
|
command: [ "cat", "/tmp/healthy" ]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user