Skip to content

Commit

Permalink
Options refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
antelman107 committed Jun 30, 2020
1 parent 97fbe8f commit c719ab9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 26 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ But also this package can be donwloaded as utility and used from command line.

# Library usage

## Simple
```GO
import "github.com/antelman107/net-wait-go/wait"

if !wait.Do(
"tcp", // proto
[]string{"postgres:5432"}, // addresses
time.Millisecond*100, // delay between requests
time.Second*15, // deadline
true, // debug
) {
if !wait.New().Do([]string{"postgres:5432"}) {
logger.Error("db is not available")
return
}
```

## All optional settings definition
```GO
import "github.com/antelman107/net-wait-go/wait"

if !wait.New(
wait.WithProto("tcp"),
wait.WithWait(200*time.Millisecond),
wait.WithBreak(50*time.Millisecond),
wait.WithDeadline(15*time.Second),
wait.WithDebug(true),
).Do([]string{"postgres:5432"}) {
logger.Error("db is not available")
return
}
Expand All @@ -34,6 +45,8 @@ if !wait.Do(
# Utility usage

```bash
net-wait-go

-addrs string
address:port
-deadline uint
Expand All @@ -50,13 +63,17 @@ if !wait.Do(
```bash
net-wait-go -addrs ya.ru:443 -debug true
2020/06/30 18:07:38 ya.ru:443 is OK

return code is 0
```

## 2 addresses check
```bash
net-wait-go -addrs ya.ru:443,yandex.ru:443 -debug true
2020/06/30 18:09:24 yandex.ru:443 is OK
2020/06/30 18:09:24 ya.ru:443 is OK

return code is 0
```

## 2 addresses check fail
Expand All @@ -65,6 +82,7 @@ net-wait-go -addrs ya.ru:445,yandex.ru:445 -debug true
2020/06/30 18:09:24 yandex.ru:443 is FAILED
2020/06/30 18:09:24 ya.ru:443 is is FAILED
...
return code is 1
```


27 changes: 17 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import (
func main() {
var proto string
flag.StringVar(&proto, "proto", "tcp", "tcp")

var addrs string
flag.StringVar(&addrs, "addrs", "", "address:port")

var deadlineMS uint
flag.UintVar(&deadlineMS, "deadline", 10000, "deadline in milliseconds")

var delayMS uint
flag.UintVar(&delayMS, "delay", 100, "delay in milliseconds")
flag.UintVar(&delayMS, "wait", 100, "delay of single request in milliseconds")

var breakMS uint
flag.UintVar(&breakMS, "delay", 50, "break between requests in milliseconds")

var debug bool
flag.BoolVar(&debug, "debug", false, "debug messages toggler")

Expand All @@ -32,18 +39,18 @@ func main() {
log.Println("addrs are not set")
flag.Usage()

os.Exit(1)
os.Exit(2)
}

if wait.Do(
proto,
addrsSlice,
time.Duration(delayMS)*time.Millisecond,
time.Duration(deadlineMS)*time.Millisecond,
debug,
) {
if wait.New(
wait.WithProto("tcp"),
wait.WithWait(time.Duration(delayMS)*time.Millisecond),
wait.WithBreak(time.Duration(breakMS)*time.Millisecond),
wait.WithDeadline(time.Duration(deadlineMS)*time.Millisecond),
wait.WithDebug(debug),
).Do(addrsSlice) {
return
}

os.Exit(2)
os.Exit(1)
}
79 changes: 73 additions & 6 deletions wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,73 @@ import (
"time"
)

func Do(proto string, addrs []string, delay, deadline time.Duration, debug bool) bool {
deadlineCh := time.After(deadline)
type Executor struct {
Proto string
Addrs []string
Wait time.Duration
Break time.Duration
Deadline time.Duration
Debug bool
}

type Option func(*Executor)

func New(opts ...Option) *Executor {
const (
defaultProto = "tcp"
defaultWait = 200 * time.Millisecond
defaultBreak = 50 * time.Millisecond
defaultDeadline = 15 * time.Second
defaultDebug = false
)

e := &Executor{
Proto: defaultProto,
Wait: defaultWait,
Break: defaultBreak,
Deadline: defaultDeadline,
Debug: defaultDebug,
}

for _, opt := range opts {
opt(e)
}

return e
}

func WithProto(proto string) Option {
return func(h *Executor) {
h.Proto = proto
}
}

func WithWait(wait time.Duration) Option {
return func(h *Executor) {
h.Wait = wait
}
}

func WithBreak(b time.Duration) Option {
return func(h *Executor) {
h.Break = b
}
}

func WithDeadline(deadline time.Duration) Option {
return func(h *Executor) {
h.Deadline = deadline
}
}

func WithDebug(debug bool) Option {
return func(h *Executor) {
h.Debug = debug
}
}

func (e *Executor) Do(addrs []string) bool {
deadlineCh := time.After(e.Deadline)
successCh := make(chan struct{})

wg := sync.WaitGroup{}
Expand All @@ -20,18 +85,20 @@ func Do(proto string, addrs []string, delay, deadline time.Duration, debug bool)
defer wg.Done()

for {
conn, err := net.DialTimeout(proto, addr, delay)
conn, err := net.DialTimeout(e.Proto, addr, e.Wait)
if err != nil {
if debug {
if e.Debug {
log.Printf("%s is FAILED", addr)
}

time.Sleep(delay)
if e.Break > 0 {
time.Sleep(e.Break)
}

continue
}

if debug {
if e.Debug {
log.Printf("%s is OK", addr)
}

Expand Down
6 changes: 3 additions & 3 deletions wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wait
import (
"net"
"testing"
"time"
)

func getServer(addr string) net.Listener {
Expand Down Expand Up @@ -31,11 +30,12 @@ func TestTCP(t *testing.T) {
srv := getServer(ok)
defer srv.Close()

if !Do("tcp", []string{ok}, time.Millisecond*100, time.Second, true) {
e := New(WithDebug(true))
if !e.Do([]string{ok}) {
t.FailNow()
}

if Do("tcp", []string{notok}, time.Millisecond*100, time.Second, true) {
if e.Do([]string{notok}) {
t.FailNow()
}
}

0 comments on commit c719ab9

Please sign in to comment.