2024-01-10 06:12:10 +05:30
|
|
|
package mojang
|
|
|
|
|
|
|
|
import (
|
2024-02-07 06:06:18 +05:30
|
|
|
"context"
|
2024-01-10 06:12:10 +05:30
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-02-19 18:24:12 +05:30
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
"go.uber.org/multierr"
|
2024-02-13 06:38:42 +05:30
|
|
|
|
2024-03-13 05:59:26 +05:30
|
|
|
"ely.by/chrly/internal/otel"
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/utils"
|
2024-01-10 06:12:10 +05:30
|
|
|
)
|
|
|
|
|
2024-02-13 06:38:42 +05:30
|
|
|
type UsernamesToUuidsEndpoint func(ctx context.Context, usernames []string) ([]*ProfileInfo, error)
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
type BatchUuidsProvider struct {
|
2024-02-13 06:38:42 +05:30
|
|
|
UsernamesToUuidsEndpoint
|
|
|
|
batch int
|
|
|
|
delay time.Duration
|
|
|
|
fireOnFull bool
|
2024-01-10 06:12:10 +05:30
|
|
|
|
|
|
|
queue *utils.Queue[*job]
|
|
|
|
fireChan chan any
|
|
|
|
stopChan chan any
|
|
|
|
onFirstCall sync.Once
|
2024-02-19 18:24:12 +05:30
|
|
|
metrics *batchUuidsProviderMetrics
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func NewBatchUuidsProvider(
|
2024-02-13 06:38:42 +05:30
|
|
|
endpoint UsernamesToUuidsEndpoint,
|
2024-01-10 06:12:10 +05:30
|
|
|
batchSize int,
|
|
|
|
awaitDelay time.Duration,
|
|
|
|
fireOnFull bool,
|
2024-02-19 18:24:12 +05:30
|
|
|
) (*BatchUuidsProvider, error) {
|
|
|
|
queue := utils.NewQueue[*job]()
|
|
|
|
|
2024-03-13 05:59:26 +05:30
|
|
|
metrics, err := newBatchUuidsProviderMetrics(otel.GetMeter(), queue)
|
2024-02-19 18:24:12 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
return &BatchUuidsProvider{
|
|
|
|
UsernamesToUuidsEndpoint: endpoint,
|
|
|
|
stopChan: make(chan any),
|
|
|
|
batch: batchSize,
|
|
|
|
delay: awaitDelay,
|
|
|
|
fireOnFull: fireOnFull,
|
2024-02-19 18:24:12 +05:30
|
|
|
queue: queue,
|
2024-01-10 06:12:10 +05:30
|
|
|
fireChan: make(chan any),
|
2024-02-19 18:24:12 +05:30
|
|
|
metrics: metrics,
|
|
|
|
}, nil
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type job struct {
|
2024-02-19 18:24:12 +05:30
|
|
|
Username string
|
|
|
|
Ctx context.Context
|
|
|
|
QueuingTime time.Time
|
|
|
|
ResultChan chan<- *jobResult
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type jobResult struct {
|
|
|
|
Profile *ProfileInfo
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *BatchUuidsProvider) GetUuid(ctx context.Context, username string) (*ProfileInfo, error) {
|
2024-01-10 06:12:10 +05:30
|
|
|
resultChan := make(chan *jobResult)
|
2024-02-19 18:24:12 +05:30
|
|
|
n := p.queue.Enqueue(&job{username, ctx, time.Now(), resultChan})
|
2024-02-07 06:06:18 +05:30
|
|
|
if p.fireOnFull && n%p.batch == 0 {
|
|
|
|
p.fireChan <- struct{}{}
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
p.onFirstCall.Do(p.startQueue)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case result := <-resultChan:
|
|
|
|
return result.Profile, result.Error
|
|
|
|
}
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *BatchUuidsProvider) StopQueue() {
|
|
|
|
close(p.stopChan)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *BatchUuidsProvider) startQueue() {
|
2024-01-10 06:12:10 +05:30
|
|
|
go func() {
|
|
|
|
for {
|
2024-02-07 06:06:18 +05:30
|
|
|
t := time.NewTimer(p.delay)
|
2024-01-10 06:12:10 +05:30
|
|
|
select {
|
2024-02-07 06:06:18 +05:30
|
|
|
case <-p.stopChan:
|
2024-01-10 06:12:10 +05:30
|
|
|
return
|
|
|
|
case <-t.C:
|
2024-02-07 06:06:18 +05:30
|
|
|
go p.fireRequest()
|
|
|
|
case <-p.fireChan:
|
2024-01-10 06:12:10 +05:30
|
|
|
t.Stop()
|
2024-02-07 06:06:18 +05:30
|
|
|
go p.fireRequest()
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *BatchUuidsProvider) fireRequest() {
|
2024-02-19 18:24:12 +05:30
|
|
|
// Since this method is an aggregator, it uses its own context to manage its lifetime
|
|
|
|
reqCtx := context.Background()
|
2024-02-07 06:06:18 +05:30
|
|
|
jobs := make([]*job, 0, p.batch)
|
|
|
|
n := p.batch
|
|
|
|
for {
|
|
|
|
foundJobs, left := p.queue.Dequeue(n)
|
|
|
|
for i := range foundJobs {
|
2024-03-05 19:44:10 +05:30
|
|
|
p.metrics.QueueTime.Record(reqCtx, float64(time.Since(foundJobs[i].QueuingTime).Milliseconds()))
|
2024-02-07 06:06:18 +05:30
|
|
|
if foundJobs[i].Ctx.Err() != nil {
|
|
|
|
// If the job context has already ended, its result will be returned in the GetUuid method
|
|
|
|
close(foundJobs[i].ResultChan)
|
|
|
|
|
|
|
|
foundJobs[i] = foundJobs[len(foundJobs)-1]
|
|
|
|
foundJobs = foundJobs[:len(foundJobs)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jobs = append(jobs, foundJobs...)
|
|
|
|
if len(jobs) != p.batch && left != 0 {
|
|
|
|
n = p.batch - len(jobs)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
if len(jobs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usernames := make([]string, len(jobs))
|
|
|
|
for i, job := range jobs {
|
|
|
|
usernames[i] = job.Username
|
|
|
|
}
|
|
|
|
|
2024-03-05 19:44:10 +05:30
|
|
|
p.metrics.Requests.Add(reqCtx, 1)
|
2024-02-19 18:24:12 +05:30
|
|
|
p.metrics.BatchSize.Record(reqCtx, int64(len(usernames)))
|
|
|
|
|
|
|
|
profiles, err := p.UsernamesToUuidsEndpoint(reqCtx, usernames)
|
2024-01-10 06:12:10 +05:30
|
|
|
for _, job := range jobs {
|
|
|
|
response := &jobResult{}
|
|
|
|
if err == nil {
|
|
|
|
// The profiles in the response aren't ordered, so we must search each username over full array
|
|
|
|
for _, profile := range profiles {
|
|
|
|
if strings.EqualFold(job.Username, profile.Name) {
|
|
|
|
response.Profile = profile
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
response.Error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
job.ResultChan <- response
|
|
|
|
close(job.ResultChan)
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 18:24:12 +05:30
|
|
|
|
|
|
|
func newBatchUuidsProviderMetrics(meter metric.Meter, queue *utils.Queue[*job]) (*batchUuidsProviderMetrics, error) {
|
|
|
|
m := &batchUuidsProviderMetrics{}
|
|
|
|
var errors, err error
|
|
|
|
|
2024-03-05 19:44:10 +05:30
|
|
|
m.Requests, err = meter.Int64Counter(
|
2024-03-13 05:59:26 +05:30
|
|
|
"chrly.mojang.uuids.batch.request.sent",
|
2024-03-05 19:44:10 +05:30
|
|
|
metric.WithDescription("Number of UUIDs requests sent to Mojang API"),
|
|
|
|
metric.WithUnit("1"),
|
|
|
|
)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
m.BatchSize, err = meter.Int64Histogram(
|
2024-03-13 05:59:26 +05:30
|
|
|
"chrly.mojang.uuids.batch.request.batch_size",
|
2024-03-05 19:44:10 +05:30
|
|
|
metric.WithDescription("The number of usernames in the query"),
|
|
|
|
metric.WithUnit("1"),
|
|
|
|
)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
2024-02-19 18:24:12 +05:30
|
|
|
m.QueueLength, err = meter.Int64ObservableGauge(
|
2024-03-13 05:59:26 +05:30
|
|
|
"chrly.mojang.uuids.batch.queue.length",
|
2024-03-05 19:44:10 +05:30
|
|
|
metric.WithDescription("Number of tasks in the queue waiting for execution"),
|
2024-02-19 18:24:12 +05:30
|
|
|
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
|
|
|
|
o.Observe(int64(queue.Len()))
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
m.QueueTime, err = meter.Float64Histogram(
|
2024-03-13 05:59:26 +05:30
|
|
|
"chrly.mojang.uuids.batch.queue.lag",
|
2024-03-05 19:44:10 +05:30
|
|
|
metric.WithDescription("Lag between placing a job in the queue and starting its processing"),
|
2024-02-19 18:24:12 +05:30
|
|
|
metric.WithUnit("ms"),
|
|
|
|
)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
return m, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
type batchUuidsProviderMetrics struct {
|
2024-03-05 19:44:10 +05:30
|
|
|
Requests metric.Int64Counter
|
|
|
|
BatchSize metric.Int64Histogram
|
2024-02-19 18:24:12 +05:30
|
|
|
QueueLength metric.Int64ObservableGauge
|
|
|
|
QueueTime metric.Float64Histogram
|
|
|
|
}
|