1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

removed procfunc type definition

This commit is contained in:
postmannen 2022-03-31 14:57:30 +02:00
parent ea79bced4d
commit 13fd48843d
2 changed files with 81 additions and 90 deletions

View file

@ -45,12 +45,33 @@ type process struct {
processKind processKind processKind processKind
// methodsAvailable // methodsAvailable
methodsAvailable MethodsAvailable methodsAvailable MethodsAvailable
// Helper or service function that can do some kind of work // procFunc is a function that will be started when a worker process
// for the process. // is started. If a procFunc is registered when creating a new process
// The idea is that this can hold for example the map of the // the procFunc will be started as a go routine when the process is started,
// the hello nodes to limit shared resources in the system as // and stopped when the process is stopped.
// a whole for sharing a map from the *server level. //
procFunc procFunc // A procFunc can be started both for publishing and subscriber processes.
//
// When used with a subscriber process the usecase is most likely to handle
// some kind of state needed for a request type. The handlers themselves
// can not hold state since they are only called once per message received,
// and exits when the message is handled leaving no state behind. With a procfunc
// we can have a process function running at all times tied to the process, and
// this function can be able to hold the state needed in a certain scenario.
//
// With a subscriber handler you generally take the message in the handler and
// pass it on to the procFunc by putting it on the procFuncCh<-, and the
// message can then be read from the procFuncCh inside the procFunc, and we
// can do some further work on it, for example update registry for metrics that
// is needed for that specific request type.
//
// With a publisher process you can attach a static function that will do some
// work to a request type, and publish the result.
//
// procFunc's can also be used to wrap in other types which we want to
// work with. An example can be handling of metrics which the message
// have no notion of, but a procFunc can have that wrapped in from when it was constructed.
procFunc func(ctx context.Context, procFuncCh chan Message) error
// The channel to send a messages to the procFunc go routine. // The channel to send a messages to the procFunc go routine.
// This is typically used within the methodHandler for so we // This is typically used within the methodHandler for so we
// can pass messages between the procFunc and the handler. // can pass messages between the procFunc and the handler.
@ -110,34 +131,6 @@ func newProcess(ctx context.Context, metrics *metrics, natsConn *nats.Conn, proc
return proc return proc
} }
// procFunc is a function that will be started when a worker process
// is started. If a procFunc is registered when creating a new process
// the procFunc will be started as a go routine when the process is started,
// and stopped when the process is stopped.
//
// A procFunc can be started both for publishing and subscriber processes.
//
// When used with a subscriber process the usecase is most likely to handle
// some kind of state needed for a request type. The handlers themselves
// can not hold state since they are only called once per message received,
// and exits when the message is handled leaving no state behind. With a procfunc
// we can have a process function running at all times tied to the process, and
// this function can be able to hold the state needed in a certain scenario.
//
// With a subscriber handler you generally take the message in the handler and
// pass it on to the procFunc by putting it on the procFuncCh<-, and the
// message can then be read from the procFuncCh inside the procFunc, and we
// can do some further work on it, for example update registry for metrics that
// is needed for that specific request type.
//
// With a publisher process you can attach a static function that will do some
// work to a request type, and publish the result.
//
// procFunc's can also be used to wrap in other types which we want to
// work with. An example can be handling of metrics which the message
// have no notion of, but a procFunc can have that wrapped in from when it was constructed.
type procFunc func(ctx context.Context, procFuncCh chan Message) error
// The purpose of this function is to check if we should start a // The purpose of this function is to check if we should start a
// publisher or subscriber process, where a process is a go routine // publisher or subscriber process, where a process is a go routine
// that will handle either sending or receiving messages on one // that will handle either sending or receiving messages on one

View file

@ -254,8 +254,7 @@ func (s startup) pubREQHello(p process) {
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, processKindPublisher, nil, s.Signatures) proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, processKindPublisher, nil, s.Signatures)
// Define the procFunc to be used for the process. // Define the procFunc to be used for the process.
proc.procFunc = procFunc( proc.procFunc = func(ctx context.Context, procFuncCh chan Message) error {
func(ctx context.Context, procFuncCh chan Message) error {
ticker := time.NewTicker(time.Second * time.Duration(p.configuration.StartPubREQHello)) ticker := time.NewTicker(time.Second * time.Duration(p.configuration.StartPubREQHello))
for { for {
@ -289,7 +288,7 @@ func (s startup) pubREQHello(p process) {
return nil return nil
} }
} }
}) }
go proc.spawnWorker(p.processes, p.natsConn) go proc.spawnWorker(p.processes, p.natsConn)
} }
@ -351,8 +350,7 @@ func (s startup) subREQHello(p process) {
// a handler are not able to hold state, and we need to hold the state // a handler are not able to hold state, and we need to hold the state
// of the nodes we've received hello's from in the sayHelloNodes map, // of the nodes we've received hello's from in the sayHelloNodes map,
// which is the information we pass along to generate metrics. // which is the information we pass along to generate metrics.
proc.procFunc = procFunc( proc.procFunc = func(ctx context.Context, procFuncCh chan Message) error {
func(ctx context.Context, procFuncCh chan Message) error {
sayHelloNodes := make(map[Node]struct{}) sayHelloNodes := make(map[Node]struct{})
for { for {
@ -376,7 +374,7 @@ func (s startup) subREQHello(p process) {
s.metrics.promHelloNodesContactLast.With(prometheus.Labels{"nodeName": string(m.FromNode)}).SetToCurrentTime() s.metrics.promHelloNodesContactLast.With(prometheus.Labels{"nodeName": string(m.FromNode)}).SetToCurrentTime()
} }
}) }
go proc.spawnWorker(p.processes, p.natsConn) go proc.spawnWorker(p.processes, p.natsConn)
} }