From 0856f95f8b8c108c244820ce5809bab745f434bd Mon Sep 17 00:00:00 2001 From: luoyy Date: Mon, 30 Sep 2024 17:07:23 +0800 Subject: [PATCH] chore(release): socket.io@v2.3.2 - refactor `ClusterAdapterWithHeartbeat`: updated `SetOpts` parameter to accept `any` type, allowing configuration retrieval through type assertion. This enhances compatibility with future rAdapter implementations. --- adapter/adapter-type.go | 24 ------------------- adapter/cluster-adapter-type.go | 18 ++++++++++++++ .../cluster-adapter-with-heartbeat-type.go | 6 +++++ adapter/cluster-adapter-with-heartbeat.go | 11 ++++----- .../cluster-adapter-with-heartbeat_test.go | 15 ++++++++++++ socket/server.go | 14 +++++------ 6 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 adapter/cluster-adapter-with-heartbeat_test.go diff --git a/adapter/adapter-type.go b/adapter/adapter-type.go index 3d57779..9083024 100644 --- a/adapter/adapter-type.go +++ b/adapter/adapter-type.go @@ -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) - } ) diff --git a/adapter/cluster-adapter-type.go b/adapter/cluster-adapter-type.go index fca5ec9..37c9c34 100644 --- a/adapter/cluster-adapter-type.go +++ b/adapter/cluster-adapter-type.go @@ -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 ( diff --git a/adapter/cluster-adapter-with-heartbeat-type.go b/adapter/cluster-adapter-with-heartbeat-type.go index a2006af..b75779c 100644 --- a/adapter/cluster-adapter-with-heartbeat-type.go +++ b/adapter/cluster-adapter-with-heartbeat-type.go @@ -15,4 +15,10 @@ type ( MissingUids *types.Set[ServerId] Responses *types.Slice[any] } + + ClusterAdapterWithHeartbeat interface { + ClusterAdapter + + SetOpts(any) + } ) diff --git a/adapter/cluster-adapter-with-heartbeat.go b/adapter/cluster-adapter-with-heartbeat.go index adcca49..6244647 100644 --- a/adapter/cluster-adapter-with-heartbeat.go +++ b/adapter/cluster-adapter-with-heartbeat.go @@ -12,7 +12,7 @@ import ( type ( ClusterAdapterWithHeartbeatBuilder struct { - Opts *ClusterAdapterOptions + Opts ClusterAdapterOptionsInterface } clusterAdapterWithHeartbeat struct { @@ -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) @@ -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) { diff --git a/adapter/cluster-adapter-with-heartbeat_test.go b/adapter/cluster-adapter-with-heartbeat_test.go new file mode 100644 index 0000000..9da682b --- /dev/null +++ b/adapter/cluster-adapter-with-heartbeat_test.go @@ -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")) +} diff --git a/socket/server.go b/socket/server.go index a7debc1..b4e4b1e 100644 --- a/socket/server.go +++ b/socket/server.go @@ -85,7 +85,7 @@ type ( _serveClient bool // @private // #readonly - opts *ServerOptions + opts ServerOptionsInterface eio engine.Server _path string clientPathRegex *regexp.Regexp @@ -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) @@ -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() } @@ -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 } @@ -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 { @@ -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) {