Skip to content

Commit 61c6dfc

Browse files
author
Valery Piashchynski
authored
Merge pull request #30 from spiral/accept_interface_into_providers
Accept "Name" interface in the Providers
2 parents 0d92166 + a6d033a commit 61c6dfc

File tree

40 files changed

+607
-265
lines changed

40 files changed

+607
-265
lines changed

.golangci.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
linters:
2+
# please, do not use `enable-all`: it's deprecated and will be removed soon.
3+
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
4+
disable-all: true
5+
enable:
6+
- bodyclose
7+
- depguard
8+
- dogsled
9+
- dupl
10+
- gochecknoinits
11+
- goconst
12+
- gocritic
13+
- gocyclo
14+
- gofmt
15+
- goimports
16+
# - golint
17+
- goprintffuncname
18+
# - gosec
19+
# - gosimple
20+
- govet
21+
- ineffassign
22+
- interfacer
23+
- misspell
24+
- nakedret
25+
- nolintlint
26+
- rowserrcheck
27+
- scopelint
28+
- staticcheck
29+
- structcheck
30+
- stylecheck
31+
- typecheck
32+
- unconvert
33+
- unparam
34+
# - unused
35+
- varcheck
36+
- whitespace
37+
38+
# don't enable:
39+
# - asciicheck
40+
# - gochecknoglobals
41+
# - gocognit
42+
# - godot
43+
# - godox
44+
# - goerr113
45+
# - maligned
46+
# - nestif
47+
# - prealloc
48+
# - testpackage
49+
# - wsl

