leaderless gossip-based service discovery w/ consul compatibility
5

Configure Feed

Select the types of activity you want to include in your feed.

remove unnecessary cleanhttp dep

Jes Olson (Mar 6, 2023, 7:01 PM -0800) c259874e 042e90a4

+65 -133
+40 -2
agent/http.go
··· 8 8 "net/http" 9 9 "strings" 10 10 "time" 11 + "unicode" 11 12 12 13 "git.j3s.sh/cascade/lib" 13 - "git.j3s.sh/cascade/lib/cleanhttp" 14 14 ) 15 15 16 16 // MethodNotAllowedError should be returned by a handler when the HTTP method is not allowed. ··· 118 118 } 119 119 120 120 // This handler bans URLs with non-printable characters 121 - h := cleanhttp.PrintablePathCheckHandler(mux, nil) 121 + h := printablePathCheckHandler(mux, nil) 122 122 return h 123 + } 124 + 125 + type HandlerInput struct { 126 + ErrStatus int 127 + } 128 + 129 + func printablePathCheckHandler(next http.Handler, input *HandlerInput) http.Handler { 130 + // Nil-check on input to make it optional 131 + if input == nil { 132 + input = &HandlerInput{ 133 + ErrStatus: http.StatusBadRequest, 134 + } 135 + } 136 + 137 + // Default to http.StatusBadRequest on error 138 + if input.ErrStatus == 0 { 139 + input.ErrStatus = http.StatusBadRequest 140 + } 141 + 142 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 143 + if r != nil { 144 + // Check URL path for non-printable characters 145 + idx := strings.IndexFunc(r.URL.Path, func(c rune) bool { 146 + return !unicode.IsPrint(c) 147 + }) 148 + 149 + if idx != -1 { 150 + w.WriteHeader(input.ErrStatus) 151 + return 152 + } 153 + 154 + if next != nil { 155 + next.ServeHTTP(w, r) 156 + } 157 + } 158 + 159 + return 160 + }) 123 161 } 124 162 125 163 // wrap is used to wrap functions to make them more convenient
+8
agent/service.go
··· 1 + package agent 2 + 3 + import "net" 4 + 5 + type Service struct { 6 + Name string 7 + Addr *net.TCPAddr 8 + }
+16 -22
api/api.go
··· 10 10 "net" 11 11 "net/http" 12 12 "net/url" 13 + "runtime" 13 14 "strconv" 14 15 "strings" 15 16 "sync" 16 17 "time" 17 - 18 - "git.j3s.sh/cascade/lib/cleanhttp" 19 18 ) 20 19 21 20 // Config is used to configure the creation of a client ··· 39 38 } 40 39 41 40 // DefaultConfig returns a default configuration for the client. By default this 42 - // will pool and reuse idle connections to cascade. If you have a long-lived 43 - // client object, this is the desired behavior and should make the most efficient 44 - // use of the connections to cascade. If you don't reuse a client object, which 45 - // is not recommended, then you may notice idle connections building up over 46 - // time. To avoid this, use the DefaultNonPooledConfig() instead. 41 + // will pool and reuse idle connections to cascade. 47 42 func DefaultConfig() *Config { 48 - return defaultConfig(cleanhttp.DefaultPooledTransport) 49 - } 50 - 51 - // DefaultNonPooledConfig returns a default configuration for the client which 52 - // does not pool connections. This isn't a recommended configuration because it 53 - // will reconnect to cascade on every request, but this is useful to avoid the 54 - // accumulation of idle connections if you make many client objects during the 55 - // lifetime of your application. 56 - func DefaultNonPooledConfig() *Config { 57 - return defaultConfig(cleanhttp.DefaultTransport) 58 - } 43 + transport := &http.Transport{ 44 + Proxy: http.ProxyFromEnvironment, 45 + DialContext: (&net.Dialer{ 46 + Timeout: 30 * time.Second, 47 + KeepAlive: 30 * time.Second, 48 + DualStack: true, 49 + }).DialContext, 50 + MaxIdleConns: 100, 51 + IdleConnTimeout: 90 * time.Second, 52 + ExpectContinueTimeout: 1 * time.Second, 53 + ForceAttemptHTTP2: true, 54 + MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1, 55 + } 59 56 60 - // defaultConfig returns the default configuration for the client, using the 61 - // given function to make the transport. 62 - func defaultConfig(transportFn func() *http.Transport) *Config { 63 57 config := &Config{ 64 58 Address: "127.0.0.1:8500", 65 59 Scheme: "http", 66 - Transport: transportFn(), 60 + Transport: transport, 67 61 } 68 62 69 63 return config
+1 -1
command/ls/members/members.go
··· 40 40 flags.BoolVar(&membersFlags.details, "details", false, "") 41 41 flags.Parse(args) 42 42 43 - cfg := api.DefaultNonPooledConfig() 43 + cfg := api.DefaultConfig() 44 44 client, err := api.NewClient(cfg) 45 45 if err != nil { 46 46 fmt.Println(err)
-60
lib/cleanhttp/cleanhttp.go
··· 1 - // Copyright (c) HashiCorp, Inc. 2 - // SPDX-License-Identifier: MPL-2.0 3 - 4 - package cleanhttp 5 - 6 - import ( 7 - "net" 8 - "net/http" 9 - "runtime" 10 - "time" 11 - ) 12 - 13 - // DefaultTransport returns a new http.Transport with similar default values to 14 - // http.DefaultTransport, but with idle connections and keepalives disabled. 15 - func DefaultTransport() *http.Transport { 16 - transport := DefaultPooledTransport() 17 - transport.DisableKeepAlives = true 18 - transport.MaxIdleConnsPerHost = -1 19 - return transport 20 - } 21 - 22 - // DefaultPooledTransport returns a new http.Transport with similar default 23 - // values to http.DefaultTransport. Do not use this for transient transports as 24 - // it can leak file descriptors over time. Only use this for transports that 25 - // will be re-used for the same host(s). 26 - func DefaultPooledTransport() *http.Transport { 27 - transport := &http.Transport{ 28 - Proxy: http.ProxyFromEnvironment, 29 - DialContext: (&net.Dialer{ 30 - Timeout: 30 * time.Second, 31 - KeepAlive: 30 * time.Second, 32 - DualStack: true, 33 - }).DialContext, 34 - MaxIdleConns: 100, 35 - IdleConnTimeout: 90 * time.Second, 36 - ExpectContinueTimeout: 1 * time.Second, 37 - ForceAttemptHTTP2: true, 38 - MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1, 39 - } 40 - return transport 41 - } 42 - 43 - // DefaultClient returns a new http.Client with similar default values to 44 - // http.Client, but with a non-shared Transport, idle connections disabled, and 45 - // keepalives disabled. 46 - func DefaultClient() *http.Client { 47 - return &http.Client{ 48 - Transport: DefaultTransport(), 49 - } 50 - } 51 - 52 - // DefaultPooledClient returns a new http.Client with similar default values to 53 - // http.Client, but with a shared Transport. Do not use this function for 54 - // transient clients as it can leak file descriptors over time. Only use this 55 - // for clients that will be re-used for the same host(s). 56 - func DefaultPooledClient() *http.Client { 57 - return &http.Client{ 58 - Transport: DefaultPooledTransport(), 59 - } 60 - }
-48
lib/cleanhttp/handlers.go
··· 1 - package cleanhttp 2 - 3 - import ( 4 - "net/http" 5 - "strings" 6 - "unicode" 7 - ) 8 - 9 - // HandlerInput provides input options to cleanhttp's handlers 10 - type HandlerInput struct { 11 - ErrStatus int 12 - } 13 - 14 - // PrintablePathCheckHandler is a middleware that ensures the request path 15 - // contains only printable runes. 16 - func PrintablePathCheckHandler(next http.Handler, input *HandlerInput) http.Handler { 17 - // Nil-check on input to make it optional 18 - if input == nil { 19 - input = &HandlerInput{ 20 - ErrStatus: http.StatusBadRequest, 21 - } 22 - } 23 - 24 - // Default to http.StatusBadRequest on error 25 - if input.ErrStatus == 0 { 26 - input.ErrStatus = http.StatusBadRequest 27 - } 28 - 29 - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 - if r != nil { 31 - // Check URL path for non-printable characters 32 - idx := strings.IndexFunc(r.URL.Path, func(c rune) bool { 33 - return !unicode.IsPrint(c) 34 - }) 35 - 36 - if idx != -1 { 37 - w.WriteHeader(input.ErrStatus) 38 - return 39 - } 40 - 41 - if next != nil { 42 - next.ServeHTTP(w, r) 43 - } 44 - } 45 - 46 - return 47 - }) 48 - }