Skip to content

Commit

Permalink
chore(release): [email protected]
Browse files Browse the repository at this point in the history
- refactor `ClusterAdapterWithHeartbeat`: updated `SetOpts` parameter to accept `any` type, allowing configuration retrieval through type assertion. This enhances compatibility with future rAdapter implementations.
  • Loading branch information
zishang520 committed Sep 30, 2024
1 parent 5a62765 commit 0856f95
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
24 changes: 0 additions & 24 deletions adapter/adapter-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,4 @@ type (
Adapter = socket.Adapter

SessionAwareAdapter = socket.SessionAwareAdapter

// A cluster-ready adapter. Any extending interface must:
//
// - implement [ClusterAdapter.DoPublish] and [ClusterAdapter.DoPublishResponse]
//
// - call [ClusterAdapter.OnMessage] and [ClusterAdapter.OnResponse]
ClusterAdapter interface {
Adapter

Uid() ServerId
OnMessage(*ClusterMessage, Offset)
OnResponse(*ClusterResponse)
Publish(*ClusterMessage)
PublishAndReturnOffset(*ClusterMessage) (Offset, error)
DoPublish(*ClusterMessage) (Offset, error)
PublishResponse(ServerId, *ClusterResponse)
DoPublishResponse(ServerId, *ClusterResponse) error
}

ClusterAdapterWithHeartbeat interface {
ClusterAdapter

SetOpts(*ClusterAdapterOptions)
}
)
18 changes: 18 additions & 0 deletions adapter/cluster-adapter-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ type (
ClientCountCallback func(uint64)
Ack socket.Ack
}

// A cluster-ready adapter. Any extending interface must:
//
// - implement [ClusterAdapter.DoPublish] and [ClusterAdapter.DoPublishResponse]
//
// - call [ClusterAdapter.OnMessage] and [ClusterAdapter.OnResponse]
ClusterAdapter interface {
Adapter

Uid() ServerId
OnMessage(*ClusterMessage, Offset)
OnResponse(*ClusterResponse)
Publish(*ClusterMessage)
PublishAndReturnOffset(*ClusterMessage) (Offset, error)
DoPublish(*ClusterMessage) (Offset, error)
PublishResponse(ServerId, *ClusterResponse)
DoPublishResponse(ServerId, *ClusterResponse) error
}
)

const (
Expand Down
6 changes: 6 additions & 0 deletions adapter/cluster-adapter-with-heartbeat-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ type (
MissingUids *types.Set[ServerId]
Responses *types.Slice[any]
}

ClusterAdapterWithHeartbeat interface {
ClusterAdapter

SetOpts(any)
}
)
11 changes: 5 additions & 6 deletions adapter/cluster-adapter-with-heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type (
ClusterAdapterWithHeartbeatBuilder struct {
Opts *ClusterAdapterOptions
Opts ClusterAdapterOptionsInterface
}

clusterAdapterWithHeartbeat struct {
Expand Down Expand Up @@ -45,7 +45,7 @@ func MakeClusterAdapterWithHeartbeat() ClusterAdapterWithHeartbeat {
return c
}

func NewClusterAdapterWithHeartbeat(nsp socket.Namespace, opts *ClusterAdapterOptions) ClusterAdapterWithHeartbeat {
func NewClusterAdapterWithHeartbeat(nsp socket.Namespace, opts any) ClusterAdapterWithHeartbeat {
c := MakeClusterAdapterWithHeartbeat()

c.SetOpts(opts)
Expand All @@ -55,11 +55,10 @@ func NewClusterAdapterWithHeartbeat(nsp socket.Namespace, opts *ClusterAdapterOp
return c
}

func (a *clusterAdapterWithHeartbeat) SetOpts(opts *ClusterAdapterOptions) {
if opts == nil {
opts = DefaultClusterAdapterOptions()
func (a *clusterAdapterWithHeartbeat) SetOpts(opts any) {
if options, ok := opts.(ClusterAdapterOptionsInterface); ok {
a._opts.Assign(options)
}
a._opts.Assign(opts)
}

func (a *clusterAdapterWithHeartbeat) Construct(nsp socket.Namespace) {
Expand Down
15 changes: 15 additions & 0 deletions adapter/cluster-adapter-with-heartbeat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package adapter

import (
"testing"

"github.com/zishang520/socket.io/v2/socket"
)

func TestClusterAdapterWithHeartbeatBuilder(t *testing.T) {
builder := &ClusterAdapterWithHeartbeatBuilder{
Opts: nil,
}

builder.New(socket.NewNamespace(socket.NewServer(nil, nil), "/test"))
}
14 changes: 7 additions & 7 deletions socket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type (
_serveClient bool
// @private
// #readonly
opts *ServerOptions
opts ServerOptionsInterface
eio engine.Server
_path string
clientPathRegex *regexp.Regexp
Expand All @@ -105,7 +105,7 @@ func MakeServer() *Server {
return s
}

func NewServer(srv any, opts *ServerOptions) *Server {
func NewServer(srv any, opts ServerOptionsInterface) *Server {
s := MakeServer()

s.Construct(srv, opts)
Expand All @@ -125,7 +125,7 @@ func (s *Server) Encoder() parser.Encoder {
return s.encoder
}

func (s *Server) Construct(srv any, opts *ServerOptions) {
func (s *Server) Construct(srv any, opts ServerOptionsInterface) {
if opts == nil {
opts = DefaultServerOptions()
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (s *Server) Construct(srv any, opts *ServerOptions) {
}
}

func (s *Server) Opts() *ServerOptions {
func (s *Server) Opts() ServerOptionsInterface {
return s.opts
}

Expand Down Expand Up @@ -340,10 +340,10 @@ func (s *Server) ServeHandler(opts *ServerOptions) http.Handler {
// Param: srv - the server to attach to
//
// Param: opts - options passed to engine.io
func (s *Server) initEngine(srv *types.HttpServer, opts *ServerOptions) {
func (s *Server) initEngine(srv *types.HttpServer, opts ServerOptionsInterface) {
// initialize engine
server_log.Debug("creating engine.io instance with opts %+v", opts)
s.eio = engine.Attach(srv, any(opts))
s.eio = engine.Attach(srv, opts)

// attach static file serving
if s._serveClient {
Expand All @@ -358,7 +358,7 @@ func (s *Server) initEngine(srv *types.HttpServer, opts *ServerOptions) {
}

// Attaches the static file serving.
func (s *Server) attachServe(srv *types.HttpServer, egs engine.Server, opts *ServerOptions) {
func (s *Server) attachServe(srv *types.HttpServer, egs engine.Server, opts ServerOptionsInterface) {
server_log.Debug("attaching client serving req handler")
srv.HandleFunc(s._path+"/", func(w http.ResponseWriter, r *http.Request) {
if s.clientPathRegex.MatchString(r.URL.Path) {
Expand Down

0 comments on commit 0856f95

Please sign in to comment.