Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed May 24, 2024
1 parent 8e82dd0 commit 8747134
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion example/ex-01-rpc/bench.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tcpkali --ws -c 100 -m '{"hdr":{"cmd":"echoRequest"},"payload":{"randomID": 1234}}' -r 1k 127.0.0.1:80 -T 30
tcpkali --ws -c 1000 -m '{"hdr":{"cmd":"echoRequest"},"payload":{"randomID": 1234}}' -r 10 127.0.0.1:80 -T 30
16 changes: 10 additions & 6 deletions example/ex-01-rpc/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"runtime"

"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/common"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
"github.com/clubpay/ronykit/std/gateways/fastws"
_ "github.com/clubpay/ronykit/std/gateways/fastws"
)

Expand All @@ -28,18 +30,20 @@ func main() {
},
),
kit.WithGateway(
//fastws.MustNew(
// fastws.Listen("tcp4://0.0.0.0:80"),
// fastws.WithPredicateKey("cmd"),
//),
fastws.MustNew(
fastws.WithLogger(common.NewStdLogger()),
fastws.Listen("tcp4://0.0.0.0:80"),
fastws.WithPredicateKey("cmd"),
),
// We can use fastws Gateway if we have millions of connections which are not very active.
// fastws ONLY supports websocket, and if you need to handle websocket and http, you should
// use fasthttp instead.
//
fasthttp.MustNew(
fasthttp.WithWebsocketEndpoint("/ws"),
fasthttp.WithLogger(common.NewStdLogger()),
fasthttp.WithWebsocketEndpoint("/"),
fasthttp.WithPredicateKey("cmd"),
fasthttp.Listen(":80"),
fasthttp.Listen(":81"),
),
),
kit.WithServiceDesc(sampleService),
Expand Down
5 changes: 3 additions & 2 deletions example/ex-01-rpc/cmd/server/service.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
"net/http"

"github.com/clubpay/ronykit/example/ex-01-rpc/dto"
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
"github.com/clubpay/ronykit/kit/utils"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
"github.com/clubpay/ronykit/std/gateways/fastws"
)
Expand All @@ -27,13 +27,14 @@ func echoHandler(ctx *kit.Context) {
//nolint:forcetypeassert
req := ctx.In().GetMsg().(*dto.EchoRequest)

fmt.Println("got request: ", req)
// fmt.Println("got request: ", req)
ctx.In().Reply().
SetHdr("cmd", ctx.In().GetHdr("cmd")).
SetMsg(
&dto.EchoResponse{
RandomID: req.RandomID,
Ok: req.Ok,
Ts: utils.NanoTime(),
},
).Send()
}
1 change: 1 addition & 0 deletions example/ex-01-rpc/dto/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type EchoRequest struct {
type EchoResponse struct {
RandomID int64 `json:"randomID"`
Ok bool `json:"ok"`
Ts int64 `json:"ts"`
}

type SumRequest struct {
Expand Down
16 changes: 8 additions & 8 deletions kit/common/nop_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package common

import "github.com/clubpay/ronykit/kit"

type NOPLogger struct{}
type nopLogger struct{}

func NewNopLogger() NOPLogger {
return NOPLogger{}
func NewNopLogger() kit.Logger {
return nopLogger{}
}

var _ kit.Logger = (*NOPLogger)(nil)
var _ kit.Logger = (*nopLogger)(nil)

func (n NOPLogger) Debug(_ ...any) {}
func (n nopLogger) Debug(_ ...any) {}

func (n NOPLogger) Debugf(_ string, _ ...any) {}
func (n nopLogger) Debugf(_ string, _ ...any) {}

func (n NOPLogger) Error(_ ...any) {}
func (n nopLogger) Error(_ ...any) {}

func (n NOPLogger) Errorf(_ string, _ ...any) {}
func (n nopLogger) Errorf(_ string, _ ...any) {}
6 changes: 5 additions & 1 deletion testenv/utils.go → kit/common/std_logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testenv
package common

import (
"fmt"
Expand All @@ -8,6 +8,10 @@ import (

type stdLogger struct{}

func NewStdLogger() kit.Logger {
return stdLogger{}
}

var _ kit.Logger = (*stdLogger)(nil)

func (s stdLogger) Debugf(format string, args ...any) {
Expand Down
8 changes: 5 additions & 3 deletions testenv/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"ronykit/testenv/services"

"github.com/clubpay/ronykit/kit/common"

"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/stub"
"github.com/clubpay/ronykit/kit/utils"
Expand Down Expand Up @@ -50,7 +52,7 @@ func invokeEdgeServerWithRedis(_ string, port int, desc ...kit.ServiceDescriptor
rediscluster.WithRedisClient(utils.Must(getRedis())),
),
),
kit.WithLogger(&stdLogger{}),
kit.WithLogger(common.NewStdLogger()),
kit.WithErrorHandler(
func(ctx *kit.Context, err error) {
fmt.Println("EdgeError: ", err)
Expand Down Expand Up @@ -90,11 +92,11 @@ func invokeEdgeServerWithP2P(_ string, port int, desc ...kit.ServiceDescriptor)
kit.WithCluster(
p2pcluster.New(
"testCluster",
p2pcluster.WithLogger(&stdLogger{}),
p2pcluster.WithLogger(common.NewStdLogger()),
p2pcluster.WithBroadcastInterval(time.Second),
),
),
kit.WithLogger(&stdLogger{}),
kit.WithLogger(common.NewStdLogger()),
kit.WithErrorHandler(
func(ctx *kit.Context, err error) {
fmt.Println("EdgeError: ", err)
Expand Down
6 changes: 4 additions & 2 deletions testenv/fastws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"ronykit/testenv/services"

"github.com/clubpay/ronykit/kit/common"

"github.com/clubpay/ronykit/kit/utils"

"github.com/clubpay/ronykit/std/gateways/fastws"
Expand Down Expand Up @@ -38,7 +40,7 @@ func invokeEdgeServerWithFastWS(port int, desc ...kit.ServiceDescriptor) fx.Opti
func(lc fx.Lifecycle) {
edge := kit.NewServer(
kit.ReusePort(true),
kit.WithLogger(&stdLogger{}),
kit.WithLogger(common.NewStdLogger()),
kit.WithErrorHandler(
func(ctx *kit.Context, err error) {
fmt.Println("EdgeError: ", err)
Expand All @@ -48,7 +50,7 @@ func invokeEdgeServerWithFastWS(port int, desc ...kit.ServiceDescriptor) fx.Opti
fastws.MustNew(
fastws.WithPredicateKey("cmd"),
fastws.Listen(fmt.Sprintf("tcp4://0.0.0.0:%d", port)),
fastws.WithLogger(&stdLogger{}),
fastws.WithLogger(common.NewStdLogger()),
),
),
kit.WithServiceDesc(desc...),
Expand Down

0 comments on commit 8747134

Please sign in to comment.