all: use github.com/deckarep/golang-set/v2 (generic set) (#26159)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Jolly Zhao
2022-11-14 15:16:52 +01:00
committed by GitHub
co-authored by Felix Lange
parent 8c5ce1107b
commit f58ebd9696
10 changed files with 71 additions and 42 deletions
+7 -7
View File
@@ -21,7 +21,7 @@ import (
"io"
"sync/atomic"
mapset "github.com/deckarep/golang-set"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/log"
)
@@ -46,12 +46,12 @@ type Server struct {
services serviceRegistry
idgen func() ID
run int32
codecs mapset.Set
codecs mapset.Set[*ServerCodec]
}
// NewServer creates a new server instance with no registered handlers.
func NewServer() *Server {
server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet(), run: 1}
server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet[*ServerCodec](), run: 1}
// Register the default service providing meta information about the RPC service such
// as the services and methods it offers.
rpcService := &RPCService{server}
@@ -81,8 +81,8 @@ func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
}
// Add the codec to the set so it can be closed by Stop.
s.codecs.Add(codec)
defer s.codecs.Remove(codec)
s.codecs.Add(&codec)
defer s.codecs.Remove(&codec)
c := initClient(codec, s.idgen, &s.services)
<-codec.closed()
@@ -122,8 +122,8 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
func (s *Server) Stop() {
if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
log.Debug("RPC server shutting down")
s.codecs.Each(func(c interface{}) bool {
c.(ServerCodec).close()
s.codecs.Each(func(c *ServerCodec) bool {
(*c).close()
return true
})
}
+4 -4
View File
@@ -27,7 +27,7 @@ import (
"sync"
"time"
mapset "github.com/deckarep/golang-set"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/log"
"github.com/gorilla/websocket"
)
@@ -69,7 +69,7 @@ func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
// websocket upgrade process. When a '*' is specified as an allowed origins all
// connections are accepted.
func wsHandshakeValidator(allowedOrigins []string) func(*http.Request) bool {
origins := mapset.NewSet()
origins := mapset.NewSet[string]()
allowAllOrigins := false
for _, origin := range allowedOrigins {
@@ -122,10 +122,10 @@ func (e wsHandshakeError) Error() string {
return s
}
func originIsAllowed(allowedOrigins mapset.Set, browserOrigin string) bool {
func originIsAllowed(allowedOrigins mapset.Set[string], browserOrigin string) bool {
it := allowedOrigins.Iterator()
for origin := range it.C {
if ruleAllowsOrigin(origin.(string), browserOrigin) {
if ruleAllowsOrigin(origin, browserOrigin) {
return true
}
}