@@ -73,6 +73,22 @@ type moduleWrapper struct {
7373 registerErr error
7474}
7575
76+ func (m * moduleWrapper ) AddInitFunc (initFunc func (context.Context ) error ) * moduleWrapper {
77+ parentInit := m .initFunc
78+ if parentInit != nil {
79+ m .initFunc = func (ctx context.Context ) error {
80+ err := parentInit (ctx )
81+ if err != nil {
82+ return err
83+ }
84+ return initFunc (ctx )
85+ }
86+ } else {
87+ m .initFunc = initFunc
88+ }
89+ return m
90+ }
91+
7692func (c * fsClient ) Register (module string , wrapper * moduleWrapper ) FSClient {
7793 if c .err != nil {
7894 return c
@@ -200,23 +216,29 @@ func Custom(init func(ctx context.Context) error, execute func(ctx context.Conte
200216}
201217
202218type FunctionContext struct {
203- c * fsClient
219+ c * fsClient
220+ name string
221+ module string
204222}
205223
206224func (c * FunctionContext ) GetState (ctx context.Context , key string ) ([]byte , error ) {
207- return c .c .rpc .GetState (ctx , key )
225+ return c .c .rpc .GetState (c . warpContext ( ctx ) , key )
208226}
209227
210228func (c * FunctionContext ) PutState (ctx context.Context , key string , value []byte ) error {
211- return c .c .rpc .PutState (ctx , key , value )
229+ return c .c .rpc .PutState (c . warpContext ( ctx ) , key , value )
212230}
213231
214232func (c * FunctionContext ) Write (ctx context.Context , payload []byte ) error {
215- return c .c .rpc .Write (ctx , payload )
233+ return c .c .rpc .Write (c . warpContext ( ctx ) , payload )
216234}
217235
218236func (c * FunctionContext ) Read (ctx context.Context ) ([]byte , error ) {
219- return c .c .rpc .Read (ctx )
237+ return c .c .rpc .Read (c .warpContext (ctx ))
238+ }
239+
240+ func (c * FunctionContext ) GetConfig (ctx context.Context ) (map [string ]string , error ) {
241+ return c .c .rpc .GetConfig (c .warpContext (ctx ))
220242}
221243
222244type funcCtxKey struct {}
@@ -241,15 +263,6 @@ func (c *fsClient) Run() error {
241263 if funcName == "" {
242264 return fmt .Errorf ("%s is not set" , FSFunctionName )
243265 }
244- funcCtx := & FunctionContext {c : c }
245- if c .rpc == nil {
246- rpc , err := newFSRPCClient ()
247- if err != nil {
248- return err
249- }
250- c .rpc = rpc
251- }
252- ctx := c .rpc .GetContext (context .WithValue (context .Background (), funcCtxKey {}, funcCtx ), funcName )
253266 module := os .Getenv (FSModuleName )
254267 if module == "" {
255268 module = DefaultModule
@@ -258,6 +271,15 @@ func (c *fsClient) Run() error {
258271 if ! ok {
259272 return fmt .Errorf ("module %s not found" , module )
260273 }
274+ funcCtx := & FunctionContext {c : c , name : funcName , module : module }
275+ if c .rpc == nil {
276+ rpc , err := newFSRPCClient ()
277+ if err != nil {
278+ return err
279+ }
280+ c .rpc = rpc
281+ }
282+ ctx := funcCtx .warpContext (context .WithValue (context .Background (), funcCtxKey {}, funcCtx ))
261283 m .fsClient = c
262284 err := m .initFunc (ctx )
263285 if err != nil {
0 commit comments