Thanks for the quick reply!

What I'm trying to do is put some things into the context so that they're
available in downstream calls, perhaps in methods with pointer receivers to
the function struct (MyFunc) but also perhaps in methods that are further
downstream that don't have access to MyFunc. If I'm understanding
correctly, your proposal would work for the former but not the latter.

An example would be to put a configured Logger into the context via a
WithLogger method (logging package - knative.dev/pkg/logging - pkg.go.dev
<https://pkg.go.dev/knative.dev/pkg/logging#WithLogger>) and then pull it
out downstream via FromContext (logging package - knative.dev/pkg/logging -
pkg.go.dev <https://pkg.go.dev/knative.dev/pkg/logging#FromContext>).




On Wed, Feb 16, 2022 at 5:50 PM Seth Wiesman <sjwies...@gmail.com> wrote:

> Hi Galen,
>
> No, that is not currently supported, the current idiomatic way would be to
> pass those values to the struct implementing the Statefun interface.
>
>
> type MyFunc struct { someRuntimeInfo string } func (m *MyFunc) Invoke(ctx
> statefun.Context, message statefun.Message) error { } func main() { builder
> := statefun.StatefulFunctionsBuilder()
> f := MyFunc { someRuntimeInfo: "runtime-provided" } builder.WithSpec
> (statefun.StatefulFunctionSpec{ FunctionType: statefun.TypeNameFrom(
> "example/my-func"), Function: f })
> http.Handle("/statefun", builder.AsHandler())
> _ = http.ListenAndServe(":8000", nil) }
>
> Would this work for you? Or what is the context (pun intended) you are
> looking for?
>
> Seth
>
> On Wed, Feb 16, 2022 at 4:35 PM Galen Warren <ga...@cvillewarrens.com>
> wrote:
>
> > When stateful functions are invoked, they are passed an instance of
> > statefun.Context, which wraps the context.Context received by the HTTP
> > request. Is there any way to customize that context.Context to, say, hold
> > custom values, using ctx.WithValue()? I don't see a way but I wanted to
> > ask.
> >
> > If not, would you be interested in a PR to add this functionality? A
> simple
> > way might be to add a property to StatefulFunctionSpec, say:
> >
> > TransformContext func(ctx context.Context) context.Context
> >
> > ... that, if supplied, would be called to create a customized context
> that
> > would be used downstream?
> >
> > Thanks.
> >
>

Reply via email to