From 106a6d05afdebb276374b6639011c743d27b045c Mon Sep 17 00:00:00 2001 From: Raqbit Date: Sat, 24 Jul 2021 20:38:17 +0200 Subject: [PATCH] Deprecate alternate Pinger constructors Callers should use WithContext & WithTimeout options instead --- _example/context-ping/main.go | 85 +++++++++++++++++----------------- _example/timed-example/main.go | 2 +- pinger.go | 16 +++---- 3 files changed, 50 insertions(+), 53 deletions(-) diff --git a/_example/context-ping/main.go b/_example/context-ping/main.go index 567d92a..82de1ad 100644 --- a/_example/context-ping/main.go +++ b/_example/context-ping/main.go @@ -1,42 +1,43 @@ -package main - -import ( - "context" - "fmt" - "log" - "os" - "os/signal" - "syscall" - "time" - - mcpinger "github.com/Raqbit/mc-pinger" -) - -func main() { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - - sig := make(chan os.Signal) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM) - go func() { - <-sig - // NOW cancel - fmt.Println("aborting due to interrupt...") - cancel() - }() - - // Create new Pinger with 10 seconds Timeout - pinger := mcpinger.NewContext(ctx, "mc.herobone.de", 25565) - // Get server info - info, err := pinger.Ping() - - if err != nil { - log.Println(err) - return - } - - // Print server info - fmt.Printf("Description: \"%s\"\n", info.Description.Text) - fmt.Printf("Online: %d/%d\n", info.Players.Online, info.Players.Max) - fmt.Printf("Version: %s\n", info.Version.Name) -} +package main + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + "syscall" + "time" + + mcpinger "github.com/Raqbit/mc-pinger" +) + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + sig := make(chan os.Signal) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM) + go func() { + <-sig + // NOW cancel + fmt.Println("aborting due to interrupt...") + cancel() + }() + + // Create new Pinger with 10 seconds Timeout + pinger := mcpinger.New("mc.herobone.de", 25565, mcpinger.WithContext(ctx)) + + // Get server info + info, err := pinger.Ping() + + if err != nil { + log.Println(err) + return + } + + // Print server info + fmt.Printf("Description: \"%s\"\n", info.Description.Text) + fmt.Printf("Online: %d/%d\n", info.Players.Online, info.Players.Max) + fmt.Printf("Version: %s\n", info.Version.Name) +} diff --git a/_example/timed-example/main.go b/_example/timed-example/main.go index d53a196..f5b47c0 100644 --- a/_example/timed-example/main.go +++ b/_example/timed-example/main.go @@ -10,7 +10,7 @@ import ( func main() { // Create new Pinger with 10 seconds Timeout - pinger := mcpinger.NewTimed("mc.herobone.de", 25565, 10*time.Second) + pinger := mcpinger.New("mc.herobone.de", 25565, mcpinger.WithTimeout(10*time.Second)) // Get server info info, err := pinger.Ping() diff --git a/pinger.go b/pinger.go index b8e1982..f25d7f5 100644 --- a/pinger.go +++ b/pinger.go @@ -190,21 +190,17 @@ func New(host string, port uint16, options ...McPingerOption) Pinger { // NewTimed Creates a new Pinger with specified host & port // to connect to a minecraft server with Timeout +// +// Deprecated: Use the WithTimeout option & New instead func NewTimed(host string, port uint16, timeout time.Duration) Pinger { - return &mcPinger{ - Host: host, - Port: port, - Timeout: timeout, - } + return New(host, port, WithTimeout(timeout)) } // NewContext Creates a new Pinger with the given Context +// +// Deprecated: Use the WithContext option & New instead func NewContext(ctx context.Context, host string, port uint16) Pinger { - return &mcPinger{ - Host: host, - Port: port, - Context: ctx, - } + return New(host, port, WithContext(ctx)) } // McPingerOption instances can be combined when creating a new Pinger