Skip to content

Commit

Permalink
build: cache go mod
Browse files Browse the repository at this point in the history
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
blakepettersson committed Sep 22, 2023
1 parent b5926d7 commit 8351667
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ ARG LD_FLAGS='-s -w -linkmode external -extldflags "-static"'
ARG TARGETPLATFORM

WORKDIR /app
COPY . .

RUN export GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) && \
export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2)

RUN go env
COPY go.* ./
RUN go env && go mod download

RUN go get -d -v \
&& go install -v
COPY . .

RUN CGO_ENABLED=1 go build -ldflags="${LD_FLAGS}" -tags="sqlite_unlock_notify" -o /app/build/policyreporter -v

Expand Down
37 changes: 37 additions & 0 deletions pkg/target/http/logroundtripper.go
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
}
29 changes: 29 additions & 0 deletions pkg/target/http/ratelimiter.go
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
}

0 comments on commit 8351667

Please sign in to comment.