-
Notifications
You must be signed in to change notification settings - Fork 45
Closed
Labels
Description
Currently there is not way of accesing context that was used to run rsocket server.
Motivation
Example 1 - I could inititate logger with some options that I attach to the context that is used to run rsocket server. Then I would acces the same context in my AbstractSocket function so I could use the same logger.
Example 2 - If my abstract socket has a long running task, having cancelable context available means that I could gracefully stop the execution if the server was requested to shutdown.
Desired solution
First thing that comes to mind is to change the signature of the OptAbstractSocket to func(context.Context, *socket.AbstractRSocket) thus having context available on every request.
Considered alternatives
Use the main context when creating AbstractSocket.
Example code
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/rsocket/rsocket-go"
"github.com/rsocket/rsocket-go/payload"
"github.com/rsocket/rsocket-go/rx/mono"
)
func main() {
go server()
time.Sleep(time.Second)
client()
}
func server() {
mainCtx := context.WithValue(context.Background(), "KEY", "VALUE")
err := rsocket.Receive().
Acceptor(func(setup payload.SetupPayload, sendingSocket rsocket.CloseableRSocket) (rsocket.RSocket, error) {
return rsocket.NewAbstractSocket(
rsocket.RequestResponse(func(msg payload.Payload) mono.Mono {
// no context available here
// return mono.Just(msg)
return mono.Create(func(ctx context.Context, sink mono.Sink) {
// ctx is not the same context
fmt.Printf("key KEY value: %v\n", ctx.Value("KEY"))
sink.Success(msg)
})
}),
), nil
}).
Transport(rsocket.TCPServer().SetAddr(":7878").Build()).
Serve(mainCtx)
log.Fatalln(err)
}
func client() {
cli, err := rsocket.Connect().
SetupPayload(payload.NewString("Hello", "World")).
Transport(rsocket.TCPClient().SetHostAndPort("127.0.0.1", 7878).Build()).
Start(context.Background())
if err != nil {
panic(err)
}
defer cli.Close()
// Send request
result, err := cli.RequestResponse(payload.NewString("你好", "世界")).Block(context.Background())
if err != nil {
panic(err)
}
log.Println("response:", result.DataUTF8())
}