Skip to content

Commit 4ab88e3

Browse files
authored
[#102]: feat(refactor): code cleanup
## Description of Changes - Remove unused data structures - Cleanup tests
2 parents 9e15174 + 5686989 commit 4ab88e3

File tree

7 files changed

+49
-30
lines changed

7 files changed

+49
-30
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
CHANGELOG
22
=========
33

4+
v1.1.0 (14.10.2021)
5+
-------------------
6+
- Fix a bug where disabled vertices don't move the LL pointer forward during the shutdown.
7+
- Prevent from calling Init() method on the already initialized vertices.
8+
- Code cleanup.
9+
10+
## 👀 Packages:
11+
12+
- Go updated to 1.17
13+
- Zap logger updated to 1.19
14+
- Spiral/errors updated to 1.0.12
15+
- Github actions moved to Go 1.17
16+
417
v1.0.3 (19.08.2021)
518
-------------------
619

pkg/container/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/cenkalti/backoff/v4"
77
"github.com/spiral/endure/pkg/fsm"
88
"github.com/spiral/endure/pkg/graph"
9-
"github.com/spiral/endure/pkg/linked_list"
9+
ll "github.com/spiral/endure/pkg/linked_list"
1010
"github.com/spiral/endure/pkg/vertex"
1111
"github.com/spiral/errors"
1212
"go.uber.org/zap"
@@ -20,7 +20,7 @@ func (e *Endure) sendResultToUser(res *result) {
2020
}
2121

2222
// traverseBackStop used to visit every Prev node and internalStop vertices
23-
func (e *Endure) traverseBackStop(n *linked_list.DllNode) {
23+
func (e *Endure) traverseBackStop(n *ll.DllNode) {
2424
const op = errors.Op("endure_traverse_back_stop")
2525
e.logger.Debug("stopping vertex in the first Serve call", zap.String("id", n.Vertex.ID))
2626
nCopy := n
@@ -76,7 +76,7 @@ func (e *Endure) retryHandler(res *result) {
7676
b.MaxElapsedTime = e.maxInterval
7777
b.InitialInterval = e.initialInterval
7878

79-
affectedRunList := linked_list.NewDoublyLinkedList()
79+
affectedRunList := ll.NewDoublyLinkedList()
8080
for i := 0; i <= len(sorted)-1; i++ {
8181
affectedRunList.Push(sorted[i])
8282
}

pkg/container/endure.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/spiral/endure/pkg/fsm"
1212
"github.com/spiral/endure/pkg/graph"
13-
"github.com/spiral/endure/pkg/linked_list"
13+
ll "github.com/spiral/endure/pkg/linked_list"
1414
"github.com/spiral/errors"
1515
"go.uber.org/zap"
1616
"go.uber.org/zap/zapcore"
@@ -55,7 +55,7 @@ type Endure struct {
5555
// Dependency graph
5656
graph *graph.Graph
5757
// DLL used as run list to run in order
58-
runList *linked_list.DoublyLinkedList
58+
runList *ll.DoublyLinkedList
5959
// logger
6060
logger *zap.Logger
6161
// OPTIONS
@@ -65,8 +65,11 @@ type Endure struct {
6565
initialInterval time.Duration
6666
stopTimeout time.Duration
6767

68-
deps map[string]interface{}
69-
disabled map[string]bool
68+
// deps is a map with all saved deps
69+
deps map[string]interface{}
70+
// disabled is a map with disabled deps
71+
disabled map[string]bool
72+
// initialized vertices map
7073
initialized map[string]bool
7174

7275
// Graph visualizer
@@ -138,7 +141,7 @@ func NewContainer(logger *zap.Logger, options ...Options) (*Endure, error) {
138141
c.FSM = fsm.NewFSM(fsm.Uninitialized, transitionMap)
139142

140143
c.graph = graph.NewGraph()
141-
c.runList = linked_list.NewDoublyLinkedList()
144+
c.runList = ll.NewDoublyLinkedList()
142145

143146
// Main thread channels
144147
c.handleErrorCh = make(chan *result)
@@ -272,10 +275,6 @@ func (e *Endure) reRegister(vertex interface{}) error {
272275
t := reflect.TypeOf(vertex)
273276
vertexID := removePointerAsterisk(t.String())
274277

275-
if t.Kind() != reflect.Ptr {
276-
return errors.E(op, errors.Register, errors.Errorf("you should pass pointer to the structure instead of value"))
277-
}
278-
279278
err := e.register(vertexID, vertex)
280279
if err != nil {
281280
return errors.E(op, errors.Register, err)
@@ -370,7 +369,7 @@ START:
370369
return errors.E(op, errors.Init, errors.Errorf("graph should contain at least 1 vertex, possibly you forget to invoke registers"))
371370
}
372371

373-
e.runList = linked_list.NewDoublyLinkedList()
372+
e.runList = ll.NewDoublyLinkedList()
374373
for i := len(sorted) - 1; i >= 0; i-- {
375374
e.runList.Push(sorted[i])
376375
}
@@ -382,6 +381,7 @@ START:
382381
head = head.Next
383382
continue
384383
}
384+
385385
// check for disabled, because that can be interface
386386
if _, ok := e.disabled[head.Vertex.ID]; ok {
387387
err = e.removeVertex(head)
@@ -431,10 +431,6 @@ func (e *Endure) Start() (<-chan *Result, error) {
431431

432432
nCopy := e.runList.Head
433433
for nCopy != nil {
434-
if nCopy.Vertex.IsDisabled {
435-
nCopy = nCopy.Next
436-
continue
437-
}
438434
atLeastOne = true
439435
nCopy.Vertex.SetState(fsm.Starting)
440436
err := e.serveInternal(nCopy)
@@ -448,7 +444,7 @@ func (e *Endure) Start() (<-chan *Result, error) {
448444
}
449445
// all vertices disabled
450446
if !atLeastOne {
451-
return nil, errors.E(op, errors.Disabled, errors.Str("all vertices disabled, nothing to serveInternal"))
447+
return nil, errors.E(op, errors.Disabled, errors.Str("all vertices disabled, nothing to run"))
452448
}
453449
return e.userResultsCh, nil
454450
}
@@ -457,20 +453,27 @@ func (e *Endure) Start() (<-chan *Result, error) {
457453
// Do not change this method fn, sync with constants in the beginning of this file
458454
func (e *Endure) Shutdown() error {
459455
e.logger.Info("exiting from the Endure")
456+
if e.runList == nil {
457+
return nil
458+
}
460459
return e.shutdown(e.runList.Head, true)
461460
}
462461

463-
func (e *Endure) removeVertex(head *linked_list.DllNode) error {
462+
func (e *Endure) removeVertex(head *ll.DllNode) error {
464463
const op = errors.Op("endure_disable")
465464
e.logger.Debug("found disabled vertex", zap.String("id", head.Vertex.ID))
466465
// add vertex to the map with disabled vertices
467466
for providesID := range head.Vertex.Provides {
467+
// disable all types which vertex provides as a root
468468
e.disabled[providesID] = true
469469
delete(e.deps, providesID)
470470
}
471+
471472
e.disabled[head.Vertex.ID] = true
473+
472474
// reset run list
473-
e.runList.Reset()
475+
e.runList = nil
476+
474477
// reset graph
475478
e.graph = nil
476479
e.graph = graph.NewGraph()
@@ -485,8 +488,5 @@ func (e *Endure) removeVertex(head *linked_list.DllNode) error {
485488
}
486489
}
487490

488-
e.runList.Remove(head)
489-
head.Vertex = nil
490-
491491
return nil
492492
}

pkg/container/serve.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package endure
33
import (
44
"reflect"
55

6-
"github.com/spiral/endure/pkg/linked_list"
6+
ll "github.com/spiral/endure/pkg/linked_list"
77
"github.com/spiral/endure/pkg/vertex"
88
"github.com/spiral/errors"
99
"go.uber.org/zap"
@@ -39,7 +39,7 @@ func (e *Endure) callServeFn(vrtx *vertex.Vertex, in []reflect.Value) (*result,
3939
}
4040

4141
// serveInternal run calls callServeFn for each node and put the results in the map
42-
func (e *Endure) serveInternal(n *linked_list.DllNode) error {
42+
func (e *Endure) serveInternal(n *ll.DllNode) error {
4343
const op = errors.Op("endure_serve_internal")
4444
// check if type implements serveInternal, if implements, call serveInternal
4545
if reflect.TypeOf(n.Vertex.Iface).Implements(reflect.TypeOf((*Service)(nil)).Elem()) {

pkg/container/stop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66

77
"github.com/spiral/endure/pkg/fsm"
8-
"github.com/spiral/endure/pkg/linked_list"
8+
ll "github.com/spiral/endure/pkg/linked_list"
99
"github.com/spiral/endure/pkg/vertex"
1010
"github.com/spiral/errors"
1111
"go.uber.org/zap"
@@ -46,7 +46,7 @@ func (e *Endure) callStopFn(vrtx *vertex.Vertex, in []reflect.Value) error {
4646

4747
// true -> next
4848
// false -> prev
49-
func (e *Endure) shutdown(n *linked_list.DllNode, traverseNext bool) error {
49+
func (e *Endure) shutdown(n *ll.DllNode, traverseNext bool) error {
5050
const op = errors.Op("endure_shutdown")
5151
numOfVertices := calculateDepth(n, traverseNext)
5252
if numOfVertices == 0 {
@@ -153,7 +153,7 @@ func (e *Endure) shutdown(n *linked_list.DllNode, traverseNext bool) error {
153153
}
154154

155155
// Using to calculate number of Vertices in DLL
156-
func calculateDepth(n *linked_list.DllNode, traverse bool) int {
156+
func calculateDepth(n *ll.DllNode, traverse bool) int {
157157
num := 0
158158
if traverse {
159159
tmp := n

pkg/linked_list/linked_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package linked_list
1+
package linkedlist
22

33
import "github.com/spiral/endure/pkg/vertex"
44

tests/disabled_vertices/plugin5/plugin5.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package plugin5
22

3+
import (
4+
"github.com/spiral/errors"
5+
)
6+
37
type Plugin5 struct {
48
val int
59
}
610

711
func (p5 *Plugin5) Init() error {
812
p5.val++
9-
println(p5.val)
13+
if p5.val > 1 {
14+
return errors.E("should not be more than 1")
15+
}
1016
return nil
1117
}
1218

0 commit comments

Comments
 (0)