chrly/internal/mojang/provider.go

140 lines
3.1 KiB
Go
Raw Normal View History

package mojang
import (
"context"
"errors"
"regexp"
"strings"
"github.com/brunomvsouza/singleflight"
2024-02-19 18:24:12 +05:30
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.uber.org/multierr"
)
2024-02-19 18:24:12 +05:30
const ScopeName = "ely.by/chrly/internal/mojang"
var InvalidUsername = errors.New("the username passed doesn't meet Mojang's requirements")
// https://help.minecraft.net/hc/en-us/articles/4408950195341#h_01GE5JX1Z0CZ833A7S54Y195KV
var allowedUsernamesRegex = regexp.MustCompile(`(?i)^[0-9a-z_]{3,16}$`)
type UuidsProvider interface {
GetUuid(ctx context.Context, username string) (*ProfileInfo, error)
}
type TexturesProvider interface {
GetTextures(ctx context.Context, uuid string) (*ProfileResponse, error)
}
2024-02-19 18:24:12 +05:30
func NewMojangTexturesProvider(
uuidsProvider UuidsProvider,
texturesProvider TexturesProvider,
) (*MojangTexturesProvider, error) {
meter, err := newProviderMetrics(otel.GetMeterProvider().Meter(ScopeName))
if err != nil {
return nil, err
}
return &MojangTexturesProvider{
UuidsProvider: uuidsProvider,
TexturesProvider: texturesProvider,
metrics: meter,
}, nil
}
type MojangTexturesProvider struct {
UuidsProvider
TexturesProvider
2024-02-19 18:24:12 +05:30
metrics *providerMetrics
group singleflight.Group[string, *ProfileResponse]
}
func (p *MojangTexturesProvider) GetForUsername(ctx context.Context, username string) (*ProfileResponse, error) {
if !allowedUsernamesRegex.MatchString(username) {
return nil, InvalidUsername
}
username = strings.ToLower(username)
2024-02-19 18:24:12 +05:30
result, err, shared := p.group.Do(username, func() (*ProfileResponse, error) {
profile, err := p.UuidsProvider.GetUuid(ctx, username)
if err != nil {
return nil, err
}
if profile == nil {
return nil, nil
}
return p.TexturesProvider.GetTextures(ctx, profile.Id)
})
2024-02-19 18:24:12 +05:30
p.recordMetrics(ctx, shared, result, err)
return result, err
}
2024-02-19 18:24:12 +05:30
func (p *MojangTexturesProvider) recordMetrics(ctx context.Context, shared bool, result *ProfileResponse, err error) {
if shared {
p.metrics.Shared.Add(ctx, 1)
}
if err != nil {
p.metrics.Failed.Add(ctx, 1)
return
}
if result != nil {
p.metrics.Found.Add(ctx, 1)
} else {
p.metrics.Missed.Add(ctx, 1)
}
}
type NilProvider struct {
}
func (*NilProvider) GetForUsername(ctx context.Context, username string) (*ProfileResponse, error) {
return nil, nil
}
2024-02-19 18:24:12 +05:30
func newProviderMetrics(meter metric.Meter) (*providerMetrics, error) {
m := &providerMetrics{}
var errors, err error
m.Found, err = meter.Int64Counter(
"results.found",
metric.WithDescription(""), // TODO: description
)
errors = multierr.Append(errors, err)
m.Missed, err = meter.Int64Counter(
"results.missed",
metric.WithDescription(""), // TODO: description
)
errors = multierr.Append(errors, err)
m.Failed, err = meter.Int64Counter(
"results.failed",
metric.WithDescription(""), // TODO: description
)
errors = multierr.Append(errors, err)
m.Shared, err = meter.Int64Counter(
"singleflight.shared",
metric.WithDescription(""), // TODO: description
)
errors = multierr.Append(errors, err)
return m, errors
}
type providerMetrics struct {
Found metric.Int64Counter
Missed metric.Int64Counter
Failed metric.Int64Counter
Shared metric.Int64Counter
}