Compare commits

..

4 Commits

Author SHA1 Message Date
Matthew Penner
99ed8dc9a9 websocket: remove channel buffers 2022-01-22 12:30:07 -07:00
Matthew Penner
86f41c8027 server: remove Push timeout from sinkPool 2022-01-22 12:29:00 -07:00
Matthew Penner
766692bfe6 websocket: add small buffer to listener channels 2022-01-22 10:16:56 -07:00
Matthew Penner
764aed89ae events: remove waitgroup on Publish, add Destroy test 2022-01-22 10:14:54 -07:00
36 changed files with 236 additions and 897 deletions

View File

@@ -32,20 +32,17 @@ jobs:
go env go env
printf "\n\nSystem Environment:\n\n" printf "\n\nSystem Environment:\n\n"
env env
printf "Git Version: $(git version)\n\n"
echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}" echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}"
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)" echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
echo "::set-output name=go_cache::$(go env GOCACHE)" echo "::set-output name=go_cache::$(go env GOCACHE)"
echo "::set-output name=go_mod_cache::$(go env GOMODCACHE)"
- name: Build Cache - name: Build Cache
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
key: ${{ runner.os }}-go${{ matrix.go }}-${{ hashFiles('**/go.sum') }} path: ${{ steps.env.outputs.go_cache }}
key: ${{ runner.os }}-${{ matrix.go }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: | restore-keys: |
${{ runner.os }}-go${{ matrix.go }}- ${{ runner.os }}-${{ matrix.go }}-go
path: |
${{ steps.env.outputs.go_cache }}
${{ steps.env.outputs.go_mod_cache }}
- name: Get Dependencies - name: Get Dependencies
run: | run: |
go get -v -t -d ./... go get -v -t -d ./...
@@ -59,10 +56,8 @@ jobs:
go build -v -trimpath -ldflags="-s -w -X ${SRC_PATH}/system.Version=dev-${GIT_COMMIT:0:7}" -o build/wings_${{ matrix.goos }}_${{ matrix.goarch }} wings.go go build -v -trimpath -ldflags="-s -w -X ${SRC_PATH}/system.Version=dev-${GIT_COMMIT:0:7}" -o build/wings_${{ matrix.goos }}_${{ matrix.goarch }} wings.go
upx build/wings_${{ matrix.goos }}_${{ matrix.goarch }} upx build/wings_${{ matrix.goos }}_${{ matrix.goarch }}
chmod +x build/wings_${{ matrix.goos }}_${{ matrix.goarch }} chmod +x build/wings_${{ matrix.goos }}_${{ matrix.goarch }}
- name: Tests - name: Test
run: go test ./... run: go test ./...
- name: Tests (Race)
run: go test -race ./...
- name: Upload Artifact - name: Upload Artifact
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
if: ${{ github.ref == 'refs/heads/develop' || github.event_name == 'pull_request' }} if: ${{ github.ref == 'refs/heads/develop' || github.event_name == 'pull_request' }}

View File

@@ -1,15 +1,5 @@
# Changelog # Changelog
## v1.5.6
### Fixed
* Rewrote handler logic for the power actions lock to hopefully address issues people have been having when a server crashes and they're unable to start it again until restarting Wings.
* Fixes files uploaded with SFTP not being owned by the Pterodactyl user.
* Fixes excessive memory usage when large lines are sent through the console event handler.
### Changed
* Replaced usage of `encoding/json` throughout the codebase with a more performant encoder (`goccy/go-json`) to hopefully improve overall performance for JSON operations.
* Added custom `ContainerInspect` function to handle direct calls to Docker's CLI and make use of the new JSON encoder logic. This should reduce the total number of memory allocations and be more performant overall in a hot pathway.
## v1.5.5 ## v1.5.5
### Fixed ### Fixed
* Fixes sending to a closed channel when sending server logs over the websocket * Fixes sending to a closed channel when sending server logs over the websocket

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -13,7 +14,6 @@ import (
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal" "github.com/AlecAivazis/survey/v2/terminal"
"github.com/goccy/go-json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -19,7 +20,6 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/parsers/kernel" "github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/parsers/operatingsystem" "github.com/docker/docker/pkg/parsers/operatingsystem"
"github.com/goccy/go-json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"

View File

@@ -2,10 +2,10 @@ package config
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json"
"sort" "sort"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/goccy/go-json"
) )
type dockerNetworkInterfaces struct { type dockerNetworkInterfaces struct {
@@ -75,8 +75,6 @@ type DockerConfiguration struct {
// Overhead controls the memory overhead given to all containers to circumvent certain // Overhead controls the memory overhead given to all containers to circumvent certain
// software such as the JVM not staying below the maximum memory limit. // software such as the JVM not staying below the maximum memory limit.
Overhead Overhead `json:"overhead" yaml:"overhead"` Overhead Overhead `json:"overhead" yaml:"overhead"`
UsePerformantInspect bool `default:"true" json:"use_performant_inspect" yaml:"use_performant_inspect"`
} }
// RegistryConfiguration defines the authentication credentials for a given // RegistryConfiguration defines the authentication credentials for a given

View File

@@ -1,116 +0,0 @@
package docker
import (
"context"
"io"
"net/http"
"reflect"
"strings"
"sync"
"emperror.dev/errors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/config"
)
var (
o sync.Once
cli cliSettings
fastEnabled bool
)
type cliSettings struct {
enabled bool
proto string
host string
scheme string
version string
}
func configure(c *client.Client) {
o.Do(func() {
fastEnabled = config.Get().Docker.UsePerformantInspect
r := reflect.ValueOf(c).Elem()
cli.proto = r.FieldByName("proto").String()
cli.host = r.FieldByName("addr").String()
cli.scheme = r.FieldByName("scheme").String()
cli.version = r.FieldByName("version").String()
})
}
// ContainerInspect is a rough equivalent of Docker's client.ContainerInspect()
// but re-written to use a more performant JSON decoder. This is important since
// a large number of requests to this endpoint are spawned by Wings, and the
// standard "encoding/json" shows its performance woes badly even with single
// containers running.
func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error) {
configure(e.client)
// Support feature flagging of this functionality so that if something goes
// wrong for now it is easy enough for people to switch back to the older method
// of fetching stats.
if !fastEnabled {
return e.client.ContainerInspect(ctx, e.Id)
}
var st types.ContainerJSON
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/containers/"+e.Id+"/json", nil)
if err != nil {
return st, errors.WithStack(err)
}
if cli.proto == "unix" || cli.proto == "npipe" {
req.Host = "docker"
}
req.URL.Host = cli.host
req.URL.Scheme = cli.scheme
res, err := e.client.HTTPClient().Do(req)
if err != nil {
return st, errdefs.FromStatusCode(err, res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return st, errors.Wrap(err, "failed to read response body from Docker")
}
if err := parseErrorFromResponse(res, body); err != nil {
return st, errdefs.FromStatusCode(err, res.StatusCode)
}
if err := json.Unmarshal(body, &st); err != nil {
return st, errors.WithStack(err)
}
return st, nil
}
// parseErrorFromResponse is a re-implementation of Docker's
// client.checkResponseErr() function.
func parseErrorFromResponse(res *http.Response, body []byte) error {
if res.StatusCode >= 200 && res.StatusCode < 400 {
return nil
}
var ct string
if res.Header != nil {
ct = res.Header.Get("Content-Type")
}
var emsg string
if (cli.version == "" || versions.GreaterThan(cli.version, "1.23")) && ct == "application/json" {
var errResp types.ErrorResponse
if err := json.Unmarshal(body, &errResp); err != nil {
return errors.WithStack(err)
}
emsg = strings.TrimSpace(errResp.Message)
} else {
emsg = strings.TrimSpace(string(body))
}
return errors.Wrap(errors.New(emsg), "Error response from daemon")
}

View File

@@ -3,6 +3,7 @@ package docker
import ( import (
"bufio" "bufio"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@@ -11,7 +12,6 @@ import (
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/buger/jsonparser"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
@@ -49,12 +49,10 @@ func (e *Environment) Attach(ctx context.Context) error {
if e.IsAttached() { if e.IsAttached() {
return nil return nil
} }
e.log().Debug("not attached to container, continuing with attach...")
if err := e.followOutput(); err != nil { if err := e.followOutput(); err != nil {
return err return err
} }
e.log().Debug("following container output")
opts := types.ContainerAttachOptions{ opts := types.ContainerAttachOptions{
Stdin: true, Stdin: true,
@@ -64,13 +62,11 @@ func (e *Environment) Attach(ctx context.Context) error {
} }
// Set the stream again with the container. // Set the stream again with the container.
e.log().Debug("attempting to attach...")
if st, err := e.client.ContainerAttach(ctx, e.Id, opts); err != nil { if st, err := e.client.ContainerAttach(ctx, e.Id, opts); err != nil {
return err return err
} else { } else {
e.SetStream(&st) e.SetStream(&st)
} }
e.log().Debug("attached!")
go func() { go func() {
// Don't use the context provided to the function, that'll cause the polling to // Don't use the context provided to the function, that'll cause the polling to
@@ -122,7 +118,7 @@ func (e *Environment) InSituUpdate() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel() defer cancel()
if _, err := e.ContainerInspect(ctx); err != nil { if _, err := e.client.ContainerInspect(ctx, e.Id); err != nil {
// If the container doesn't exist for some reason there really isn't anything // If the container doesn't exist for some reason there really isn't anything
// we can do to fix that in this process (it doesn't make sense at least). In those // we can do to fix that in this process (it doesn't make sense at least). In those
// cases just return without doing anything since we still want to save the configuration // cases just return without doing anything since we still want to save the configuration
@@ -154,7 +150,7 @@ func (e *Environment) Create() error {
// If the container already exists don't hit the user with an error, just return // If the container already exists don't hit the user with an error, just return
// the current information about it which is what we would do when creating the // the current information about it which is what we would do when creating the
// container anyways. // container anyways.
if _, err := e.ContainerInspect(context.Background()); err == nil { if _, err := e.client.ContainerInspect(context.Background(), e.Id); err == nil {
return nil return nil
} else if !client.IsErrNotFound(err) { } else if !client.IsErrNotFound(err) {
return errors.Wrap(err, "environment/docker: failed to inspect container") return errors.Wrap(err, "environment/docker: failed to inspect container")
@@ -368,6 +364,11 @@ func (e *Environment) scanOutput(reader io.ReadCloser) {
go e.followOutput() go e.followOutput()
} }
type imagePullStatus struct {
Status string `json:"status"`
Progress string `json:"progress"`
}
// Pulls the image from Docker. If there is an error while pulling the image // Pulls the image from Docker. If there is an error while pulling the image
// from the source but the image already exists locally, we will report that // from the source but the image already exists locally, we will report that
// error to the logger but continue with the process. // error to the logger but continue with the process.
@@ -453,11 +454,12 @@ func (e *Environment) ensureImageExists(image string) error {
scanner := bufio.NewScanner(out) scanner := bufio.NewScanner(out)
for scanner.Scan() { for scanner.Scan() {
b := scanner.Bytes() s := imagePullStatus{}
status, _ := jsonparser.GetString(b, "status") fmt.Println(scanner.Text())
progress, _ := jsonparser.GetString(b, "progress")
e.Events().Publish(environment.DockerImagePullStatus, status+" "+progress) if err := json.Unmarshal(scanner.Bytes(), &s); err == nil {
e.Events().Publish(environment.DockerImagePullStatus, s.Status+" "+s.Progress)
}
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {

View File

@@ -10,6 +10,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/events" "github.com/pterodactyl/wings/events"
"github.com/pterodactyl/wings/remote" "github.com/pterodactyl/wings/remote"
@@ -115,7 +116,7 @@ func (e *Environment) Events() *events.Bus {
// will work fine when using the container name as the lookup parameter in addition to the longer // will work fine when using the container name as the lookup parameter in addition to the longer
// ID auto-assigned when the container is created. // ID auto-assigned when the container is created.
func (e *Environment) Exists() (bool, error) { func (e *Environment) Exists() (bool, error) {
_, err := e.ContainerInspect(context.Background()) _, err := e.client.ContainerInspect(context.Background(), e.Id)
if err != nil { if err != nil {
// If this error is because the container instance wasn't found via Docker we // If this error is because the container instance wasn't found via Docker we
// can safely ignore the error and just return false. // can safely ignore the error and just return false.
@@ -139,7 +140,7 @@ func (e *Environment) Exists() (bool, error) {
// //
// @see docker/client/errors.go // @see docker/client/errors.go
func (e *Environment) IsRunning(ctx context.Context) (bool, error) { func (e *Environment) IsRunning(ctx context.Context) (bool, error) {
c, err := e.ContainerInspect(ctx) c, err := e.client.ContainerInspect(ctx, e.Id)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -149,7 +150,7 @@ func (e *Environment) IsRunning(ctx context.Context) (bool, error) {
// Determine the container exit state and return the exit code and whether or not // Determine the container exit state and return the exit code and whether or not
// the container was killed by the OOM killer. // the container was killed by the OOM killer.
func (e *Environment) ExitState() (uint32, bool, error) { func (e *Environment) ExitState() (uint32, bool, error) {
c, err := e.ContainerInspect(context.Background()) c, err := e.client.ContainerInspect(context.Background(), e.Id)
if err != nil { if err != nil {
// I'm not entirely sure how this can happen to be honest. I tried deleting a // I'm not entirely sure how this can happen to be honest. I tried deleting a
// container _while_ a server was running and wings gracefully saw the crash and // container _while_ a server was running and wings gracefully saw the crash and

View File

@@ -66,7 +66,7 @@ func (e *Environment) Start(ctx context.Context) error {
} }
}() }()
if c, err := e.ContainerInspect(ctx); err != nil { if c, err := e.client.ContainerInspect(ctx, e.Id); err != nil {
// Do nothing if the container is not found, we just don't want to continue // Do nothing if the container is not found, we just don't want to continue
// to the next block of code here. This check was inlined here to guard against // to the next block of code here. This check was inlined here to guard against
// a nil-pointer when checking c.State below. // a nil-pointer when checking c.State below.
@@ -111,19 +111,14 @@ func (e *Environment) Start(ctx context.Context) error {
actx, cancel := context.WithTimeout(ctx, time.Second*30) actx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel() defer cancel()
if err := e.Attach(actx); err != nil {
return err
}
e.log().Debug("attempting to start container...")
if err := e.client.ContainerStart(actx, e.Id, types.ContainerStartOptions{}); err != nil { if err := e.client.ContainerStart(actx, e.Id, types.ContainerStartOptions{}); err != nil {
return errors.WrapIf(err, "environment/docker: failed to start container") return errors.WrapIf(err, "environment/docker: failed to start container")
} }
e.log().Debug("started container!")
// No errors, good to continue through. // No errors, good to continue through.
sawError = false sawError = false
return nil
return e.Attach(actx)
} }
// Stop stops the container that the server is running in. This will allow up to // Stop stops the container that the server is running in. This will allow up to
@@ -240,7 +235,7 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
// Terminate forcefully terminates the container using the signal provided. // Terminate forcefully terminates the container using the signal provided.
func (e *Environment) Terminate(signal os.Signal) error { func (e *Environment) Terminate(signal os.Signal) error {
c, err := e.ContainerInspect(context.Background()) c, err := e.client.ContainerInspect(context.Background(), e.Id)
if err != nil { if err != nil {
// Treat missing containers as an okay error state, means it is obviously // Treat missing containers as an okay error state, means it is obviously
// already terminated at this point. // already terminated at this point.

View File

@@ -2,13 +2,13 @@ package docker
import ( import (
"context" "context"
"encoding/json"
"io" "io"
"math" "math"
"time" "time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
) )
@@ -16,7 +16,7 @@ import (
// Uptime returns the current uptime of the container in milliseconds. If the // Uptime returns the current uptime of the container in milliseconds. If the
// container is not currently running this will return 0. // container is not currently running this will return 0.
func (e *Environment) Uptime(ctx context.Context) (int64, error) { func (e *Environment) Uptime(ctx context.Context) (int64, error) {
ins, err := e.ContainerInspect(ctx) ins, err := e.client.ContainerInspect(ctx, e.Id)
if err != nil { if err != nil {
return 0, errors.Wrap(err, "environment: could not inspect container") return 0, errors.Wrap(err, "environment: could not inspect container")
} }

View File

@@ -104,17 +104,13 @@ func (b *Bus) Publish(topic string, data interface{}) {
return return
} }
var wg sync.WaitGroup
event := Event{Topic: topic, Data: data} event := Event{Topic: topic, Data: data}
for _, listener := range listeners { for _, listener := range listeners {
l := listener l := listener
wg.Add(1)
go func(l Listener, event Event) { go func(l Listener, event Event) {
defer wg.Done()
l <- event l <- event
}(l, event) }(l, event)
} }
wg.Wait()
} }
// Destroy destroys the Event Bus by unregistering and closing all listeners. // Destroy destroys the Event Bus by unregistering and closing all listeners.

View File

@@ -168,3 +168,58 @@ func TestBus_Publish(t *testing.T) {
}) })
}) })
} }
func TestBus_Destroy(t *testing.T) {
g := Goblin(t)
g.Describe("Destroy", func() {
g.It("unsubscribes and closes all listeners", func() {
bus := NewBus()
listener := make(chan Event)
bus.On(listener, "test")
done := make(chan struct{}, 1)
go func() {
select {
case m := <-listener:
g.Assert(m).IsZero()
case <-time.After(1 * time.Second):
g.Fail("listener did not receive message in time")
}
done <- struct{}{}
}()
bus.Destroy()
<-done
g.Assert(bus.listeners).Equal(map[string][]Listener{})
})
// This is a check that ensures Destroy only closes each listener
// channel once, even if it is subscribed to multiple topics.
//
// Closing a channel multiple times will cause a runtime panic, which
// I'm pretty sure we don't want.
g.It("unsubscribes and closes channel only once", func() {
bus := NewBus()
listener := make(chan Event)
bus.On(listener, "test", "test2", "test3", "test4", "test5")
done := make(chan struct{}, 1)
go func() {
select {
case m := <-listener:
g.Assert(m).IsZero()
case <-time.After(1 * time.Second):
g.Fail("listener did not receive message in time")
}
done <- struct{}{}
}()
bus.Destroy()
<-done
g.Assert(bus.listeners).Equal(map[string][]Listener{})
})
})
}

5
go.mod
View File

@@ -45,10 +45,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )
require github.com/goccy/go-json v0.9.4
require golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect
require ( require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.0 // indirect github.com/Microsoft/go-winio v0.5.0 // indirect
@@ -106,6 +102,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect go.uber.org/multierr v1.7.0 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.6 // indirect golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect

5
go.sum
View File

@@ -371,8 +371,6 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k= github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k=
github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk= github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/goccy/go-json v0.9.4 h1:L8MLKG2mvVXiQu07qB6hmfqeSYQdOnqPot2GhsIwIaI=
github.com/goccy/go-json v0.9.4/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
@@ -1113,9 +1111,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=

View File

@@ -2,6 +2,7 @@ package parser
import ( import (
"bufio" "bufio"
"encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@@ -13,7 +14,6 @@ import (
"github.com/buger/jsonparser" "github.com/buger/jsonparser"
"github.com/icza/dyno" "github.com/icza/dyno"
"github.com/magiconair/properties" "github.com/magiconair/properties"
"github.com/goccy/go-json"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@@ -80,8 +80,8 @@ func (cp ConfigurationParser) String() string {
return string(cp) return string(cp)
} }
// ConfigurationFile defines a configuration file for the server startup. These // Defines a configuration file for the server startup. These will be looped over
// will be looped over and modified before the server finishes booting. // and modified before the server finishes booting.
type ConfigurationFile struct { type ConfigurationFile struct {
FileName string `json:"file"` FileName string `json:"file"`
Parser ConfigurationParser `json:"parser"` Parser ConfigurationParser `json:"parser"`
@@ -92,10 +92,12 @@ type ConfigurationFile struct {
configuration []byte configuration []byte
} }
// UnmarshalJSON is a custom unmarshaler for configuration files. If there is an // Custom unmarshaler for configuration files. If there is an error while parsing out the
// error while parsing out the replacements, don't fail the entire operation, // replacements, don't fail the entire operation, just log a global warning so someone can
// just log a global warning so someone can find the issue, and return an empty // find the issue, and return an empty array of replacements.
// array of replacements. //
// I imagine people will notice configuration replacement isn't working correctly and then
// the logs should help better expose that issue.
func (f *ConfigurationFile) UnmarshalJSON(data []byte) error { func (f *ConfigurationFile) UnmarshalJSON(data []byte) error {
var m map[string]*json.RawMessage var m map[string]*json.RawMessage
if err := json.Unmarshal(data, &m); err != nil { if err := json.Unmarshal(data, &m); err != nil {

View File

@@ -3,6 +3,7 @@ package remote
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -13,7 +14,6 @@ import (
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/system" "github.com/pterodactyl/wings/system"
) )

View File

@@ -1,11 +1,11 @@
package remote package remote
import ( import (
"encoding/json"
"regexp" "regexp"
"strings" "strings"
"github.com/apex/log" "github.com/apex/log"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/parser" "github.com/pterodactyl/wings/parser"
) )

View File

@@ -2,6 +2,7 @@ package downloader
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net" "net"
@@ -14,7 +15,6 @@ import (
"emperror.dev/errors" "emperror.dev/errors"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server"
) )

View File

@@ -52,8 +52,7 @@ func postServerPower(c *gin.Context) {
s := ExtractServer(c) s := ExtractServer(c)
var data struct { var data struct {
Action server.PowerAction `json:"action"` Action server.PowerAction `json:"action"`
WaitSeconds int `json:"wait_seconds"`
} }
if err := c.BindJSON(&data); err != nil { if err := c.BindJSON(&data); err != nil {
@@ -84,16 +83,12 @@ func postServerPower(c *gin.Context) {
// we can immediately return a response from the server. Some of these actions // we can immediately return a response from the server. Some of these actions
// can take quite some time, especially stopping or restarting. // can take quite some time, especially stopping or restarting.
go func(s *server.Server) { go func(s *server.Server) {
if data.WaitSeconds < 0 || data.WaitSeconds > 300 { if err := s.HandlePowerAction(data.Action, 30); err != nil {
data.WaitSeconds = 30
}
if err := s.HandlePowerAction(data.Action, data.WaitSeconds); err != nil {
if errors.Is(err, context.DeadlineExceeded) { if errors.Is(err, context.DeadlineExceeded) {
s.Log().WithField("action", data.Action).WithField("error", err).Warn("could not process server power action") s.Log().WithField("action", data.Action).
} else if errors.Is(err, server.ErrIsRunning) { Warn("could not acquire a lock while attempting to perform a power action")
// Do nothing, this isn't something we care about for logging,
} else { } else {
s.Log().WithFields(log.Fields{"action": data.Action, "wait_seconds": data.WaitSeconds, "error": err}). s.Log().WithFields(log.Fields{"action": data, "error": err}).
Error("encountered error processing a server power action in the background") Error("encountered error processing a server power action in the background")
} }
} }
@@ -187,7 +182,15 @@ func deleteServer(c *gin.Context) {
// Immediately suspend the server to prevent a user from attempting // Immediately suspend the server to prevent a user from attempting
// to start it while this process is running. // to start it while this process is running.
s.Config().SetSuspended(true) s.Config().SetSuspended(true)
s.CleanupForDestroy()
// Stop all running background tasks for this server that are using the context on
// the server struct. This will cancel any running install processes for the server
// as well.
s.CtxCancel()
s.Events().Destroy()
s.LogSink().Destroy()
s.InstallSink().Destroy()
s.Websockets().CancelAll()
// Remove any pending remote file downloads for the server. // Remove any pending remote file downloads for the server.
for _, dl := range downloader.ByServer(s.ID()) { for _, dl := range downloader.ByServer(s.ID()) {

View File

@@ -2,11 +2,11 @@ package router
import ( import (
"context" "context"
"encoding/json"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
ws "github.com/gorilla/websocket" ws "github.com/gorilla/websocket"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/router/middleware" "github.com/pterodactyl/wings/router/middleware"
"github.com/pterodactyl/wings/router/websocket" "github.com/pterodactyl/wings/router/websocket"

View File

@@ -1,13 +1,13 @@
package tokens package tokens
import ( import (
"encoding/json"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/apex/log" "github.com/apex/log"
"github.com/gbrlsnchs/jwt/v3" "github.com/gbrlsnchs/jwt/v3"
"github.com/goccy/go-json"
) )
// The time at which Wings was booted. No JWT's created before this time are allowed to // The time at which Wings was booted. No JWT's created before this time are allowed to

View File

@@ -2,12 +2,11 @@ package websocket
import ( import (
"context" "context"
"encoding/json"
"sync" "sync"
"time" "time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/events" "github.com/pterodactyl/wings/events"
"github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server"
) )
@@ -92,8 +91,8 @@ func (h *Handler) listenForServerEvents(ctx context.Context) error {
logOutput := make(chan []byte) logOutput := make(chan []byte)
installOutput := make(chan []byte) installOutput := make(chan []byte)
h.server.Events().On(eventChan, e...) h.server.Events().On(eventChan, e...)
h.server.Sink(server.LogSink).On(logOutput) h.server.LogSink().On(logOutput)
h.server.Sink(server.InstallSink).On(installOutput) h.server.InstallSink().On(installOutput)
onError := func(evt string, err2 error) { onError := func(evt string, err2 error) {
h.Logger().WithField("event", evt).WithField("error", err2).Error("failed to send event over server websocket") h.Logger().WithField("event", evt).WithField("error", err2).Error("failed to send event over server websocket")
@@ -149,8 +148,8 @@ func (h *Handler) listenForServerEvents(ctx context.Context) error {
// These functions will automatically close the channel if it hasn't been already. // These functions will automatically close the channel if it hasn't been already.
h.server.Events().Off(eventChan, e...) h.server.Events().Off(eventChan, e...)
h.server.Sink(server.LogSink).Off(logOutput) h.server.LogSink().Off(logOutput)
h.server.Sink(server.InstallSink).Off(installOutput) h.server.InstallSink().Off(installOutput)
// If the internal context is stopped it is either because the parent context // If the internal context is stopped it is either because the parent context
// got canceled or because we ran into an error. If the "err" variable is nil // got canceled or because we ran into an error. If the "err" variable is nil

View File

@@ -2,6 +2,7 @@ package websocket
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@@ -13,7 +14,6 @@ import (
"github.com/gbrlsnchs/jwt/v3" "github.com/gbrlsnchs/jwt/v3"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
@@ -267,11 +267,18 @@ func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
h.Unlock() h.Unlock()
} }
var actions = map[server.PowerAction]string{
server.PowerActionStart: PermissionSendPowerStart,
server.PowerActionStop: PermissionSendPowerStop,
server.PowerActionRestart: PermissionSendPowerRestart,
server.PowerActionTerminate: PermissionSendPowerStop,
}
// HandleInbound handles an inbound socket request and route it to the proper action. // HandleInbound handles an inbound socket request and route it to the proper action.
func (h *Handler) HandleInbound(ctx context.Context, m Message) error { func (h *Handler) HandleInbound(ctx context.Context, m Message) error {
if m.Event != AuthenticationEvent { if m.Event != AuthenticationEvent {
if err := h.TokenValid(); err != nil { if err := h.TokenValid(); err != nil {
h.unsafeSendJson(Message{ _ = h.unsafeSendJson(Message{
Event: JwtErrorEvent, Event: JwtErrorEvent,
Args: []string{err.Error()}, Args: []string{err.Error()},
}) })
@@ -339,12 +346,6 @@ func (h *Handler) HandleInbound(ctx context.Context, m Message) error {
{ {
action := server.PowerAction(strings.Join(m.Args, "")) action := server.PowerAction(strings.Join(m.Args, ""))
actions := make(map[server.PowerAction]string)
actions[server.PowerActionStart] = PermissionSendPowerStart
actions[server.PowerActionStop] = PermissionSendPowerStop
actions[server.PowerActionRestart] = PermissionSendPowerRestart
actions[server.PowerActionTerminate] = PermissionSendPowerStop
// Check that they have permission to perform this action if it is needed. // Check that they have permission to perform this action if it is needed.
if permission, exists := actions[action]; exists { if permission, exists := actions[action]; exists {
if !h.GetJwt().HasPermission(permission) { if !h.GetJwt().HasPermission(permission) {

View File

@@ -1,12 +1,12 @@
package filesystem package filesystem
import ( import (
"encoding/json"
"os" "os"
"strconv" "strconv"
"time" "time"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/goccy/go-json"
) )
type Stat struct { type Stat struct {

View File

@@ -507,9 +507,9 @@ func (ip *InstallationProcess) Execute() (string, error) {
return r.ID, nil return r.ID, nil
} }
// StreamOutput streams the output of the installation process to a log file in // Streams the output of the installation process to a log file in the server configuration
// the server configuration directory, as well as to a websocket listener so // directory, as well as to a websocket listener so that the process can be viewed in
// that the process can be viewed in the panel by administrators. // the panel by administrators.
func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error { func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error {
reader, err := ip.client.ContainerLogs(ctx, id, types.ContainerLogsOptions{ reader, err := ip.client.ContainerLogs(ctx, id, types.ContainerLogsOptions{
ShowStdout: true, ShowStdout: true,
@@ -521,7 +521,7 @@ func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) erro
} }
defer reader.Close() defer reader.Close()
err = system.ScanReader(reader, ip.Server.Sink(InstallSink).Push) err = system.ScanReader(reader, ip.Server.InstallSink().Push)
if err != nil { if err != nil {
ip.Server.Log().WithFields(log.Fields{"container_id": id, "error": err}).Warn("error processing install output lines") ip.Server.Log().WithFields(log.Fields{"container_id": id, "error": err}).Warn("error processing install output lines")
} }

View File

@@ -83,7 +83,7 @@ func (s *Server) processConsoleOutputEvent(v []byte) {
// If we are not throttled, go ahead and output the data. // If we are not throttled, go ahead and output the data.
if !t.Throttled() { if !t.Throttled() {
s.Sink(LogSink).Push(v) s.LogSink().Push(v)
} }
// Also pass the data along to the console output channel. // Also pass the data along to the console output channel.
@@ -125,7 +125,7 @@ func (s *Server) StartEventListeners() {
l.Trigger() l.Trigger()
} }
s.Events().Publish(StatsEvent, s.Proc()) s.emitProcUsage()
}() }()
case e := <-docker: case e := <-docker:
go func() { go func() {

View File

@@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
@@ -13,7 +14,6 @@ import (
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/gammazero/workerpool" "github.com/gammazero/workerpool"
"github.com/goccy/go-json"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"

View File

@@ -2,13 +2,12 @@ package server
import ( import (
"context" "context"
"fmt"
"os" "os"
"sync"
"time" "time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/google/uuid" "golang.org/x/sync/semaphore"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
) )
@@ -41,85 +40,19 @@ func (pa PowerAction) IsStart() bool {
return pa == PowerActionStart || pa == PowerActionRestart return pa == PowerActionStart || pa == PowerActionRestart
} }
type powerLocker struct { // ExecutingPowerAction checks if there is currently a power action being processed for the server.
mu sync.RWMutex
ch chan bool
}
func newPowerLocker() *powerLocker {
return &powerLocker{
ch: make(chan bool, 1),
}
}
type errPowerLockerLocked struct{}
func (e errPowerLockerLocked) Error() string {
return "cannot acquire a lock on the power state: already locked"
}
var ErrPowerLockerLocked error = errPowerLockerLocked{}
// IsLocked returns the current state of the locker channel. If there is
// currently a value in the channel, it is assumed to be locked.
func (pl *powerLocker) IsLocked() bool {
pl.mu.RLock()
defer pl.mu.RUnlock()
return len(pl.ch) == 1
}
// Acquire will acquire the power lock if it is not currently locked. If it is
// already locked, acquire will fail to acquire the lock, and will return false.
func (pl *powerLocker) Acquire() error {
pl.mu.Lock()
defer pl.mu.Unlock()
if len(pl.ch) == 1 {
return errors.WithStack(ErrPowerLockerLocked)
}
pl.ch <- true
return nil
}
// TryAcquire will attempt to acquire a power-lock until the context provided
// is canceled.
func (pl *powerLocker) TryAcquire(ctx context.Context) error {
select {
case pl.ch <- true:
return nil
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return errors.WithStack(err)
}
return nil
}
}
// Release will drain the locker channel so that we can properly re-acquire it
// at a later time.
func (pl *powerLocker) Release() {
pl.mu.Lock()
if len(pl.ch) == 1 {
<-pl.ch
}
pl.mu.Unlock()
}
// Destroy cleans up the power locker by closing the channel.
func (pl *powerLocker) Destroy() {
pl.mu.Lock()
if pl.ch != nil {
if len(pl.ch) == 1 {
<-pl.ch
}
close(pl.ch)
}
pl.mu.Unlock()
}
// ExecutingPowerAction checks if there is currently a power action being
// processed for the server.
func (s *Server) ExecutingPowerAction() bool { func (s *Server) ExecutingPowerAction() bool {
return s.powerLock.IsLocked() if s.powerLock == nil {
return false
}
ok := s.powerLock.TryAcquire(1)
if ok {
s.powerLock.Release(1)
}
// Remember, if we acquired a lock it means nothing was running.
return !ok
} }
// HandlePowerAction is a helper function that can receive a power action and then process the // HandlePowerAction is a helper function that can receive a power action and then process the
@@ -130,29 +63,22 @@ func (s *Server) ExecutingPowerAction() bool {
// function rather than making direct calls to the start/stop/restart functions on the // function rather than making direct calls to the start/stop/restart functions on the
// environment struct. // environment struct.
func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error { func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error {
if s.IsInstalling() || s.IsTransferring() || s.IsRestoring() { if s.IsInstalling() {
if s.IsRestoring() {
return ErrServerIsRestoring
} else if s.IsTransferring() {
return ErrServerIsTransferring
}
return ErrServerIsInstalling return ErrServerIsInstalling
} }
lockId, _ := uuid.NewUUID() if s.IsTransferring() {
log := s.Log().WithField("lock_id", lockId.String()).WithField("action", action) return ErrServerIsTransferring
cleanup := func() {
log.Info("releasing exclusive lock for power action")
s.powerLock.Release()
} }
var wait int if s.IsRestoring() {
if len(waitSeconds) > 0 && waitSeconds[0] > 0 { return ErrServerIsRestoring
wait = waitSeconds[0] }
if s.powerLock == nil {
s.powerLock = semaphore.NewWeighted(1)
} }
log.WithField("wait_seconds", wait).Debug("acquiring power action lock for instance")
// Only attempt to acquire a lock on the process if this is not a termination event. We want to // Only attempt to acquire a lock on the process if this is not a termination event. We want to
// just allow those events to pass right through for good reason. If a server is currently trying // just allow those events to pass right through for good reason. If a server is currently trying
// to process a power action but has gotten stuck you still should be able to pass through the // to process a power action but has gotten stuck you still should be able to pass through the
@@ -161,38 +87,33 @@ func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error
if action != PowerActionTerminate { if action != PowerActionTerminate {
// Determines if we should wait for the lock or not. If a value greater than 0 is passed // Determines if we should wait for the lock or not. If a value greater than 0 is passed
// into this function we will wait that long for a lock to be acquired. // into this function we will wait that long for a lock to be acquired.
if wait > 0 { if len(waitSeconds) > 0 && waitSeconds[0] != 0 {
ctx, cancel := context.WithTimeout(s.ctx, time.Second*time.Duration(wait)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(waitSeconds[0]))
defer cancel() defer cancel()
// Attempt to acquire a lock on the power action lock for up to 30 seconds. If more // Attempt to acquire a lock on the power action lock for up to 30 seconds. If more
// time than that passes an error will be propagated back up the chain and this // time than that passes an error will be propagated back up the chain and this
// request will be aborted. // request will be aborted.
if err := s.powerLock.TryAcquire(ctx); err != nil { if err := s.powerLock.Acquire(ctx, 1); err != nil {
return errors.Wrap(err, fmt.Sprintf("could not acquire lock on power action after %d seconds", wait)) return errors.WithMessage(err, "could not acquire lock on power state")
} }
} else { } else {
// If no wait duration was provided we will attempt to immediately acquire the lock // If no wait duration was provided we will attempt to immediately acquire the lock
// and bail out with a context deadline error if it is not acquired immediately. // and bail out with a context deadline error if it is not acquired immediately.
if err := s.powerLock.Acquire(); err != nil { if ok := s.powerLock.TryAcquire(1); !ok {
return errors.Wrap(err, "failed to acquire exclusive lock for power actions") return errors.WithMessage(context.DeadlineExceeded, "could not acquire lock on power state")
} }
} }
log.Info("acquired exclusive lock on power actions, processing event...") // Release the lock once the process being requested has finished executing.
defer cleanup() defer s.powerLock.Release(1)
} else { } else {
// Still try to acquire the lock if terminating, and it is available, just so that // Still try to acquire the lock if terminating, and it is available, just so that other power
// other power actions are blocked until it has completed. However, if it cannot be // actions are blocked until it has completed. However, if it is unavailable we won't stop
// acquired we won't stop the entire process. // the entire process.
// if ok := s.powerLock.TryAcquire(1); ok {
// If we did successfully acquire the lock, make sure we release it once we're done // If we managed to acquire the lock be sure to released it once this process is completed.
// executiong the power actions. defer s.powerLock.Release(1)
if err := s.powerLock.Acquire(); err == nil {
log.Info("acquired exclusive lock on power actions, processing event...")
defer cleanup()
} else {
log.Warn("failed to acquire exclusive lock, ignoring failure for termination event")
} }
} }

View File

@@ -1,158 +0,0 @@
package server
import (
"context"
"testing"
"time"
"emperror.dev/errors"
. "github.com/franela/goblin"
)
func TestPower(t *testing.T) {
g := Goblin(t)
g.Describe("PowerLocker", func() {
var pl *powerLocker
g.BeforeEach(func() {
pl = newPowerLocker()
})
g.Describe("PowerLocker#IsLocked", func() {
g.It("should return false when the channel is empty", func() {
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(pl.IsLocked()).IsFalse()
})
g.It("should return true when the channel is at capacity", func() {
pl.ch <- true
g.Assert(pl.IsLocked()).IsTrue()
<-pl.ch
g.Assert(pl.IsLocked()).IsFalse()
// We don't care what the channel value is, just that there is
// something in it.
pl.ch <- false
g.Assert(pl.IsLocked()).IsTrue()
g.Assert(cap(pl.ch)).Equal(1)
})
})
g.Describe("PowerLocker#Acquire", func() {
g.It("should acquire a lock when channel is empty", func() {
err := pl.Acquire()
g.Assert(err).IsNil()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(1)
})
g.It("should return an error when the channel is full", func() {
pl.ch <- true
err := pl.Acquire()
g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, ErrPowerLockerLocked)).IsTrue()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(1)
})
})
g.Describe("PowerLocker#TryAcquire", func() {
g.It("should acquire a lock when channel is empty", func() {
g.Timeout(time.Second)
err := pl.TryAcquire(context.Background())
g.Assert(err).IsNil()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(1)
g.Assert(pl.IsLocked()).IsTrue()
})
g.It("should block until context is canceled if channel is full", func() {
g.Timeout(time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer cancel()
pl.ch <- true
err := pl.TryAcquire(ctx)
g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, context.DeadlineExceeded)).IsTrue()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(1)
g.Assert(pl.IsLocked()).IsTrue()
})
g.It("should block until lock can be acquired", func() {
g.Timeout(time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
pl.Acquire()
go func() {
time.AfterFunc(time.Millisecond * 50, func() {
pl.Release()
})
}()
err := pl.TryAcquire(ctx)
g.Assert(err).IsNil()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(1)
g.Assert(pl.IsLocked()).IsTrue()
})
})
g.Describe("PowerLocker#Release", func() {
g.It("should release when channel is full", func() {
pl.Acquire()
g.Assert(pl.IsLocked()).IsTrue()
pl.Release()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(0)
g.Assert(pl.IsLocked()).IsFalse()
})
g.It("should release when channel is empty", func() {
g.Assert(pl.IsLocked()).IsFalse()
pl.Release()
g.Assert(cap(pl.ch)).Equal(1)
g.Assert(len(pl.ch)).Equal(0)
g.Assert(pl.IsLocked()).IsFalse()
})
})
g.Describe("PowerLocker#Destroy", func() {
g.It("should unlock and close the channel", func() {
pl.Acquire()
g.Assert(pl.IsLocked()).IsTrue()
pl.Destroy()
g.Assert(pl.IsLocked()).IsFalse()
defer func() {
r := recover()
g.Assert(r).IsNotNil()
g.Assert(r.(error).Error()).Equal("send on closed channel")
}()
pl.Acquire()
})
})
})
g.Describe("Server#ExecutingPowerAction", func() {
g.It("should return based on locker status", func() {
s := &Server{powerLock: newPowerLocker()}
g.Assert(s.ExecutingPowerAction()).IsFalse()
s.powerLock.Acquire()
g.Assert(s.ExecutingPowerAction()).IsTrue()
})
})
}

View File

@@ -50,3 +50,7 @@ func (ru *ResourceUsage) Reset() {
ru.Network.TxBytes = 0 ru.Network.TxBytes = 0
ru.Network.RxBytes = 0 ru.Network.RxBytes = 0
} }
func (s *Server) emitProcUsage() {
s.Events().Publish(StatsEvent, s.Proc())
}

View File

@@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@@ -11,7 +12,7 @@ import (
"emperror.dev/errors" "emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/goccy/go-json" "golang.org/x/sync/semaphore"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
@@ -31,7 +32,7 @@ type Server struct {
ctxCancel *context.CancelFunc ctxCancel *context.CancelFunc
emitterLock sync.Mutex emitterLock sync.Mutex
powerLock *powerLocker powerLock *semaphore.Weighted
throttleOnce sync.Once throttleOnce sync.Once
// Maintains the configuration for the server. This is the data that gets returned by the Panel // Maintains the configuration for the server. This is the data that gets returned by the Panel
@@ -70,8 +71,6 @@ type Server struct {
wsBag *WebsocketBag wsBag *WebsocketBag
wsBagLocker sync.Mutex wsBagLocker sync.Mutex
sinks map[SinkName]*sinkPool
logSink *sinkPool logSink *sinkPool
installSink *sinkPool installSink *sinkPool
} }
@@ -87,11 +86,9 @@ func New(client remote.Client) (*Server, error) {
installing: system.NewAtomicBool(false), installing: system.NewAtomicBool(false),
transferring: system.NewAtomicBool(false), transferring: system.NewAtomicBool(false),
restoring: system.NewAtomicBool(false), restoring: system.NewAtomicBool(false),
powerLock: newPowerLocker(),
sinks: map[SinkName]*sinkPool{ logSink: newSinkPool(),
LogSink: newSinkPool(), installSink: newSinkPool(),
InstallSink: newSinkPool(),
},
} }
if err := defaults.Set(&s); err != nil { if err := defaults.Set(&s); err != nil {
return nil, errors.Wrap(err, "server: could not set default values for struct") return nil, errors.Wrap(err, "server: could not set default values for struct")
@@ -103,17 +100,6 @@ func New(client remote.Client) (*Server, error) {
return &s, nil return &s, nil
} }
// CleanupForDestroy stops all running background tasks for this server that are
// using the context on the server struct. This will cancel any running install
// processes for the server as well.
func (s *Server) CleanupForDestroy() {
s.CtxCancel()
s.Events().Destroy()
s.DestroyAllSinks()
s.Websockets().CancelAll()
s.powerLock.Destroy()
}
// ID returns the UUID for the server instance. // ID returns the UUID for the server instance.
func (s *Server) ID() string { func (s *Server) ID() string {
return s.Config().GetUuid() return s.Config().GetUuid()
@@ -313,7 +299,7 @@ func (s *Server) OnStateChange() {
// views in the Panel correctly display 0. // views in the Panel correctly display 0.
if st == environment.ProcessOfflineState { if st == environment.ProcessOfflineState {
s.resources.Reset() s.resources.Reset()
s.Events().Publish(StatsEvent, s.Proc()) s.emitProcUsage()
} }
// If server was in an online state, and is now in an offline state we should handle // If server was in an online state, and is now in an offline state we should handle
@@ -369,3 +355,11 @@ func (s *Server) ToAPIResponse() APIResponse {
Configuration: *s.Config(), Configuration: *s.Config(),
} }
} }
func (s *Server) LogSink() *sinkPool {
return s.logSink
}
func (s *Server) InstallSink() *sinkPool {
return s.installSink
}

View File

@@ -4,114 +4,65 @@ import (
"sync" "sync"
) )
// SinkName represents one of the registered sinks for a server.
type SinkName string
const (
// LogSink handles console output for game servers, including messages being
// sent via Wings to the console instance.
LogSink SinkName = "log"
// InstallSink handles installation output for a server.
InstallSink SinkName = "install"
)
// sinkPool represents a pool with sinks. // sinkPool represents a pool with sinks.
type sinkPool struct { type sinkPool struct {
mu sync.RWMutex mx sync.RWMutex
sinks []chan []byte sinks []chan []byte
} }
// newSinkPool returns a new empty sinkPool. A sink pool generally lives with a // newSinkPool returns a new empty sinkPool.
// server instance for it's full lifetime.
func newSinkPool() *sinkPool { func newSinkPool() *sinkPool {
return &sinkPool{} return &sinkPool{}
} }
// On adds a channel to the sink pool instance. // Off removes a sink from the pool.
func (p *sinkPool) On(c chan []byte) {
p.mu.Lock()
p.sinks = append(p.sinks, c)
p.mu.Unlock()
}
// Off removes a given channel from the sink pool. If no matching sink is found
// this function is a no-op. If a matching channel is found, it will be removed.
func (p *sinkPool) Off(c chan []byte) { func (p *sinkPool) Off(c chan []byte) {
p.mu.Lock() p.mx.Lock()
defer p.mu.Unlock() defer p.mx.Unlock()
sinks := p.sinks sinks := p.sinks
for i, sink := range sinks { for i, sink := range sinks {
if c != sink { if c != sink {
continue continue
} }
// We need to maintain the order of the sinks in the slice we're tracking,
// so shift everything to the left, rather than changing the order of the
// elements.
copy(sinks[i:], sinks[i+1:]) copy(sinks[i:], sinks[i+1:])
sinks[len(sinks)-1] = nil sinks[len(sinks)-1] = nil
sinks = sinks[:len(sinks)-1] sinks = sinks[:len(sinks)-1]
p.sinks = sinks p.sinks = sinks
close(c)
// Avoid a panic if the sink channel is nil at this point.
if c != nil {
close(c)
}
return return
} }
} }
// Destroy destroys the pool by removing and closing all sinks and destroying // On adds a sink on the pool.
// all of the channels that are present. func (p *sinkPool) On(c chan []byte) {
p.mx.Lock()
defer p.mx.Unlock()
p.sinks = append(p.sinks, c)
}
// Destroy destroys the pool by removing and closing all sinks.
func (p *sinkPool) Destroy() { func (p *sinkPool) Destroy() {
p.mu.Lock() p.mx.Lock()
defer p.mu.Unlock() defer p.mx.Unlock()
for _, c := range p.sinks { for _, c := range p.sinks {
if c != nil { close(c)
close(c)
}
} }
p.sinks = nil p.sinks = nil
} }
// Push sends a given message to each of the channels registered in the pool. // Push pushes a message to all registered sinks.
func (p *sinkPool) Push(data []byte) { func (p *sinkPool) Push(v []byte) {
p.mu.RLock() p.mx.RLock()
// Attempt to send the data over to the channels. If the channel buffer is full,
// or otherwise blocked for some reason (such as being a nil channel), just discard
// the event data and move on to the next channel in the slice. If you don't
// implement the "default" on the select you'll block execution until the channel
// becomes unblocked, which is not what we want to do here.
for _, c := range p.sinks { for _, c := range p.sinks {
select { select {
case c <- data: // Send the log output to the channel
default: case c <- v:
} }
} }
p.mu.RUnlock() p.mx.RUnlock()
}
// Sink returns the instantiated and named sink for a server. If the sink has
// not been configured yet this function will cause a panic condition.
func (s *Server) Sink(name SinkName) *sinkPool {
sink, ok := s.sinks[name]
if !ok {
s.Log().Fatalf("attempt to access nil sink: %s", name)
}
return sink
}
// DestroyAllSinks iterates over all of the sinks configured for the server and
// destroys their instances. Note that this will cause a panic if you attempt
// to call Server.Sink() again after. This function is only used when a server
// is being deleted from the system.
func (s *Server) DestroyAllSinks() {
s.Log().Info("destroying all registered sinks for server instance")
for _, sink := range s.sinks {
sink.Destroy()
}
} }

View File

@@ -1,189 +0,0 @@
package server
import (
"reflect"
"sync"
"testing"
. "github.com/franela/goblin"
)
func MutexLocked(m *sync.RWMutex) bool {
v := reflect.ValueOf(m).Elem()
state := v.FieldByName("w").FieldByName("state")
return state.Int()&1 == 1 || v.FieldByName("readerCount").Int() > 0
}
func TestSink(t *testing.T) {
g := Goblin(t)
g.Describe("SinkPool#On", func() {
g.It("pushes additional channels to a sink", func() {
pool := &sinkPool{}
g.Assert(pool.sinks).IsZero()
c1 := make(chan []byte, 1)
pool.On(c1)
g.Assert(len(pool.sinks)).Equal(1)
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
})
g.Describe("SinkPool#Off", func() {
var pool *sinkPool
g.BeforeEach(func() {
pool = &sinkPool{}
})
g.It("works when no sinks are registered", func() {
ch := make(chan []byte, 1)
g.Assert(pool.sinks).IsZero()
pool.Off(ch)
g.Assert(pool.sinks).IsZero()
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
g.It("does not remove any sinks when the channel does not match", func() {
ch := make(chan []byte, 1)
ch2 := make(chan []byte, 1)
pool.On(ch)
g.Assert(len(pool.sinks)).Equal(1)
pool.Off(ch2)
g.Assert(len(pool.sinks)).Equal(1)
g.Assert(pool.sinks[0]).Equal(ch)
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
g.It("removes a channel and maintains the order", func() {
channels := make([]chan []byte, 8)
for i := 0; i < len(channels); i++ {
channels[i] = make(chan []byte, 1)
pool.On(channels[i])
}
g.Assert(len(pool.sinks)).Equal(8)
pool.Off(channels[2])
g.Assert(len(pool.sinks)).Equal(7)
g.Assert(pool.sinks[1]).Equal(channels[1])
g.Assert(pool.sinks[2]).Equal(channels[3])
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
g.It("does not panic if a nil channel is provided", func() {
ch := make([]chan []byte, 1)
defer func () {
if r := recover(); r != nil {
g.Fail("removing a nil channel should not cause a panic")
}
}()
pool.On(ch[0])
pool.Off(ch[0])
g.Assert(len(pool.sinks)).Equal(0)
})
})
g.Describe("SinkPool#Push", func() {
var pool *sinkPool
g.BeforeEach(func() {
pool = &sinkPool{}
})
g.It("works when no sinks are registered", func() {
g.Assert(len(pool.sinks)).IsZero()
pool.Push([]byte("test"))
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
g.It("sends data to every registered sink", func() {
ch1 := make(chan []byte, 1)
ch2 := make(chan []byte, 1)
pool.On(ch1)
pool.On(ch2)
g.Assert(len(pool.sinks)).Equal(2)
b := []byte("test")
pool.Push(b)
g.Assert(MutexLocked(&pool.mu)).IsFalse()
g.Assert(<-ch1).Equal(b)
g.Assert(<-ch2).Equal(b)
g.Assert(len(pool.sinks)).Equal(2)
})
g.It("does not block if a channel is nil or otherwise full", func() {
ch := make([]chan []byte, 2)
ch[1] = make(chan []byte, 1)
ch[1] <- []byte("test")
pool.On(ch[0])
pool.On(ch[1])
pool.Push([]byte("testing"))
g.Assert(MutexLocked(&pool.mu)).IsFalse()
g.Assert(<-ch[1]).Equal([]byte("test"))
pool.Push([]byte("test2"))
g.Assert(<-ch[1]).Equal([]byte("test2"))
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
})
g.Describe("SinkPool#Destroy", func() {
var pool *sinkPool
g.BeforeEach(func() {
pool = &sinkPool{}
})
g.It("works if no sinks are registered", func() {
pool.Destroy()
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
g.It("closes all channels fully", func() {
ch1 := make(chan []byte, 1)
ch2 := make(chan []byte, 1)
pool.On(ch1)
pool.On(ch2)
g.Assert(len(pool.sinks)).Equal(2)
pool.Destroy()
g.Assert(pool.sinks).IsZero()
defer func() {
r := recover()
g.Assert(r).IsNotNil()
g.Assert(r.(error).Error()).Equal("send on closed channel")
}()
ch1 <- []byte("test")
})
g.It("works when a sink channel is nil", func() {
ch := make([]chan []byte, 2)
pool.On(ch[0])
pool.On(ch[1])
pool.Destroy()
g.Assert(MutexLocked(&pool.mu)).IsFalse()
})
})
}

View File

@@ -119,9 +119,6 @@ func (h *Handler) Filewrite(request *sftp.Request) (io.WriterAt, error) {
l.WithField("flags", request.Flags).WithField("error", err).Error("failed to open existing file on system") l.WithField("flags", request.Flags).WithField("error", err).Error("failed to open existing file on system")
return nil, sftp.ErrSSHFxFailure return nil, sftp.ErrSSHFxFailure
} }
// Chown may or may not have been called in the touch function, so always do
// it at this point to avoid the file being improperly owned.
_ = h.server.Filesystem().Chown(request.Filepath)
return f, nil return f, nil
} }

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@@ -11,7 +12,6 @@ import (
"time" "time"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/goccy/go-json"
) )
var ( var (
@@ -19,11 +19,6 @@ var (
crr = []byte("\r\n") crr = []byte("\r\n")
) )
// The maximum size of the buffer used to send output over the console to
// clients. Once this length is reached, the line will be truncated and sent
// as is.
var maxBufferSize = 64 * 1024
// FirstNotEmpty returns the first string passed in that is not an empty value. // FirstNotEmpty returns the first string passed in that is not an empty value.
func FirstNotEmpty(v ...string) string { func FirstNotEmpty(v ...string) string {
for _, val := range v { for _, val := range v {
@@ -42,22 +37,12 @@ func MustInt(v string) int {
return i return i
} }
// ScanReader reads up to 64KB of line from the reader and emits that value
// over the websocket. If a line exceeds that size, it is truncated and only that
// amount is sent over.
func ScanReader(r io.Reader, callback func(line []byte)) error { func ScanReader(r io.Reader, callback func(line []byte)) error {
// Based on benchmarking this seems to be the best size for the reader buffer br := bufio.NewReader(r)
// to maintain fast enough workflows without hammering the CPU for allocations.
//
// Additionally, most games are outputting a high-frequency of smaller lines,
// rather than a bunch of massive lines. This allocation amount is the total
// number of bytes being output for each call to ReadLine() before it moves on
// to the next data pull.
br := bufio.NewReaderSize(r, 256)
// Avoid constantly re-allocating memory when we're flooding lines through this // Avoid constantly re-allocating memory when we're flooding lines through this
// function by using the same buffer for the duration of the call and just truncating // function by using the same buffer for the duration of the call and just truncating
// the value back to 0 every loop. // the value back to 0 every loop.
var buf bytes.Buffer buf := &bytes.Buffer{}
for { for {
buf.Reset() buf.Reset()
var err error var err error
@@ -67,54 +52,32 @@ func ScanReader(r io.Reader, callback func(line []byte)) error {
for { for {
// Read the line and write it to the buffer. // Read the line and write it to the buffer.
line, isPrefix, err = br.ReadLine() line, isPrefix, err = br.ReadLine()
// Certain games like Minecraft output absolutely random carriage returns in the output seemingly // Certain games like Minecraft output absolutely random carriage returns in the output seemingly
// in line with that it thinks is the terminal size. Those returns break a lot of output handling, // in line with that it thinks is the terminal size. Those returns break a lot of output handling,
// so we'll just replace them with proper new-lines and then split it later and send each line as // so we'll just replace them with proper new-lines and then split it later and send each line as
// its own event in the response. // its own event in the response.
line = bytes.Replace(line, cr, crr, -1) buf.Write(bytes.Replace(line, cr, crr, -1))
ns := buf.Len() + len(line) // Finish this loop and begin outputting the line if there is no prefix (the line fit into
// the default buffer), or if we hit the end of the line.
// If the length of the line value and the current value in the buffer will
// exceed the maximum buffer size, chop it down to hit the maximum size and
// then send that data over the socket before ending this loop.
//
// This ensures that we send as much data as possible, without allowing very
// long lines to grow the buffer size excessively and potentially DOS the Wings
// instance. If the line is not too long, just store the whole value into the
// buffer. This is kind of a re-implementation of the bufio.Scanner.Scan() logic
// without triggering an error when you exceed this buffer size.
if ns > maxBufferSize {
buf.Write(line[:len(line)-(ns-maxBufferSize)])
break
} else {
buf.Write(line)
}
// Finish this loop and begin outputting the line if there is no prefix
// (the line fit into the default buffer), or if we hit the end of the line.
if !isPrefix || err == io.EOF { if !isPrefix || err == io.EOF {
break break
} }
// If we encountered an error with something in ReadLine that was not an // If we encountered an error with something in ReadLine that was not an EOF just abort
// EOF just abort the entire process here. // the entire process here.
if err != nil { if err != nil {
return err return err
} }
} }
// Ensure that the scanner is always able to read the last line.
// Send the full buffer length over to the event handler to be emitted in _, _ = buf.Write([]byte("\r\n"))
// the websocket. The front-end can handle the linebreaks in the middle of // Publish the line for this loop. Break on new-line characters so every line is sent as a single
// the output, it simply expects that the end of the event emit is a newline. // output event, otherwise you get funky handling in the browser console.
if buf.Len() > 0 { s := bufio.NewScanner(buf)
// You need to make a copy of the buffer here because the callback will encounter for s.Scan() {
// a race condition since "buf.Bytes()" is going to be by-reference if passed directly. callback(s.Bytes())
c := make([]byte, buf.Len())
copy(c, buf.Bytes())
callback(c)
} }
// If the error we got previously that lead to the line being output is an io.EOF we want to
// If the error we got previously that lead to the line being output is // exit the entire looping process.
// an io.EOF we want to exit the entire looping process.
if err == io.EOF { if err == io.EOF {
break break
} }

View File

@@ -1,59 +0,0 @@
package system
import (
"math/rand"
"strings"
"testing"
"time"
. "github.com/franela/goblin"
)
func Test_Utils(t *testing.T) {
g := Goblin(t)
g.Describe("ScanReader", func() {
g.BeforeEach(func() {
maxBufferSize = 10
})
g.It("should truncate and return long lines", func() {
reader := strings.NewReader("hello world this is a long line\nof text that should be truncated\nnot here\nbut definitely on this line")
var lines []string
err := ScanReader(reader, func(line []byte) {
lines = append(lines, string(line))
})
g.Assert(err).IsNil()
g.Assert(lines).Equal([]string{"hello worl", "of text th", "not here", "but defini"})
})
g.It("should replace cariage returns with newlines", func() {
reader := strings.NewReader("test\rstring\r\nanother\rline\nhodor\r\r\rheld the door\nmaterial gourl\n")
var lines []string
err := ScanReader(reader, func(line []byte) {
lines = append(lines, string(line))
})
g.Assert(err).IsNil()
g.Assert(lines).Equal([]string{"test\rstrin", "another\rli", "hodor\r\r\rhe", "material g"})
})
})
}
func Benchmark_ScanReader(b *testing.B) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var str string
for i := 0; i < 10; i++ {
str += strings.Repeat("hello \rworld", r.Intn(2000)) + "\n"
}
reader := strings.NewReader(str)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ScanReader(reader, func(line []byte) {
// no op
})
}
}