mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 14:33:52 +00:00
feat(rpc): allow configure WS compression (#210)
* Default open ws compression. * Default open ws compression. * Enable ws compression. * Enable ws compression. --------- Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: HAOYUatHZ <haoyu@protonmail.com>
This commit is contained in:
parent
8224bc8b9c
commit
bb26fa3e39
2 changed files with 23 additions and 4 deletions
|
|
@ -17,7 +17,9 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/flate"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
|
@ -47,6 +49,8 @@ type Server struct {
|
||||||
idgen func() ID
|
idgen func() ID
|
||||||
run int32
|
run int32
|
||||||
codecs mapset.Set
|
codecs mapset.Set
|
||||||
|
// Add compressionLevel inorder to enable set it when open websocket server.
|
||||||
|
compressionLevel int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer creates a new server instance with no registered handlers.
|
// NewServer creates a new server instance with no registered handlers.
|
||||||
|
|
@ -89,6 +93,15 @@ func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCompressionLevel set compression level (-2 ~ 9), this function only works on websocket.
|
||||||
|
func (s *Server) SetCompressionLevel(level int) error {
|
||||||
|
if !(flate.HuffmanOnly <= level && level <= flate.BestCompression) {
|
||||||
|
return errors.New("websocket: invalid compression level")
|
||||||
|
}
|
||||||
|
s.compressionLevel = level
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// serveSingleRequest reads and processes a single RPC request from the given codec. This
|
// serveSingleRequest reads and processes a single RPC request from the given codec. This
|
||||||
// is used to serve HTTP connections. Subscriptions and reverse calls are not allowed in
|
// is used to serve HTTP connections. Subscriptions and reverse calls are not allowed in
|
||||||
// this mode.
|
// this mode.
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/flate"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -49,11 +50,13 @@ var wsBufferPool = new(sync.Pool)
|
||||||
// allowedOrigins should be a comma-separated list of allowed origin URLs.
|
// allowedOrigins should be a comma-separated list of allowed origin URLs.
|
||||||
// To allow connections with any origin, pass "*".
|
// To allow connections with any origin, pass "*".
|
||||||
func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
|
func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
|
||||||
|
enableCompression := s.compressionLevel != flate.NoCompression
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: wsReadBuffer,
|
EnableCompression: enableCompression,
|
||||||
WriteBufferSize: wsWriteBuffer,
|
ReadBufferSize: wsReadBuffer,
|
||||||
WriteBufferPool: wsBufferPool,
|
WriteBufferSize: wsWriteBuffer,
|
||||||
CheckOrigin: wsHandshakeValidator(allowedOrigins),
|
WriteBufferPool: wsBufferPool,
|
||||||
|
CheckOrigin: wsHandshakeValidator(allowedOrigins),
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
|
@ -61,6 +64,9 @@ func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
|
||||||
log.Debug("WebSocket upgrade failed", "err", err)
|
log.Debug("WebSocket upgrade failed", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if enableCompression {
|
||||||
|
_ = conn.SetCompressionLevel(s.compressionLevel)
|
||||||
|
}
|
||||||
codec := newWebsocketCodec(conn)
|
codec := newWebsocketCodec(conn)
|
||||||
s.ServeCodec(codec, 0)
|
s.ServeCodec(codec, 0)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue