mirror of
https://github.com/elyby/chrly.git
synced 2024-12-24 05:59:54 +05:30
#1: Pull queue into struct, add storage interface
This commit is contained in:
parent
879a33344b
commit
fd4e5eb9ca
@ -8,43 +8,50 @@ import (
|
|||||||
"github.com/elyby/chrly/api/mojang"
|
"github.com/elyby/chrly/api/mojang"
|
||||||
)
|
)
|
||||||
|
|
||||||
var onFirstCall sync.Once
|
var usernamesToUuids = mojang.UsernamesToUuids
|
||||||
var queue = jobsQueue{}
|
var uuidToTextures = mojang.UuidToTextures
|
||||||
|
|
||||||
func ScheduleTexturesForUsername(username string) (resultChan chan *mojang.SignedTexturesResponse) {
|
type JobsQueue struct {
|
||||||
onFirstCall.Do(func() {
|
Storage Storage
|
||||||
queue.New()
|
|
||||||
startQueue()
|
onFirstCall sync.Once
|
||||||
|
queue jobsQueue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *JobsQueue) GetTexturesForUsername(username string) (resultChan chan *mojang.SignedTexturesResponse) {
|
||||||
|
ctx.onFirstCall.Do(func() {
|
||||||
|
ctx.queue.New()
|
||||||
|
ctx.startQueue()
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: prevent of adding the same username more than once
|
// TODO: prevent of adding the same username more than once
|
||||||
queue.Enqueue(&jobItem{username, resultChan})
|
ctx.queue.Enqueue(&jobItem{username, resultChan})
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func startQueue() {
|
func (ctx *JobsQueue) startQueue() {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
queueRound()
|
ctx.queueRound()
|
||||||
time.Sleep(time.Second - time.Since(start))
|
time.Sleep(time.Second - time.Since(start))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueRound() {
|
func (ctx *JobsQueue) queueRound() {
|
||||||
if queue.IsEmpty() {
|
if ctx.queue.IsEmpty() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jobs := queue.Dequeue(100)
|
jobs := ctx.queue.Dequeue(100)
|
||||||
var usernames []string
|
var usernames []string
|
||||||
for _, job := range jobs {
|
for _, job := range jobs {
|
||||||
usernames = append(usernames, job.Username)
|
usernames = append(usernames, job.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
profiles, err := mojang.UsernamesToUuids(usernames)
|
profiles, err := usernamesToUuids(usernames)
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case *mojang.TooManyRequestsError:
|
case *mojang.TooManyRequestsError:
|
||||||
for _, job := range jobs {
|
for _, job := range jobs {
|
||||||
@ -71,7 +78,7 @@ func queueRound() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if uuid != "" {
|
if uuid != "" {
|
||||||
result, err = mojang.UuidToTextures(uuid, true)
|
result, err = uuidToTextures(uuid, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(*mojang.TooManyRequestsError); !ok {
|
if _, ok := err.(*mojang.TooManyRequestsError); !ok {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
19
api/mojang/queue/storage.go
Normal file
19
api/mojang/queue/storage.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package queue
|
||||||
|
|
||||||
|
import "github.com/elyby/chrly/api/mojang"
|
||||||
|
|
||||||
|
type Storage interface {
|
||||||
|
Get(username string) *mojang.SignedTexturesResponse
|
||||||
|
Set(textures *mojang.SignedTexturesResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NilStorage used for testing purposes
|
||||||
|
type NilStorage struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*NilStorage) Get(username string) *mojang.SignedTexturesResponse {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*NilStorage) Set(textures *mojang.SignedTexturesResponse) {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user