2019-11-21 04:03:13 +05:30
|
|
|
package mojangtextures
|
|
|
|
|
|
|
|
import (
|
2020-04-24 15:50:03 +05:30
|
|
|
"context"
|
2019-11-21 04:03:13 +05:30
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jobResult struct {
|
2020-04-24 15:50:03 +05:30
|
|
|
Profile *mojang.ProfileInfo
|
|
|
|
Error error
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
type job struct {
|
|
|
|
Username string
|
|
|
|
RespondChan chan *jobResult
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type jobsQueue struct {
|
|
|
|
lock sync.Mutex
|
2020-04-24 15:50:03 +05:30
|
|
|
items []*job
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func newJobsQueue() *jobsQueue {
|
|
|
|
return &jobsQueue{
|
|
|
|
items: []*job{},
|
|
|
|
}
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func (s *jobsQueue) Enqueue(job *job) int {
|
2019-11-21 04:03:13 +05:30
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
s.items = append(s.items, job)
|
|
|
|
|
|
|
|
return len(s.items)
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func (s *jobsQueue) Dequeue(n int) ([]*job, int) {
|
2019-11-21 04:03:13 +05:30
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
l := len(s.items)
|
|
|
|
if n > l {
|
|
|
|
n = l
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
items := s.items[0:n]
|
2020-04-24 15:50:03 +05:30
|
|
|
s.items = s.items[n:l]
|
2019-11-21 04:03:13 +05:30
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
return items, l - n
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
var usernamesToUuids = mojang.UsernamesToUuids
|
2020-01-03 03:34:23 +05:30
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
type JobsIteration struct {
|
|
|
|
Jobs []*job
|
|
|
|
Queue int
|
2020-04-26 06:18:23 +05:30
|
|
|
c chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JobsIteration) Done() {
|
|
|
|
if j.c != nil {
|
|
|
|
close(j.c)
|
|
|
|
}
|
2020-01-03 03:34:23 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
type BatchUuidsProviderStrategy interface {
|
|
|
|
Queue(job *job)
|
|
|
|
GetJobs(abort context.Context) <-chan *JobsIteration
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
type PeriodicStrategy struct {
|
|
|
|
Delay time.Duration
|
|
|
|
Batch int
|
|
|
|
queue *jobsQueue
|
2020-04-26 06:18:23 +05:30
|
|
|
done chan struct{}
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func NewPeriodicStrategy(delay time.Duration, batch int) *PeriodicStrategy {
|
|
|
|
return &PeriodicStrategy{
|
|
|
|
Delay: delay,
|
|
|
|
Batch: batch,
|
|
|
|
queue: newJobsQueue(),
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 15:58:10 +05:30
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func (ctx *PeriodicStrategy) Queue(job *job) {
|
|
|
|
ctx.queue.Enqueue(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *PeriodicStrategy) GetJobs(abort context.Context) <-chan *JobsIteration {
|
|
|
|
ch := make(chan *JobsIteration)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-abort.Done():
|
2020-04-26 06:18:23 +05:30
|
|
|
close(ch)
|
2020-04-24 15:50:03 +05:30
|
|
|
return
|
|
|
|
case <-time.After(ctx.Delay):
|
|
|
|
jobs, queueLen := ctx.queue.Dequeue(ctx.Batch)
|
2020-04-26 06:18:23 +05:30
|
|
|
jobDoneChan := make(chan struct{})
|
|
|
|
ch <- &JobsIteration{jobs, queueLen, jobDoneChan}
|
|
|
|
<-jobDoneChan
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-11-21 04:03:13 +05:30
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
type FullBusStrategy struct {
|
2020-04-26 06:18:23 +05:30
|
|
|
Delay time.Duration
|
|
|
|
Batch int
|
|
|
|
queue *jobsQueue
|
|
|
|
busIsFull chan bool
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func NewFullBusStrategy(delay time.Duration, batch int) *FullBusStrategy {
|
|
|
|
return &FullBusStrategy{
|
2020-04-26 06:18:23 +05:30
|
|
|
Delay: delay,
|
|
|
|
Batch: batch,
|
|
|
|
queue: newJobsQueue(),
|
|
|
|
busIsFull: make(chan bool),
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *FullBusStrategy) Queue(job *job) {
|
|
|
|
n := ctx.queue.Enqueue(job)
|
2020-04-26 06:18:23 +05:30
|
|
|
if n % ctx.Batch == 0 {
|
|
|
|
ctx.busIsFull <- true
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:18:23 +05:30
|
|
|
// Формально, это описание логики водителя маршрутки xD
|
2020-04-24 15:50:03 +05:30
|
|
|
func (ctx *FullBusStrategy) GetJobs(abort context.Context) <-chan *JobsIteration {
|
|
|
|
ch := make(chan *JobsIteration)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
t := time.NewTimer(ctx.Delay)
|
|
|
|
select {
|
|
|
|
case <-abort.Done():
|
2020-04-26 06:18:23 +05:30
|
|
|
close(ch)
|
2020-04-24 15:50:03 +05:30
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
ctx.sendJobs(ch)
|
2020-04-26 06:18:23 +05:30
|
|
|
case <-ctx.busIsFull:
|
2020-04-24 15:50:03 +05:30
|
|
|
t.Stop()
|
|
|
|
ctx.sendJobs(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *FullBusStrategy) sendJobs(ch chan *JobsIteration) {
|
|
|
|
jobs, queueLen := ctx.queue.Dequeue(ctx.Batch)
|
2020-04-26 06:18:23 +05:30
|
|
|
ch <- &JobsIteration{jobs, queueLen, nil}
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type BatchUuidsProvider struct {
|
|
|
|
context context.Context
|
|
|
|
emitter Emitter
|
|
|
|
strategy BatchUuidsProviderStrategy
|
2019-11-21 04:03:13 +05:30
|
|
|
onFirstCall sync.Once
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
|
2020-04-26 06:18:23 +05:30
|
|
|
func NewBatchUuidsProvider(
|
|
|
|
context context.Context,
|
|
|
|
strategy BatchUuidsProviderStrategy,
|
|
|
|
emitter Emitter,
|
|
|
|
) *BatchUuidsProvider {
|
2020-04-24 15:50:03 +05:30
|
|
|
return &BatchUuidsProvider{
|
|
|
|
context: context,
|
|
|
|
emitter: emitter,
|
|
|
|
strategy: strategy,
|
|
|
|
}
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BatchUuidsProvider) GetUuid(username string) (*mojang.ProfileInfo, error) {
|
2020-04-24 15:50:03 +05:30
|
|
|
ctx.onFirstCall.Do(ctx.startQueue)
|
2019-11-21 04:03:13 +05:30
|
|
|
|
|
|
|
resultChan := make(chan *jobResult)
|
2020-04-24 15:50:03 +05:30
|
|
|
ctx.strategy.Queue(&job{username, resultChan})
|
|
|
|
ctx.emitter.Emit("mojang_textures:batch_uuids_provider:queued", username)
|
2019-11-21 04:03:13 +05:30
|
|
|
|
|
|
|
result := <-resultChan
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
return result.Profile, result.Error
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *BatchUuidsProvider) startQueue() {
|
|
|
|
go func() {
|
2020-04-24 15:50:03 +05:30
|
|
|
jobsChan := ctx.strategy.GetJobs(ctx.context)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.context.Done():
|
|
|
|
return
|
|
|
|
case iteration := <-jobsChan:
|
2020-04-26 06:18:23 +05:30
|
|
|
go func() {
|
|
|
|
ctx.performRequest(iteration)
|
|
|
|
iteration.Done()
|
|
|
|
}()
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
func (ctx *BatchUuidsProvider) performRequest(iteration *JobsIteration) {
|
|
|
|
usernames := make([]string, len(iteration.Jobs))
|
|
|
|
for i, job := range iteration.Jobs {
|
|
|
|
usernames[i] = job.Username
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
ctx.emitter.Emit("mojang_textures:batch_uuids_provider:round", usernames, iteration.Queue)
|
2020-02-08 15:58:10 +05:30
|
|
|
if len(usernames) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:03:13 +05:30
|
|
|
profiles, err := usernamesToUuids(usernames)
|
2020-04-24 15:50:03 +05:30
|
|
|
ctx.emitter.Emit("mojang_textures:batch_uuids_provider:result", usernames, profiles, err)
|
|
|
|
for _, job := range iteration.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
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
}
|
2020-04-24 15:50:03 +05:30
|
|
|
} else {
|
|
|
|
response.Error = err
|
|
|
|
}
|
2019-11-21 04:03:13 +05:30
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
job.RespondChan <- response
|
|
|
|
close(job.RespondChan)
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
}
|