Skip to content

Commit

Permalink
cleanups and test stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
j-bro committed Nov 3, 2023
1 parent 7372f33 commit 276ab47
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 59 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

go-svrquery is a [Go](http://golang.org/) client for talking to game servers using various query protocols.

Features
Features
--------
* Support for various game server query protocol's including:
** SQP, TF2E, Prometheus
Expand Down Expand Up @@ -50,18 +50,18 @@ endpoint. For example:
client, err := svrquery.NewClient("prom", "http://127.0.0.1:9100/metrics")
```

The library will read the following metrics:
The library expects the following metrics to be available:

```text
# HELP gameserver_current_players Number of players currently connected to the server.
# TYPE gameserver_current_players gauge
gameserver_current_players 1
# HELP gameserver_max_players Maximum number of players that can connect to the server.
# TYPE gameserver_max_players gauge
gameserver_max_players 2
# HELP gameserver_server_info Server status info.
# TYPE gameserver_server_info gauge
gameserver_server_info{game_type="Game Type",map_name="Map",port="1000",server_name="Name"} 1
# HELP current_players Number of players currently connected to the server.
# TYPE current_players gauge
current_players 1
# HELP max_players Maximum number of players that can connect to the server.
# TYPE max_players gauge
max_players 2
# HELP server_info Server status info.
# TYPE server_info gauge
server_info{game_type="Game Type",map_name="Map",port="1000",server_name="Name"} 1
```

CLI
Expand Down
6 changes: 5 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -110,8 +111,11 @@ func server(l *log.Logger, proto, address string) error {
return fmt.Errorf("create responder: %w", err)
}

// TODO: implement cancellable context
ctx, _ := context.WithCancel(context.Background())

// this function will block until the transport is closed
err = transport.Start(responder)
err = transport.Start(ctx, responder)
if err != nil {
return fmt.Errorf("transport error")
}
Expand Down
25 changes: 13 additions & 12 deletions lib/svrquery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package svrquery
import (
"errors"
"fmt"
"io"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -36,15 +37,6 @@ type Client struct {
transport
}

type transport interface {
Setup() error
Address() string
Read(b []byte) (int, error)
Write(b []byte) (int, error)
Close() error
SetTimeout(time.Duration)
}

// WithKey sets the key used for request by for the client.
func WithKey(key string) Option {
return func(c *Client) error {
Expand All @@ -61,7 +53,7 @@ func WithTimeout(t time.Duration) Option {
}
}

// NewClient creates a new client that talks to address.
// NewClient creates a new client that talks to addr using proto.
func NewClient(proto, addr string, options ...Option) (*Client, error) {
f, err := protocol.Get(proto)
if err != nil {
Expand Down Expand Up @@ -98,8 +90,17 @@ func (c *Client) Query() (protocol.Responser, error) {
return c.Queryer.Query()
}

var _ transport = (*udpTransport)(nil)
var _ transport = (*httpTransport)(nil)
var (
_ transport = (*udpTransport)(nil)
_ transport = (*httpTransport)(nil)
)

type transport interface {
Setup() error
Address() string
SetTimeout(time.Duration)
io.ReadWriteCloser
}

type udpTransport struct {
address string
Expand Down
10 changes: 10 additions & 0 deletions lib/svrquery/protocol/prom/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package prom

import (
"testing"
)

func TestQuery(t *testing.T) {
// use httptest.NewServer()
t.Fatal("unimplemented")
}
2 changes: 1 addition & 1 deletion lib/svrquery/protocol/prom/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package prom

const (
metricNamespace = "gameserver_"
metricNamespace = "" // adjust this if we want to enforce a namespace/prefix for metrics
currentPlayersMetricName = metricNamespace + "current_players"
maxPlayersMetricName = metricNamespace + "max_players"
serverInfoMetricName = metricNamespace + "server_info"
Expand Down
2 changes: 1 addition & 1 deletion lib/svrsample/protocol/prom/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var _ prometheus.Collector = (*metrics)(nil)

const (
metricNamespace = "gameserver"
metricNamespace = ""
)

// metrics holds the current prometheus metrics data for the server
Expand Down
9 changes: 9 additions & 0 deletions lib/svrsample/protocol/prom/prom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package prom

import (
"testing"
)

func TestPrometheusServer(t *testing.T) {
t.Fatal("unimplemented")
}
28 changes: 0 additions & 28 deletions lib/svrsample/protocol/prom/server_info.go

This file was deleted.

3 changes: 1 addition & 2 deletions lib/svrsample/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package svrsample
import (
"errors"
"fmt"
"github.com/multiplay/go-svrquery/lib/svrsample/protocol/prom"

"github.com/multiplay/go-svrquery/lib/svrsample/common"

"github.com/multiplay/go-svrquery/lib/svrsample/protocol/prom"
"github.com/multiplay/go-svrquery/lib/svrsample/protocol/sqp"
)

Expand Down
13 changes: 10 additions & 3 deletions lib/svrsample/net.go → lib/svrsample/transport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package svrsample

import (
"context"
"errors"
"fmt"
"github.com/multiplay/go-svrquery/lib/svrsample/common"
Expand All @@ -16,8 +17,10 @@ var (
_ Transport = (*HTTPTransport)(nil)
)

// Transport is an abstraction of the metrics transport (UDP, HTTP, etc.)
type Transport interface {
Start(responder common.QueryResponder) error
// Start starts the transport and blocks until it is stopped
Start(context.Context, common.QueryResponder) error
}

type UDPTransport struct {
Expand All @@ -30,7 +33,9 @@ func NewUDPTransport(address string) UDPTransport {
return UDPTransport{address: address}
}

func (u UDPTransport) Start(responder common.QueryResponder) error {
func (u UDPTransport) Start(ctx context.Context, responder common.QueryResponder) error {
// TODO: do something with context

addr, err := net.ResolveUDPAddr("udp4", u.address)
if err != nil {
return fmt.Errorf("resolved udp: %w", err)
Expand Down Expand Up @@ -91,12 +96,14 @@ func NewHTTPTransport(address string) HTTPTransport {
return HTTPTransport{address: address}
}

func (h HTTPTransport) Start(responder common.QueryResponder) error {
func (h HTTPTransport) Start(ctx context.Context, responder common.QueryResponder) error {
promResponder, ok := responder.(*prom.QueryResponder)
if !ok {
return errors.New(fmt.Sprintf("bad responder type, expected prom.QueryResponder but got %T", responder))
}

// TODO: do something with context

listener, err := net.Listen("tcp", h.address)
if err != nil {
return fmt.Errorf("listen tcp: %w", err)
Expand Down
13 changes: 13 additions & 0 deletions lib/svrsample/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package svrsample

import (
"testing"
)

func TestUDPTransport(t *testing.T) {
t.Fatal("unimplemented")
}

func TestHTTPTransport(t *testing.T) {
t.Fatal("unimplemented")
}

0 comments on commit 276ab47

Please sign in to comment.