Skip to content

Commit

Permalink
added group options
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrik-haase committed Sep 5, 2024
1 parent de1ac63 commit 39b65bf
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/dummy_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/scayle/goload"
"github.com/HenriBeck/goload"
)

type DummyExecutor struct {
Expand Down
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"github.com/scayle/goload"
goload_http "github.com/scayle/goload/http"
"github.com/scayle/goload/http/url_builder"
"github.com/scayle/goload/pacer"
"github.com/HenriBeck/goload"
goload_http "github.com/HenriBeck/goload/http"
"github.com/HenriBeck/goload/http/url_builder"
"github.com/HenriBeck/goload/pacer"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/scayle/goload
module github.com/HenriBeck/goload

go 1.21

Expand Down
25 changes: 0 additions & 25 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goload
import (
"context"
"github.com/mroth/weightedrand/v2"
"github.com/rs/zerolog/log"
"time"
)

Expand All @@ -14,30 +13,6 @@ type executorGroup struct {
timeout time.Duration
}

// TODO: add options
func NewGroup(name string, weight int, executors []Executor) Executor {
if len(executors) == 0 {
log.Fatal().Msg("group can't be empty")
}
choises := make([]weightedrand.Choice[Executor, int], 0, len(executors))
for _, exec := range executors {
choises = append(choises, weightedrand.NewChoice(exec, exec.Options().Weight))
}
chooser, err := weightedrand.NewChooser(
choises...,
)
if err != nil {
log.Fatal().Err(err).Msg("can't create chooser")
}

return &executorGroup{
name: name,
chooser: chooser,
weight: weight,
timeout: 0,
}
}

func (e *executorGroup) Execute(ctx context.Context) ExecutionResponse {
return e.chooser.Pick().Execute(ctx)
}
Expand Down
76 changes: 76 additions & 0 deletions group_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package goload

import (
"github.com/mroth/weightedrand/v2"
"github.com/rs/zerolog/log"
"time"
)

type GroupOptions struct {
name string
weight int
timeout time.Duration
executors []Executor
}

type GroupOption func(*GroupOptions)

func WithGroup(opts ...GroupOption) Executor {
options := &GroupOptions{}
for _, opt := range opts {
opt(options)
}

if len(options.executors) == 0 {
log.Fatal().Msg("group can't be empty")
}
choises := make([]weightedrand.Choice[Executor, int], 0, len(options.executors))
for _, exec := range options.executors {
choises = append(choises, weightedrand.NewChoice(exec, exec.Options().Weight))
}
chooser, err := weightedrand.NewChooser(
choises...,
)
if err != nil {
log.Fatal().Err(err).Msg("can't create chooser")
}

if options.weight == 0 {
weightSum := 0
for _, exec := range options.executors {
weightSum += exec.Options().Weight
}
options.weight = weightSum
}

return &executorGroup{
name: options.name,
chooser: chooser,
weight: options.weight,
timeout: options.timeout,
}
}

func WithGroupWeight(weight int) GroupOption {
return func(options *GroupOptions) {
options.weight = weight
}
}

func WithGroupTimeout(timeout time.Duration) GroupOption {
return func(options *GroupOptions) {
options.timeout = timeout
}
}

func WithGroupExecutors(executors ...Executor) GroupOption {
return func(options *GroupOptions) {
options.executors = append(options.executors, executors...)
}
}

func WithGroupName(name string) GroupOption {
return func(options *GroupOptions) {
options.name = name
}
}
2 changes: 1 addition & 1 deletion http/http_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package goload_http
import (
"context"
"fmt"
"github.com/HenriBeck/goload"
"github.com/rs/zerolog/log"
"github.com/scayle/goload"
"io"
"net/http"
"net/url"
Expand Down
4 changes: 2 additions & 2 deletions http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package goload_http

import (
"errors"
"github.com/scayle/goload"
"github.com/scayle/goload/http/url_builder"
"github.com/HenriBeck/goload"
"github.com/HenriBeck/goload/http/url_builder"
"io"
"net/http"
"net/url"
Expand Down
2 changes: 1 addition & 1 deletion http/url_builder/query_param.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package url_builder

import (
"github.com/HenriBeck/goload/utils/random"
"github.com/mroth/weightedrand/v2"
"github.com/rs/zerolog/log"
"github.com/scayle/goload/utils/random"
"net/url"
)

Expand Down
2 changes: 1 addition & 1 deletion http/url_builder/query_param_options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package url_builder

import (
"github.com/HenriBeck/goload/utils/random"
"github.com/mroth/weightedrand/v2"
"github.com/rs/zerolog/log"
"github.com/scayle/goload/utils/random"
"strconv"
)

Expand Down
2 changes: 1 addition & 1 deletion http/url_builder/url_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package url_builder

import (
"fmt"
"github.com/HenriBeck/goload/utils/random"
"github.com/rs/zerolog/log"
"github.com/scayle/goload/utils/random"
"net/url"
"strings"
)
Expand Down
4 changes: 2 additions & 2 deletions loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package goload
import (
"context"
"fmt"
"github.com/HenriBeck/goload/pacer"
ctx_utils "github.com/HenriBeck/goload/utils/ctx"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/scayle/goload/pacer"
ctx_utils "github.com/scayle/goload/utils/ctx"
"math"
"os"
"time"
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package goload

import (
"context"
"github.com/scayle/goload/pacer"
"github.com/HenriBeck/goload/pacer"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package goload

import (
"context"
"github.com/HenriBeck/goload/pacer"
"github.com/mroth/weightedrand/v2"
"github.com/rs/zerolog/log"
"github.com/scayle/goload/pacer"
"sync"
"time"
)
Expand Down

0 comments on commit 39b65bf

Please sign in to comment.