Files
gocryptotrader/dispatch/dispatch_types.go
Ryan O'Hara-Reid db317a2447 Engine: Dispatch service (#346)
* Added dispatch service

* Added orderbook streaming capabilities

* Assigned correct orderbook.base exchange name

* Fixed Requested niterinos
Add in cli orderbook QA tool to gctcli
Add exchange orderbook streaming

* Add ticker streaming support through dispatch package

* Added in some more info on error returns for orderbook.go

* fix linter issues

* Fix some issues

* Update

* Fix requested

* move dispatch out of exchanges folder to its own independant folder

* Fix requested

* change orderbook string to tickers

* Limit orderbooks to 50 and made dispatch system more stateless in operation

* lower cases for update/retrieve/sub exchange name

* Adds in asset validation and lower case conversion on cli

* Remove comment

* Moved timer to a higher scope so its not constantly initialised just reset per instance and removed returning unused channel on error

* Rm unused release function in dispatch.go
Reset timer and bleed buffered timer chan if needed in dispatch.go
Added in ticker.Stop() and timer.Stop() functions for worker routine return in dispatch.go
Index aggregated bid and ask functions for orderbook.go
Added in dummy slice for wsorderbook_test.go

* Moved drain to above Reset so potential race would not occur in dispatch.go
Fix various linter issues dispatch.go

* Fix some issues

* change to start/stop service, added in service state change via cli, updated logger

* fix requested

* Add worker amount init spawning

* fix linter issues

* Fix more linter issues

* More fixes

* Fix race issue on releasing pipe channel on a close after shutting down dispatcher system

* Moved all types to dispatch_types.go && remove panic

* Moved types into serperate file && improve test coverage

* RM unnecessary select case for draining channel && fixed error string

* Added orderbook_types file and improved code coverage

* gofmt file

* reinstated select cases on drain because I am silly

* Remove error for drop worker

* Added more test cases

* not checking error issue fix

* remove func causing race in test, this has required protection via an exported function

* set Gemini websocket orderbook exchange name
2019-10-03 09:47:37 +10:00

89 lines
2.4 KiB
Go

package dispatch
import (
"sync"
"time"
"github.com/gofrs/uuid"
)
const (
// DefaultJobBuffer defines a maxiumum amount of jobs allowed in channel
DefaultJobBuffer = 100
// DefaultMaxWorkers is the package default worker ceiling amount
DefaultMaxWorkers = 10
// DefaultHandshakeTimeout defines a workers max length of time to wait on a
// an unbuffered channel for a receiver before moving on to next route
DefaultHandshakeTimeout = 200 * time.Nanosecond
errNotInitialised = "dispatcher not initialised"
errAlreadyStarted = "dispatcher already started"
errCannotShutdown = "dispatcher cannot shutdown, already stopped"
errShutdownRoutines = "dispatcher did not shutdown properly, routines failed to close"
)
// dispatcher is our main in memory instance with a stop/start mtx below
var dispatcher *Dispatcher
var mtx sync.Mutex
// Dispatcher defines an internal subsystem communication/change state publisher
type Dispatcher struct {
// routes refers to a subystem uuid ticket map with associated publish
// channels, a relayer will be given a unique id through its job channel,
// then publish the data across the full registered channels for that uuid.
// See relayer() method below.
routes map[uuid.UUID][]chan interface{}
// rMtx protects the routes variable ensuring acceptable read/write access
rMtx sync.RWMutex
// Persistent buffered job queue for relayers
jobs chan *job
// Dynamic channel pool; returns an unbuffered channel for routes map
outbound sync.Pool
// MaxWorkers defines max worker ceiling
maxWorkers int64
// Atomic values -----------------------
// Worker counter
count int64
// Dispatch status
running uint32
// Unbufferd shutdown chan, sync wg for ensuring concurrency when only
// dropping a single relayer routine
shutdown chan *sync.WaitGroup
// Relayer shutdown tracking
wg sync.WaitGroup
}
// job defines a relaying job associated with a ticket which allows routing to
// routines that require specific data
type job struct {
Data interface{}
ID uuid.UUID
}
// Mux defines a new multiplexor for the dispatch system, these a generated
// per subsystem
type Mux struct {
// Reference to the main running dispatch service
d *Dispatcher
sync.RWMutex
}
// Pipe defines an outbound object to the desired routine
type Pipe struct {
// Channel to get all our lovely informations
C chan interface{}
// ID to tracked system
id uuid.UUID
// Reference to multiplexor
m *Mux
}