From 3d39375b99fc5d6b50c00d6d4c37cfaeb539b174 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 12 Dec 2022 14:09:00 +0100 Subject: [PATCH] chore: use generic queue --- core/loadpoint.go | 13 ++++++------ go.mod | 1 - go.sum | 2 -- util/queue.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 util/queue.go diff --git a/core/loadpoint.go b/core/loadpoint.go index 0beb2c2dd..f9fafee3f 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -24,8 +24,6 @@ import ( "github.com/avast/retry-go/v3" "github.com/benbjohnson/clock" "github.com/cjrd/allocate" - "github.com/emirpasic/gods/queues" - aq "github.com/emirpasic/gods/queues/arrayqueue" ) const ( @@ -89,6 +87,9 @@ type ThresholdConfig struct { Threshold float64 } +// Task is the task type +type Task = func() + // LoadPoint is responsible for controlling charge depending on // SoC needs and power availability. type LoadPoint struct { @@ -162,7 +163,7 @@ type LoadPoint struct { db db.Database session *db.Session - tasks queues.Queue // tasks to be executed + tasks *util.Queue[Task] // tasks to be executed } // NewLoadPointFromConfig creates a new loadpoint @@ -283,7 +284,7 @@ func NewLoadPoint(log *util.Logger) *LoadPoint { GuardDuration: 5 * time.Minute, progress: NewProgress(0, 10), // soc progress indicator coordinator: coordinator.NewDummy(), // dummy vehicle coordinator - tasks: aq.New(), // task queue + tasks: util.NewQueue[Task](), // task queue } // allow target charge handler to access loadpoint @@ -1656,7 +1657,7 @@ func (lp *LoadPoint) addTask(task func()) { // test guard if lp.tasks != nil { // don't add twice - if t, ok := lp.tasks.Peek(); ok && + if t, ok := lp.tasks.First(); ok && reflect.ValueOf(t).Pointer() == reflect.ValueOf(task).Pointer() { return } @@ -1669,7 +1670,7 @@ func (lp *LoadPoint) processTasks() { // test guard if lp.tasks != nil { if task, ok := lp.tasks.Dequeue(); ok { - task.(func())() + task() } } } diff --git a/go.mod b/go.mod index c5196d7f4..7032a71df 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,6 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/dylanmei/iso8601 v0.1.0 github.com/eclipse/paho.mqtt.golang v1.4.2 - github.com/emirpasic/gods v1.18.1 github.com/enbility/cemd v0.1.5 github.com/enbility/eebus-go v0.1.5 github.com/fatih/structs v1.1.0 diff --git a/go.sum b/go.sum index 465349112..fcac3ad1b 100644 --- a/go.sum +++ b/go.sum @@ -222,8 +222,6 @@ github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFP github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4= github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= -github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= -github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/enbility/cemd v0.1.5 h1:9DHDH90YoldCpbg2gt06y0ZgOOk6pJB0BspBMrwnUq0= github.com/enbility/cemd v0.1.5/go.mod h1:PZfCRYAe9PDI8fKLADtvotiVTym9ZJ2LZaLfi4EeIaE= github.com/enbility/eebus-go v0.1.5 h1:IkRxwN+jCqQ4TCbbtjJf70iaeSUUicZZSUsMTLFtVpg= diff --git a/util/queue.go b/util/queue.go new file mode 100644 index 000000000..93392ad26 --- /dev/null +++ b/util/queue.go @@ -0,0 +1,52 @@ +package util + +// Queue is based on https://github.com/golang-ds/queue +type Queue[T any] struct { + data []T +} + +// NewQueue constructs and returns an empty slice-queue. +func NewQueue[T any]() *Queue[T] { + return new(Queue[T]) +} + +// Enqueue adds an element to the end of the queue. +func (q *Queue[T]) Enqueue(data T) { + q.data = append(q.data, data) +} + +// Dequeue removes and returns the front element of the queue. It returns false if the queue was empty. +func (q *Queue[T]) Dequeue() (val T, ok bool) { + if q.IsEmpty() { + return + } + + val = q.data[0] + q.data = q.data[1:] + + return val, true +} + +// First returns the front element of the queue. It returns false if the queue was empty. +func (q *Queue[T]) First() (val T, ok bool) { + if q.IsEmpty() { + return + } + + return q.data[0], true +} + +// Size returns the number of the elements in the queue. +func (q *Queue[T]) Size() int { + return len(q.data) +} + +// Clear empties the queue. +func (q *Queue[T]) Clear() { + q.data = nil +} + +// IsEmpty returns true if the queue is empty. +func (q *Queue[T]) IsEmpty() bool { + return q.Size() == 0 +}