calculate_deps.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *Cascade) addEdges() error {
7070

7171
/* Add the dependencies (if) which this vertex needs to init
7272
Information we know at this step is:
73-
1. VertexId
73+
1. vertexID
7474
2. Vertex structure value (interface)
7575
3. Provided type
7676
4. Provided type String name
@@ -171,10 +171,7 @@ func (c *Cascade) addDependersDeps(vertexID string, vertex interface{}) error {
171171

172172
func (c *Cascade) addInitDeps(vertexID string, initMethod reflect.Method) error {
173173
// S2 init args
174-
initArgs, err := functionParameters(initMethod)
175-
if err != nil {
176-
return err
177-
}
174+
initArgs := functionParameters(initMethod)
178175

179176
// iterate over all function parameters
180177
for _, initArg := range initArgs {
@@ -196,15 +193,15 @@ func (c *Cascade) addInitDeps(vertexID string, initMethod reflect.Method) error
196193
}
197194
tmpIsRef := isReference(initArg)
198195
tmpValue := reflect.ValueOf(c.graph.Vertices[i].Iface)
199-
err = c.graph.Vertices[i].AddProvider(removePointerAsterisk(initArg.String()), tmpValue, tmpIsRef, initArg.Kind())
196+
err := c.graph.Vertices[i].AddProvider(removePointerAsterisk(initArg.String()), tmpValue, tmpIsRef, initArg.Kind())
200197
if err != nil {
201198
return err
202199
}
203200
}
204201
}
205202
}
206203

207-
err = c.graph.AddDep(vertexID, removePointerAsterisk(initArg.String()), structures.Init, isReference(initArg), initArg.Kind())
204+
err := c.graph.AddDep(vertexID, removePointerAsterisk(initArg.String()), structures.Init, isReference(initArg), initArg.Kind())
208205
if err != nil {
209206
return err
210207
}

cascade.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package cascade
33
import (
44
"errors"
55
"net/http"
6+
7+
// pprof enabled in debug mode
68
_ "net/http/pprof"
9+
710
"reflect"
811
"sync"
912
"time"
@@ -187,12 +190,12 @@ func (c *Cascade) Register(vertex interface{}) error {
187190

188191
ok := t.Implements(reflect.TypeOf((*Service)(nil)).Elem())
189192
if !ok {
190-
return typeNotImplementError
193+
return errTypeNotImplementError
191194
}
192195

193196
/* Depender the type
194197
Information we know at this step is:
195-
1. VertexId
198+
1. vertexID
196199
2. Vertex structure value (interface)
197200
And we fill vertex with this information
198201
*/
@@ -203,7 +206,7 @@ func (c *Cascade) Register(vertex interface{}) error {
203206
order++
204207
/* Add the types, which (if) current vertex provides
205208
Information we know at this step is:
206-
1. VertexId
209+
1. vertexID
207210
2. Vertex structure value (interface)
208211
3. Provided type
209212
4. Provided type String name
@@ -254,7 +257,7 @@ func (c *Cascade) Init() error {
254257
return nil
255258
}
256259

257-
func (c *Cascade) Serve() (error, <-chan *Result) {
260+
func (c *Cascade) Serve() (<-chan *Result, error) {
258261
c.rwMutex.Lock()
259262
defer c.rwMutex.Unlock()
260263
c.startMainThread()
@@ -264,8 +267,8 @@ func (c *Cascade) Serve() (error, <-chan *Result) {
264267
for nCopy != nil {
265268
err := c.configure(nCopy)
266269
if err != nil {
267-
c.logger.Error("backoff failed", zap.String("vertex id", nCopy.Vertex.Id), zap.Error(err))
268-
return err, nil
270+
c.logger.Error("backoff failed", zap.String("vertex id", nCopy.Vertex.ID), zap.Error(err))
271+
return nil, err
269272
}
270273

271274
nCopy = nCopy.Next
@@ -275,11 +278,11 @@ func (c *Cascade) Serve() (error, <-chan *Result) {
275278
for nCopy != nil {
276279
err := c.serve(nCopy)
277280
if err != nil {
278-
return err, nil
281+
return nil, err
279282
}
280283
nCopy = nCopy.Next
281284
}
282-
return nil, c.userResultsCh
285+
return c.userResultsCh, nil
283286
}
284287

285288
func (c *Cascade) Stop() error {
@@ -319,18 +322,18 @@ func (c *Cascade) startMainThread() {
319322
return
320323
}
321324

322-
c.logger.Debug("processing error in the main thread", zap.String("vertex id", res.vertexId))
325+
c.logger.Debug("processing error in the main thread", zap.String("vertex id", res.vertexID))
323326
if c.checkLeafErrorTime(res) {
324-
c.logger.Debug("error processing skipped because vertex already restartedTime by the root", zap.String("vertex id", res.vertexId))
327+
c.logger.Debug("error processing skipped because vertex already restartedTime by the root", zap.String("vertex id", res.vertexID))
325328
c.sendResultToUser(res)
326329
c.rwMutex.Unlock()
327330
continue
328331
}
329332

330333
// get vertex from the graph
331-
vertex := c.graph.GetVertex(res.vertexId)
334+
vertex := c.graph.GetVertex(res.vertexID)
332335
if vertex == nil {
333-
c.logger.Error("failed to get vertex from the graph, vertex is nil", zap.String("vertex id from the handleErrorCh channel", res.vertexId))
336+
c.logger.Error("failed to get vertex from the graph, vertex is nil", zap.String("vertex id from the handleErrorCh channel", res.vertexID))
334337
c.userResultsCh <- &Result{
335338
Error: FailedToGetTheVertex,
336339
VertexID: "",
@@ -341,12 +344,12 @@ func (c *Cascade) startMainThread() {
341344

342345
// reset vertex and dependencies to the initial state
343346
// NumOfDeps and Visited/Visiting
344-
vertices := c.resetVertices(vertex)
347+
vertices := c.graph.Reset(vertex)
345348

346349
// Topologically sort the graph
347350
sorted := structures.TopologicalSort(vertices)
348351
if sorted == nil {
349-
c.logger.Error("sorted list should not be nil", zap.String("vertex id from the handleErrorCh channel", res.vertexId))
352+
c.logger.Error("sorted list should not be nil", zap.String("vertex id from the handleErrorCh channel", res.vertexID))
350353
c.userResultsCh <- &Result{
351354
Error: FailedToSortTheGraph,
352355
VertexID: "",
@@ -377,10 +380,10 @@ func (c *Cascade) startMainThread() {
377380
for headCopy != nil {
378381
berr := backoff.Retry(c.backoffInit(headCopy.Vertex), b)
379382
if berr != nil {
380-
c.logger.Error("backoff failed", zap.String("vertex id", headCopy.Vertex.Id), zap.Error(berr))
383+
c.logger.Error("backoff failed", zap.String("vertex id", headCopy.Vertex.ID), zap.Error(berr))
381384
c.userResultsCh <- &Result{
382385
Error: ErrorDuringInit,
383-
VertexID: headCopy.Vertex.Id,
386+
VertexID: headCopy.Vertex.ID,
384387
}
385388
c.rwMutex.Unlock()
386389
return
@@ -396,9 +399,9 @@ func (c *Cascade) startMainThread() {
396399
if berr != nil {
397400
c.userResultsCh <- &Result{
398401
Error: ErrorDuringInit,
399-
VertexID: headCopy.Vertex.Id,
402+
VertexID: headCopy.Vertex.ID,
400403
}
401-
c.logger.Error("backoff failed", zap.String("vertex id", headCopy.Vertex.Id), zap.Error(berr))
404+
c.logger.Error("backoff failed", zap.String("vertex id", headCopy.Vertex.ID), zap.Error(berr))
402405
c.rwMutex.Unlock()
403406
return
404407
}
@@ -413,9 +416,9 @@ func (c *Cascade) startMainThread() {
413416
if err != nil {
414417
c.userResultsCh <- &Result{
415418
Error: ErrorDuringServe,
416-
VertexID: headCopy.Vertex.Id,
419+
VertexID: headCopy.Vertex.ID,
417420
}
418-
c.logger.Error("fatal error during the serve in the main thread", zap.String("vertex id", headCopy.Vertex.Id), zap.Error(err))
421+
c.logger.Error("fatal error during the serve in the main thread", zap.String("vertex id", headCopy.Vertex.ID), zap.Error(err))
419422
c.rwMutex.Unlock()
420423
return
421424
}

container.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,37 @@ type result struct {
2626
// error from the channel
2727
err error
2828
// unique vertex id
29-
vertexId string
29+
vertexID string
3030
// signal to the vertex goroutine to exit
3131
exit chan struct{}
3232
// internal exit, used to notify main thread to release resources
3333
internalExit bool
3434
}
3535

3636
type (
37-
// TODO namings
37+
// used to gracefully stop and configure the plugins
3838
Graceful interface {
3939
// Configure is used when we need to make preparation and wait for all services till Serve
4040
Configure() error
4141
// Close frees resources allocated by the service
4242
Close() error
4343
}
44+
// this is the main service interface with should implement every plugin
4445
Service interface {
4546
// Serve
4647
Serve() chan error
4748
// Stop
4849
Stop() error
4950
}
5051

51-
Container interface {
52-
Serve() (error, <-chan *Result)
52+
// Name of the service
53+
Named interface {
54+
Name() string
55+
}
56+
57+
// internal container interface
58+
container interface {
59+
Serve() (<-chan *Result, error)
5360
Stop() error
5461
restart() error
5562
Register(service interface{}) error

errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ var ErrorDuringServe = Error{
4343
Stack: debug.Stack(),
4444
}
4545

46-
var typeNotImplementError = errors.New("type should implement Service interface")
47-
var vertexAlreadyExists = func(name string) error { return fmt.Errorf("vertex `%s` already exists", name) }
48-
var unknownErrorOccurred = errors.New("unknown error occurred during the function call")
46+
var errTypeNotImplementError = errors.New("type should implement Service interface")
47+
var errVertexAlreadyExists = func(name string) error { return fmt.Errorf("vertex `%s` already exists", name) }
48+
var errUnknownErrorOccurred = errors.New("unknown error occurred during the function call")

examples/db_http_logger/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require (
88
github.com/rs/cors v1.7.0
99
github.com/spiral/cascade v1.0.0-beta4
1010
go.etcd.io/bbolt v1.3.5
11-
)
11+
)

examples/db_http_logger/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
panic(err)
4444
}
4545

46-
err, errCh := container.Serve()
46+
errCh, err := container.Serve()
4747
if err != nil {
4848
panic(err)
4949
}

examples/db_http_logger/modules/db/db_layer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func (db *DB) Stop() error {
5050
return nil
5151
}
5252

53+
func (db *DB) Name() string {
54+
return "super DATABASE service"
55+
}
56+
5357
/////////////// DB LAYER /////////////////
5458

5559

examples/db_http_logger/modules/gzip/gzip.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ func (gz *Gzip) Middleware(f http.Handler) http.HandlerFunc {
2929
gziphandler.GzipHandler(f).ServeHTTP(w, r)
3030
}
3131
}
32+
33+
func (gz *Gzip) Name() string {
34+
return "super Gzip middleware"
35+
}

examples/db_http_logger/modules/headers/headers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ func (h *Headers) Middleware(f http.Handler) http.HandlerFunc {
3333
return func(w http.ResponseWriter, r *http.Request) {
3434
f.ServeHTTP(w, r)
3535
}
36+
}
37+
38+
func (h *Headers) Name() string {
39+
return "super Headers middleware"
3640
}

0 commit comments

Comments
 (0)