-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For local development, to remove the step where go modules are downloaded on every `docker build`, first copy only `go.mod` and `go.sum` to the Docker workspace. This step only needs to be performed whenever one of those files changes. Signed-off-by: Blake Pettersson <[email protected]>
- Loading branch information
1 parent
b5926d7
commit 8351667
Showing
3 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
func NewLoggingRoundTripper(roundTripper http.RoundTripper) http.RoundTripper { | ||
return &logRoundTripper{roundTripper: roundTripper} | ||
} | ||
|
||
type logRoundTripper struct { | ||
roundTripper http.RoundTripper | ||
} | ||
|
||
func (rt *logRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
logger := zap.L() | ||
if logger.Core().Enabled(zap.DebugLevel) { | ||
if info, err := httputil.DumpRequest(req, true); err == nil { | ||
logger.Debug(fmt.Sprintf("Sending request: %s", string(info))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
resp, err := rt.roundTripper.RoundTrip(req) | ||
if resp != nil { | ||
if logger.Core().Enabled(zap.DebugLevel) { | ||
if info, err := httputil.DumpResponse(resp, true); err == nil { | ||
logger.Debug(fmt.Sprintf("Received response: %s", string(info))) | ||
} | ||
} | ||
} | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package http | ||
|
||
import ( | ||
"golang.org/x/time/rate" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// rateLimiterRoundTripper is an implementation of http.RoundTripper that adds rate limiting. | ||
type rateLimiterRoundTripper struct { | ||
limiter *rate.Limiter | ||
transport http.RoundTripper | ||
} | ||
|
||
func NewRateLimiterRoundTripper(rps int, transport http.RoundTripper) http.RoundTripper { | ||
return &rateLimiterRoundTripper{ | ||
transport: transport, | ||
limiter: rate.NewLimiter(rate.Every((time.Second*2)/time.Duration(rps)), rps), | ||
} | ||
} | ||
|
||
func (rt *rateLimiterRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
ctx := req.Context() | ||
err := rt.limiter.Wait(ctx) // This will block until a token is available or context is done | ||
if err != nil { | ||
return nil, err // Context was done or rate limiter was stopped | ||
} | ||
return rt.transport.RoundTrip(req) // Perform the actual request | ||
} |