#1: Rough implementation of textures queue

This commit is contained in:
ErickSkrauch
2019-04-15 00:52:10 +03:00
parent 44f3ee7413
commit d2d6d07fa6
3 changed files with 194 additions and 0 deletions

51
api/mojang/queue/queue.go Normal file
View File

@@ -0,0 +1,51 @@
// Based on the implementation from https://flaviocopes.com/golang-data-structure-queue/
package queue
import (
"sync"
"github.com/elyby/chrly/api/mojang"
)
type Job struct {
Username string
RespondTo chan *mojang.SignedTexturesResponse
}
type JobsQueue struct {
items []*Job
lock sync.RWMutex
}
func (s *JobsQueue) New() *JobsQueue {
s.items = []*Job{}
return s
}
func (s *JobsQueue) Enqueue(t *Job) {
s.lock.Lock()
s.items = append(s.items, t)
s.lock.Unlock()
}
func (s *JobsQueue) Dequeue(n int) []*Job {
s.lock.Lock()
if n > s.Size() {
n = s.Size()
}
items := s.items[0:n]
s.items = s.items[n:len(s.items)]
s.lock.Unlock()
return items
}
func (s *JobsQueue) IsEmpty() bool {
return len(s.items) == 0
}
func (s *JobsQueue) Size() int {
return len(s.items)
}