lang: interfaces, funcs: Port Func API to new Stream signature

This removes the `Close() error` and replaces it with a more modern
Stream API that takes a context. This removes boilerplate and makes
integration with concurrent code easier. The only downside is that there
isn't an explicit cleanup step, but only one function was even using
that and it was possible to switch it to a defer in Stream.

This also renames the functions from polyfunc to just func which we
determine by API not naming.
This commit is contained in:
James Shubin
2023-05-28 16:20:42 -04:00
parent 6a06f7b2ea
commit b134c4b778
41 changed files with 276 additions and 540 deletions

View File

@@ -18,6 +18,7 @@
package coresys
import (
"context"
"time"
"github.com/purpleidea/mgmt/lang/funcs/facts"
@@ -37,8 +38,7 @@ func init() {
// UptimeFact is a fact which returns the current uptime of your system.
type UptimeFact struct {
init *facts.Init
closeChan chan struct{}
init *facts.Init
}
// String returns a simple name for this fact. This is needed so this struct can
@@ -57,12 +57,11 @@ func (obj *UptimeFact) Info() *facts.Info {
// Init runs some startup code for this fact.
func (obj *UptimeFact) Init(init *facts.Init) error {
obj.init = init
obj.closeChan = make(chan struct{})
return nil
}
// Stream returns the changing values that this fact has over time.
func (obj *UptimeFact) Stream() error {
func (obj *UptimeFact) Stream(ctx context.Context) error {
defer close(obj.init.Output)
ticker := time.NewTicker(time.Duration(1) * time.Second)
@@ -75,7 +74,7 @@ func (obj *UptimeFact) Stream() error {
startChan = nil
case <-ticker.C:
// send
case <-obj.closeChan:
case <-ctx.Done():
return nil
}
@@ -87,14 +86,8 @@ func (obj *UptimeFact) Stream() error {
select {
case obj.init.Output <- &types.IntValue{V: uptime}:
// send
case <-obj.closeChan:
case <-ctx.Done():
return nil
}
}
}
// Close runs some shutdown code for this fact and turns off the stream.
func (obj *UptimeFact) Close() error {
close(obj.closeChan)
return nil
}