Skip to content

Commit

Permalink
hub: allow switching zlib level at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed May 5, 2019
1 parent 3972301 commit ae2a4e7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
4 changes: 2 additions & 2 deletions adc/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (c *Conn) SetWriteTimeout(dt time.Duration) {
}
}

func (c *Conn) ZOn() error {
return c.w.EnableZlib()
func (c *Conn) ZOn(lvl int) error {
return c.w.EnableZlibLevel(lvl)
}

// Close closes the connection.
Expand Down
2 changes: 1 addition & 1 deletion hub/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func cmdParseString(text string) (string, string, error) {
if len(text) == 0 {
return "", text, errCmdInvalidArg
}
if text[0] != '"' && text[1] != '`' {
if text[0] != '"' && text[0] != '`' {
// unquoted string
i := strings.IndexByte(text, ' ')
if i < 0 {
Expand Down
15 changes: 15 additions & 0 deletions hub/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const (
ConfigHubMOTD = "hub.motd"
)

const (
ConfigZlibLevel = "zlib.level"
)

var configAliases = map[string]string{
"name": ConfigHubName,
"desc": ConfigHubDesc,
Expand Down Expand Up @@ -132,6 +136,7 @@ func (h *Hub) ConfigKeys() []string {
ConfigHubOwner,
ConfigHubWebsite,
ConfigHubEmail,
ConfigZlibLevel,
}
h.conf.RLock()
for k := range h.conf.m {
Expand Down Expand Up @@ -162,6 +167,12 @@ func (h *Hub) GetConfig(key string) (interface{}, bool) {
return nil, false
}
return v, true
case ConfigZlibLevel:
v, ok := h.GetConfigInt(key)
if !ok {
return nil, false
}
return v, true
}
h.conf.RLock()
v, ok := h.conf.m[key]
Expand Down Expand Up @@ -309,6 +320,8 @@ func (h *Hub) setConfigInt(key string, val int64) {
return
}
switch key {
case ConfigZlibLevel:
h.setZlibLevel(int(val))
default:
h.setConfigMap(key, val)
}
Expand All @@ -324,6 +337,8 @@ func (h *Hub) GetConfigInt(key string) (int64, bool) {
key = alias
}
switch key {
case ConfigZlibLevel:
return int64(h.zlibLevel()), true
default:
v, ok := h.getConfigMap(key)
if !ok || v == nil {
Expand Down
18 changes: 18 additions & 0 deletions hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func NewHub(conf Config) (*Hub, error) {
tls: conf.TLS,
}
h.conf.Config = conf
h.setZlibLevel(-1)
if conf.FallbackEncoding != "" {
enc, err := htmlindex.Get(conf.FallbackEncoding)
if err != nil {
Expand Down Expand Up @@ -145,6 +146,9 @@ type Hub struct {
names map[string]struct{} // no aliases
byName map[string]*Command
}
zlib struct {
level int32
}

globalChat *Room
rooms rooms
Expand Down Expand Up @@ -172,6 +176,20 @@ func (h *Hub) decShare(v uint64) {
cntShare.Add(-float64(v))
}

func (h *Hub) zlibLevel() int {
return int(atomic.LoadInt32(&h.zlib.level))
}

func (h *Hub) setZlibLevel(level int) {
if level < -1 {
level = -1
}
if level > 9 {
level = 9
}
atomic.StoreInt32(&h.zlib.level, int32(level))
}

type Stats struct {
Name string `json:"name"`
Desc string `json:"desc,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions hub/hub_adc.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ func (h *Hub) adcStageProtocol(c *adc.Conn, cinfo *ConnInfo) (*adcPeer, error) {
return nil, fmt.Errorf("client does not support TIGR")
}

if mutual.IsSet(adc.FeaZLIF) {
if lvl := h.zlibLevel(); lvl != 0 && mutual.IsSet(adc.FeaZLIF) {
err = c.WriteInfoMsg(adc.ZOn{})
if err != nil {
return nil, err
}
err = c.ZOn()
err = c.ZOn(lvl)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions hub/hub_nmdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ func (h *Hub) nmdcAccept(peer *nmdcPeer) error {
if err != nil {
return err
}
if peer.fea.Has(nmdcp.ExtZPipe0) {
err = c.ZOn() // flushes
if lvl := h.zlibLevel(); lvl != 0 && peer.fea.Has(nmdcp.ExtZPipe0) {
err = c.ZOn(lvl) // flushes
} else {
err = c.Flush()
}
Expand Down
4 changes: 4 additions & 0 deletions hub/plugins/tor/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ github.com/direct-connect/go-dc v0.7.2/go.mod h1:AwUTf54jhNPJvs+QNHoZxEb3p0LS8ik
github.com/direct-connect/go-dc v0.7.3/go.mod h1:AwUTf54jhNPJvs+QNHoZxEb3p0LS8ik+2t0R8b4+MdI=
github.com/direct-connect/go-dc v0.7.4/go.mod h1:AwUTf54jhNPJvs+QNHoZxEb3p0LS8ik+2t0R8b4+MdI=
github.com/direct-connect/go-dc v0.8.0/go.mod h1:AwUTf54jhNPJvs+QNHoZxEb3p0LS8ik+2t0R8b4+MdI=
github.com/direct-connect/go-dc v0.8.1 h1:PSE7WRV+XU+CHkis1RPo73Jnky/Kzn1JZipsGyQm3Dg=
github.com/direct-connect/go-dc v0.8.1/go.mod h1:AwUTf54jhNPJvs+QNHoZxEb3p0LS8ik+2t0R8b4+MdI=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -307,6 +308,7 @@ golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaE
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480 h1:O5YqonU5IWby+w98jVUG9h7zlCWCcH4RHyPVReBmhzk=
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -341,6 +343,7 @@ golang.org/x/net v0.0.0-20190420063019-afa5a82059c6/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -376,6 +379,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
4 changes: 2 additions & 2 deletions nmdc/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func (c *Conn) SetFallbackEncoding(enc encoding.Encoding) {
c.fallback = enc
}

func (c *Conn) ZOn() error {
return c.w.ZOn()
func (c *Conn) ZOn(lvl int) error {
return c.w.ZOnLevel(lvl)
}

// Close closes the connection.
Expand Down

0 comments on commit ae2a4e7

Please sign in to comment.