debug server

This commit is contained in:
Sergey Cherepanov 2023-06-08 13:27:01 +02:00
parent 33cbdd06a6
commit f9cb0c2dbb
No known key found for this signature in database
GPG Key ID: 87F8EDE8FBDF637C
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package debugserver
type configGetter interface {
GetDebugServer() Config
}
type Config struct {
ListenAddr string `yaml:"listenAddr"`
}

View File

@ -0,0 +1,63 @@
package debugserver
import (
"context"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/net/rpc"
"net"
"storj.io/drpc"
"storj.io/drpc/drpcmanager"
"storj.io/drpc/drpcmux"
"storj.io/drpc/drpcserver"
"storj.io/drpc/drpcstream"
"storj.io/drpc/drpcwire"
)
const CName = "net.rpc.debugserver"
type DebugServer interface {
app.ComponentRunnable
drpc.Mux
}
type debugServer struct {
drpcServer *drpcserver.Server
*drpcmux.Mux
drpcConf rpc.Config
config Config
runCtx context.Context
runCtxCancel context.CancelFunc
}
func (d *debugServer) Init(a *app.App) (err error) {
d.drpcConf = a.MustComponent("config").(rpc.ConfigGetter).GetDrpc()
d.config = a.MustComponent("config").(configGetter).GetDebugServer()
d.Mux = drpcmux.New()
bufSize := d.drpcConf.Stream.MaxMsgSizeMb * (1 << 20)
d.drpcServer = drpcserver.NewWithOptions(d, drpcserver.Options{Manager: drpcmanager.Options{
Reader: drpcwire.ReaderOptions{MaximumBufferSize: bufSize},
Stream: drpcstream.Options{MaximumBufferSize: bufSize},
}})
return nil
}
func (d *debugServer) Name() (name string) {
return CName
}
func (d *debugServer) Run(ctx context.Context) (err error) {
lis, err := net.Listen("tpc", d.config.ListenAddr)
if err != nil {
return
}
d.runCtx, d.runCtxCancel = context.WithCancel(context.Background())
go d.drpcServer.Serve(d.runCtx, lis)
return
}
func (d *debugServer) Close(ctx context.Context) (err error) {
if d.runCtx != nil {
d.runCtxCancel()
}
return nil
}