Skip to content

Commit

Permalink
optmize optimize WSAcceptor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
urionz committed Mar 4, 2024
1 parent 13cff0c commit d313780
Show file tree
Hide file tree
Showing 23 changed files with 1,005 additions and 2,075 deletions.
1 change: 1 addition & 0 deletions acceptor/ws_acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewWSAcceptor(addr string, opts ...WSAcceptorOption) *WSAcceptor {
addr: addr,
connChan: make(chan PlayerConn),
running: false,
path: "/",
}

for _, opt := range opts {
Expand Down
31 changes: 14 additions & 17 deletions acceptor/ws_acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ var wsAcceptorTables = []struct {
// TODO change to allocatable ports
{"test_1", "0.0.0.0:0", []byte{0x01, 0x02}, []string{"./fixtures/server.crt", "./fixtures/server.key"}, nil},
{"test_2", "127.0.0.1:0", []byte{0x00}, []string{"./fixtures/server.crt", "./fixtures/server.key"}, nil},
{"test_3", "0.0.0.0:0", []byte{0x00}, []string{"wqodij"}, constants.ErrInvalidCertificates},
{"test_4", "0.0.0.0:0", []byte{0x00}, []string{"wqodij", "qwdo", "wod"}, constants.ErrInvalidCertificates},
{"test_4", "0.0.0.0:0", []byte{0x00}, []string{}, nil},
}

func TestNewWSAcceptor(t *testing.T) {
Expand All @@ -34,12 +31,12 @@ func TestNewWSAcceptor(t *testing.T) {
t.Run(table.name, func(t *testing.T) {
if table.panicErr != nil {
assert.PanicsWithValue(t, table.panicErr, func() {
NewWSAcceptor(table.addr, "", table.certs...)
NewWSAcceptor(table.addr, WithWSAcceptorCerts(table.certs[0], table.certs[1]))
})
} else {
var w *WSAcceptor
assert.NotPanics(t, func() {
w = NewWSAcceptor(table.addr, "", table.certs...)
w = NewWSAcceptor(table.addr, WithWSAcceptorCerts(table.certs[0], table.certs[1]))
})

if len(table.certs) == 2 {
Expand All @@ -59,7 +56,7 @@ func TestWSAcceptorGetAddr(t *testing.T) {
t.Parallel()
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
// will return empty string because acceptor is not listening
assert.Equal(t, "", w.GetAddr())
})
Expand All @@ -70,7 +67,7 @@ func TestWSAcceptorGetConn(t *testing.T) {
t.Parallel()
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
assert.NotNil(t, w.GetConnChan())
})
}
Expand All @@ -91,7 +88,7 @@ func mustConnectToWS(t *testing.T, write []byte, w *WSAcceptor, protocol string)
func TestWSAcceptorListenAndServe(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand All @@ -105,7 +102,7 @@ func TestWSAcceptorListenAndServe(t *testing.T) {
func TestWSAcceptorListenAndServeTLS(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServeTLS("./fixtures/server.crt", "./fixtures/server.key")
Expand All @@ -119,7 +116,7 @@ func TestWSAcceptorListenAndServeTLS(t *testing.T) {
func TestWSAcceptorStop(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
go w.ListenAndServe()
mustConnectToWS(t, table.write, w, "ws")
addr := fmt.Sprintf("ws://%s", w.GetAddr())
Expand All @@ -133,7 +130,7 @@ func TestWSAcceptorStop(t *testing.T) {
func TestWSConnRead(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand All @@ -151,7 +148,7 @@ func TestWSConnRead(t *testing.T) {
func TestWSConnWrite(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand All @@ -168,7 +165,7 @@ func TestWSConnWrite(t *testing.T) {
func TestWSConnLocalAddr(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand All @@ -183,7 +180,7 @@ func TestWSConnLocalAddr(t *testing.T) {
func TestWSConnRemoteAddr(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand All @@ -198,7 +195,7 @@ func TestWSConnRemoteAddr(t *testing.T) {
func TestWSConnSetDeadline(t *testing.T) {
for _, table := range wsAcceptorTables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor(table.addr, "/")
w := NewWSAcceptor(table.addr)
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand Down Expand Up @@ -226,7 +223,7 @@ func TestWSGetNextMessage(t *testing.T) {

for _, table := range tables {
t.Run(table.name, func(t *testing.T) {
w := NewWSAcceptor("0.0.0.0:0", "/")
w := NewWSAcceptor("0.0.0.0:0")
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand Down Expand Up @@ -256,7 +253,7 @@ func TestWSGetNextMessage(t *testing.T) {
}

func TestWSGetNextMessageSequentially(t *testing.T) {
w := NewWSAcceptor("0.0.0.0:0", "/")
w := NewWSAcceptor("0.0.0.0:0")
c := w.GetConnChan()
defer w.Stop()
go w.ListenAndServe()
Expand Down
3 changes: 1 addition & 2 deletions cluster/etcd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ import (
"strings"
"sync"
"time"

"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/constants"
"github.com/topfreegames/pitaya/v2/logger"
"github.com/topfreegames/pitaya/v2/util"
logutil "go.etcd.io/etcd/client/pkg/v3/logutil"
clientv3 "go.etcd.io/etcd/client/v3"
logutil "go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/client/v3/namespace"
"google.golang.org/grpc"
)
Expand Down
2 changes: 1 addition & 1 deletion cluster/nats_rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestNatsRPCClientCall(t *testing.T) {
ss.EXPECT().SetRequestInFlight(gomock.Any(), gomock.Any(), gomock.Any()).Times(2)

res, err := rpcClient.Call(context.Background(), protos.RPCType_Sys, rt, ss, msg, sv2)
assert.Equal(t, table.expected.GetData(), res.GetData())
assert.Equal(t, table.expected, res)
if table.err != nil {
assert.Error(t, err)
assert.Contains(t, err.Error(), table.err.Error())
Expand Down
6 changes: 3 additions & 3 deletions groups/etcd_group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"sync"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/namespace"
"go.etcd.io/etcd/api/v3/mvccpb"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/constants"
"github.com/topfreegames/pitaya/v2/logger"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/namespace"
)

var (
Expand Down
1 change: 0 additions & 1 deletion helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func GetTestNatsServer(t *testing.T) *server.Server {
// GetTestEtcd gets a test in memory etcd server
func GetTestEtcd(t *testing.T) (*integration.ClusterV3, *clientv3.Client) {
t.Helper()
// skip etcd go leak detection
integration.BeforeTest(t)
c := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
cli := c.RandClient()
Expand Down
4 changes: 2 additions & 2 deletions modules/binding_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
"fmt"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/namespace"
"github.com/topfreegames/pitaya/v2/cluster"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/constants"
"github.com/topfreegames/pitaya/v2/logger"
"github.com/topfreegames/pitaya/v2/session"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/namespace"
)

// ETCDBindingStorage module that uses etcd to keep in which frontend server each user is bound
Expand Down
Loading

0 comments on commit d313780

Please sign in to comment.