-
Notifications
You must be signed in to change notification settings - Fork 96
feat: add go-memory-load-mongo sample app #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pathakharshit
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-go-memory-load-mongo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| # Go build outputs | ||
| /bin/ | ||
| *.exe | ||
| *.exe~ | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
|
|
||
| # Test artifacts | ||
| *.test | ||
| *.out | ||
| coverage.txt | ||
| coverage.html | ||
|
|
||
| # Go vendor | ||
| vendor/ | ||
|
|
||
| # IDE | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| # Docker | ||
| **/.git |
This file contains hidden or 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,2 @@ | ||
| APP_PORT=8080 | ||
| MONGO_URI=mongodb://app_user:app_password@localhost:27017/orderdb?authSource=admin |
This file contains hidden or 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,18 @@ | ||
| FROM golang:1.26-alpine AS build | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY go.mod go.sum* ./ | ||
| RUN go mod download | ||
|
|
||
| COPY . . | ||
| RUN go build -o /bin/api ./cmd/api | ||
|
|
||
| FROM alpine:3.22 | ||
|
|
||
| WORKDIR /app | ||
| COPY --from=build /bin/api /app/api | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD ["/app/api"] |
This file contains hidden or 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,80 @@ | ||
| // Package main is the entry point for the load-test MongoDB API server. | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "loadtestmongoapi/internal/config" | ||
| "loadtestmongoapi/internal/database" | ||
| "loadtestmongoapi/internal/httpapi" | ||
| "loadtestmongoapi/internal/store" | ||
| ) | ||
|
|
||
| func main() { | ||
| logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
| Level: slog.LevelInfo, | ||
| })) | ||
|
|
||
| cfg, err := config.Load() | ||
| if err != nil { | ||
| logger.Error("load config", "error", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| defer stop() | ||
|
|
||
| client, db, err := database.Open(ctx, cfg.MongoURI, "orderdb") | ||
| if err != nil { | ||
| logger.Error("connect mongo", "error", err) | ||
| os.Exit(1) | ||
| } | ||
| defer func() { | ||
| disconnectCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
| _ = client.Disconnect(disconnectCtx) | ||
| }() | ||
|
|
||
| st := store.New(db) | ||
|
|
||
| if err := st.EnsureIndexes(ctx); err != nil { | ||
| logger.Error("ensure indexes", "error", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| handler := httpapi.New(st, logger) | ||
|
|
||
| server := &http.Server{ | ||
| Addr: ":" + cfg.Port, | ||
| Handler: handler, | ||
| ReadHeaderTimeout: 3 * time.Second, | ||
| ReadTimeout: 30 * time.Second, | ||
| WriteTimeout: 60 * time.Second, | ||
| IdleTimeout: 60 * time.Second, | ||
| } | ||
|
|
||
| go func() { | ||
| logger.Info("api listening", "addr", server.Addr) | ||
| if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
| logger.Error("listen and serve", "error", err) | ||
| stop() | ||
| } | ||
| }() | ||
|
|
||
| <-ctx.Done() | ||
| logger.Info("shutdown signal received") | ||
|
|
||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| if err := server.Shutdown(shutdownCtx); err != nil { | ||
| logger.Error("graceful shutdown", "error", err) | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or 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,41 @@ | ||
| services: | ||
| db: | ||
| image: mongo:7 | ||
| container_name: load-test-mongo-db | ||
| ports: | ||
| - "27017:27017" | ||
| volumes: | ||
| - mongo_data:/data/db | ||
| healthcheck: | ||
| test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping').ok"] | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 20 | ||
|
|
||
| api: | ||
| build: | ||
| context: . | ||
| container_name: load-test-mongo-api | ||
| environment: | ||
| APP_PORT: "8080" | ||
| MONGO_URI: mongodb://db:27017/orderdb | ||
| ports: | ||
| - "8080:8080" | ||
| depends_on: | ||
| db: | ||
| condition: service_healthy | ||
|
|
||
| k6: | ||
| image: grafana/k6:0.49.0 | ||
| profiles: ["loadtest"] | ||
| environment: | ||
| BASE_URL: http://api:8080 | ||
| volumes: | ||
| - ./loadtest:/scripts:ro | ||
| depends_on: | ||
| api: | ||
| condition: service_started | ||
| entrypoint: ["k6"] | ||
|
|
||
| volumes: | ||
| mongo_data: |
This file contains hidden or 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,17 @@ | ||
| module loadtestmongoapi | ||
|
|
||
| go 1.26 | ||
|
|
||
| require go.mongodb.org/mongo-driver/v2 v2.2.1 | ||
|
|
||
| require ( | ||
| github.com/golang/snappy v1.0.0 // indirect | ||
| github.com/klauspost/compress v1.16.7 // indirect | ||
| github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
| github.com/xdg-go/scram v1.1.2 // indirect | ||
| github.com/xdg-go/stringprep v1.0.4 // indirect | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect | ||
| golang.org/x/crypto v0.33.0 // indirect | ||
| golang.org/x/sync v0.11.0 // indirect | ||
| golang.org/x/text v0.22.0 // indirect | ||
| ) | ||
This file contains hidden or 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,48 @@ | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs= | ||
| github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||
| github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
| github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
| github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= | ||
| github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= | ||
| github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= | ||
| github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= | ||
| github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= | ||
| github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= | ||
| github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= | ||
| github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= | ||
| github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
| go.mongodb.org/mongo-driver/v2 v2.2.1 h1:w5xra3yyu/sGrziMzK1D0cRRaH/b7lWCSsoN6+WV6AM= | ||
| go.mongodb.org/mongo-driver/v2 v2.2.1/go.mod h1:qQkDMhCGWl3FN509DfdPd4GRBLU/41zqF/k8eTRceps= | ||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
| golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
| golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= | ||
| golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= | ||
| golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
| golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
| golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
| golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
| golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= | ||
| golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
| golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
| golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
| golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
| golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
| golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= | ||
| golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= | ||
| golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= | ||
| golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
| golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
| golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
| golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains hidden or 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,35 @@ | ||
| // Package config handles configuration loading from environment variables. | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| type Config struct { | ||
| Port string | ||
| MongoURI string | ||
| } | ||
|
|
||
| func Load() (Config, error) { | ||
| cfg := Config{ | ||
| Port: getEnv("APP_PORT", "8080"), | ||
| MongoURI: strings.TrimSpace(os.Getenv("MONGO_URI")), | ||
| } | ||
|
|
||
| if cfg.MongoURI == "" { | ||
| return Config{}, errors.New("MONGO_URI is required") | ||
| } | ||
|
|
||
| return cfg, nil | ||
| } | ||
|
|
||
| func getEnv(key, fallback string) string { | ||
| value := strings.TrimSpace(os.Getenv(key)) | ||
| if value == "" { | ||
| return fallback | ||
| } | ||
|
|
||
| return value | ||
| } |
This file contains hidden or 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,47 @@ | ||
| // Package database provides MongoDB connection helpers. | ||
| package database | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.mongodb.org/mongo-driver/v2/mongo" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/readpref" | ||
| ) | ||
|
|
||
| // Open creates a new MongoDB client, verifies connectivity with retries, and | ||
| // returns the client and the named database handle. | ||
| func Open(ctx context.Context, uri, dbName string) (*mongo.Client, *mongo.Database, error) { | ||
| opts := options.Client(). | ||
| ApplyURI(uri). | ||
| SetMaxPoolSize(25). | ||
| SetMinPoolSize(10). | ||
| SetMaxConnIdleTime(5 * time.Minute) | ||
|
|
||
| client, err := mongo.Connect(opts) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("connect mongo: %w", err) | ||
| } | ||
|
|
||
| var pingErr error | ||
| for attempt := 1; attempt <= 20; attempt++ { | ||
| pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second) | ||
| pingErr = client.Ping(pingCtx, readpref.Primary()) | ||
| cancel() | ||
| if pingErr == nil { | ||
| return client, client.Database(dbName), nil | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| _ = client.Disconnect(context.Background()) | ||
| return nil, nil, fmt.Errorf("ping mongo: %w", ctx.Err()) | ||
| case <-time.After(2 * time.Second): | ||
| } | ||
| } | ||
|
|
||
| _ = client.Disconnect(context.Background()) | ||
| return nil, nil, fmt.Errorf("ping mongo after retries: %w", pingErr) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new Go sample directory isn’t included in the
golangci-lintworkflow’sworking-directorymatrix, so CI linting won’t run for it. Addgo-memory-load-mongoto.github/workflows/golangci-lint.ymlto keep it consistent with the other Go samples.