Skip to content

Commit

Permalink
Merge pull request #57 from h0n9/develop
Browse files Browse the repository at this point in the history
Prepare to release `v0.0.12`
  • Loading branch information
h0n9 authored Nov 27, 2024
2 parents 298b717 + 157c697 commit 18c23e3
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 619 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
.DS_Store
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# builder
FROM golang:1.20.4-alpine3.17 AS builder
FROM golang:1.23.3-alpine3.20 AS builder
WORKDIR /usr/src/app
COPY go.mod go.sum ./
RUN go mod download
Expand All @@ -14,7 +14,7 @@ COPY relayer/ relayer/
RUN go build ./cmd/msg-lake

# runner
FROM alpine:3.17.3 AS runner
FROM alpine:3.20 AS runner
WORKDIR /usr/bin/app
RUN addgroup --system app && adduser --system --shell /bin/false --ingroup app app
COPY --from=builder /usr/src/app/msg-lake .
Expand Down
2 changes: 2 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/h0n9/msg-lake/cli/agent"
"github.com/h0n9/msg-lake/cli/client"
"github.com/h0n9/msg-lake/cli/tools"
)

var RootCmd = &cobra.Command{
Expand All @@ -17,5 +18,6 @@ func init() {
RootCmd.AddCommand(
agent.Cmd,
client.Cmd,
tools.Cmd,
)
}
138 changes: 138 additions & 0 deletions cli/tools/loader/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package loader

import (
"context"
"fmt"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/spf13/cobra"

"github.com/postie-labs/go-postie-lib/crypto"

"github.com/h0n9/msg-lake/client"
)

var Cmd = &cobra.Command{
Use: "loader",
Short: "tool for load test",
RunE: runE,
}

var (
tlsEnabled bool
hostAddr string
topicID string
nickname string

interval time.Duration
loadCount int // 0 means unlimited
)

func runE(cmd *cobra.Command, args []string) error {
var msgLakeClient *client.Client

// init wg
wg := sync.WaitGroup{}

// init sig channel
sigCh := make(chan os.Signal, 1)
defer close(sigCh)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)

// init ctx with cancel
ctx, cancel := context.WithCancel(context.Background())

// listen signals
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-ctx.Done():
fmt.Println("cancelled context")
case s := <-sigCh:
fmt.Printf("got signal %v\n", s)
fmt.Printf("cancelling ctx ... ")
cancel()
fmt.Printf("done\n")
}
if msgLakeClient != nil {
fmt.Printf("closing msg lake client ... ")
msgLakeClient.Close()
fmt.Printf("done\n")
}
}()

/////////////////////////////////
// real things begin from here //
/////////////////////////////////

// init privKey
privKey, err := crypto.GenPrivKeyFromSeed([]byte(nickname))
if err != nil {
return err
}
// pubKeyBytes := privKey.PubKey().Bytes()

// init msg lake client
msgLakeClient, err = client.NewClient(privKey, hostAddr, tlsEnabled)
if err != nil {
return err
}

wg.Add(1)
go func() {
defer wg.Done()

// init ticker with interval
ticker := time.NewTicker(interval)
defer ticker.Stop()

var (
i int = 0
err error
)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if loadCount > 0 && i >= loadCount {
fmt.Println("end of load test")
cancel()
return
}
// do something here
fmt.Println(i)
err = msgLakeClient.Publish(ctx, topicID, "")
if err != nil {
fmt.Println(err)
}

// increment i by 1
i += 1
}
}
}()

wg.Wait()

return nil
}

func init() {
r := rand.New(rand.NewSource(time.Now().UnixMicro())).Int()

Cmd.Flags().BoolVarP(&tlsEnabled, "tls", "t", false, "enable tls connection")
Cmd.Flags().StringVar(&hostAddr, "host", "localhost:8080", "host addr")
Cmd.Flags().StringVar(&topicID, "topic", "life is beautiful", "topic id")
Cmd.Flags().StringVarP(&nickname, "nickname", "n", fmt.Sprintf("alien-%d", r), "consumer id")

Cmd.Flags().DurationVar(&interval, "interval", 1*time.Second, "interval")
Cmd.Flags().IntVarP(&loadCount, "count", "c", 0, "load count (0 means unlimited)")
}
18 changes: 18 additions & 0 deletions cli/tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tools

import (
"github.com/h0n9/msg-lake/cli/tools/loader"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "tools",
Short: "useful tools for msg-lake",
}

func init() {
cobra.EnableCommandSorting = false
Cmd.AddCommand(
loader.Cmd,
)
}
11 changes: 10 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
build:
context: .
image: ghcr.io/h0n9/msg-lake
environment:
- UNARY_SERVER_INTERCEPTOR_RATE_LIMIT=10
command: ["agent", "--mdns"]
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080"]
Expand Down Expand Up @@ -36,4 +38,11 @@ services:
image: ghcr.io/h0n9/msg-lake
command: ["client", "--host", "nginx:8080"]
deploy:
replicas: 0
replicas: 3
loader:
depends_on:
- nginx
image: ghcr.io/h0n9/msg-lake
command: ["tools", "loader", "--interval", "10ms", "--host", "nginx:8080"]
deploy:
replicas: 10
Loading

0 comments on commit 18c23e3

Please sign in to comment.