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 core // TODO: should this be in its own individual package?
import (
"context"
"crypto/rand"
"fmt"
"math/big"
@@ -56,8 +57,6 @@ type Random1Func struct {
init *interfaces.Init
finished bool // did we send the random string?
closeChan chan struct{}
}
// String returns a simple name for this function. This is needed so this struct
@@ -119,12 +118,11 @@ func generate(length uint16) (string, error) {
// Init runs some startup code for this function.
func (obj *Random1Func) Init(init *interfaces.Init) error {
obj.init = init
obj.closeChan = make(chan struct{})
return nil
}
// Stream returns the single value that was generated and then closes.
func (obj *Random1Func) Stream() error {
func (obj *Random1Func) Stream(ctx context.Context) error {
defer close(obj.init.Output) // the sender closes
var result string
for {
@@ -153,7 +151,7 @@ func (obj *Random1Func) Stream() error {
return err // no errwrap needed b/c helper func
}
case <-obj.closeChan:
case <-ctx.Done():
return nil
}
@@ -164,14 +162,8 @@ func (obj *Random1Func) Stream() error {
// we only send one value, then wait for input to close
obj.finished = true
case <-obj.closeChan:
case <-ctx.Done():
return nil
}
}
}
// Close runs some shutdown code for this function and turns off the stream.
func (obj *Random1Func) Close() error {
close(obj.closeChan)
return nil
}