Skip to content

Commit

Permalink
Merge pull request #28 from spiral/feature/arguments
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
wolfy-j authored Jul 9, 2018
2 parents ad05629 + 21bd058 commit 47ae9f4
Show file tree
Hide file tree
Showing 33 changed files with 793 additions and 370 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
CHANGELOG
=========

v1.1.0 (80.07.2018)
-------
- bugfix: Wrong values for $_SERVER['REQUEST_TIME'] and $_SERVER['REQUEST_TIME_FLOAT']
- rr now resolves remoteAddr (ip-address)
- improvements in error buffer
- support for custom configs and dependency injection for services
- support for net/http native middlewares
- better debugger
- config pre-processing now allows second values for http service timeouts
- support for non serving services

v1.0.5 (30.06.2018)
-------
- docker compatible logging (forcing TTY output for logrus)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ http:
# maximum jobs per worker, 0 - unlimited.
maxJobs: 0

# for how long pool should attempt to allocate free worker (request timeout). Nanoseconds atm. (60s)
allocateTimeout: 60000000000
# for how long pool should attempt to allocate free worker (request timeout).
allocateTimeout: 60

# amount of time given to worker to gracefully destruct itself. Nanoseconds atm. (30s)
destroyTimeout: 30000000000
# amount of time given to worker to gracefully destruct itself.
destroyTimeout: 30

# static file serving.
static:
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cd $(dirname "${BASH_SOURCE[0]}")
OD="$(pwd)"
# Pushes application version into the build information.
RR_VERSION=1.0.5
RR_VERSION=1.1.0

# Hardcode some values to the core package
LDFLAGS="$LDFLAGS -X github.com/spiral/roadrunner/cmd/rr/cmd.Version=${RR_VERSION}"
Expand Down
8 changes: 4 additions & 4 deletions cmd/rr/.rr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ http:
# maximum jobs per worker, 0 - unlimited.
maxJobs: 0

# for how long worker is allowed to be bootstrapped. In nanoseconds :(
allocateTimeout: 600000000
# for how long worker is allowed to be bootstrapped.
allocateTimeout: 60

# amount of time given to worker to gracefully destruct itself. In nanoseconds :(
destroyTimeout: 600000000
# amount of time given to worker to gracefully destruct itself.
destroyTimeout: 60

# static file serving.
static:
Expand Down
3 changes: 2 additions & 1 deletion cmd/rr/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "time"

var (
// Version - defines build version.
Version = "development"
Version = "local"

// BuildTime - defined build time.
BuildTime = time.Now().Format(time.RFC1123)
)
53 changes: 39 additions & 14 deletions cmd/rr/debug/debugger.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package debug

import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/cmd/rr/utils"
"github.com/spiral/roadrunner/service/http"
rrhttp "github.com/spiral/roadrunner/service/http"
"net/http"
"strings"
)

Expand All @@ -20,21 +22,32 @@ type debugger struct{ logger *logrus.Logger }
func (s *debugger) listener(event int, ctx interface{}) {
// http events
switch event {
case http.EventResponse:
log := ctx.(*http.Event)
s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
case http.EventError:
log := ctx.(*http.Event)

if _, ok := log.Error.(roadrunner.JobError); ok {
s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
case rrhttp.EventResponse:
e := ctx.(*rrhttp.ResponseEvent)
s.logger.Info(utils.Sprintf(
"<cyan+h>%s</reset> %s <white+hb>%s</reset> %s",
e.Request.RemoteAddr,
statusColor(e.Response.Status),
e.Request.Method,
e.Request.URI,
))
case rrhttp.EventError:
e := ctx.(*rrhttp.ErrorEvent)

if _, ok := e.Error.(roadrunner.JobError); ok {
s.logger.Info(utils.Sprintf(
"%s <white+hb>%s</reset> %s",
statusColor(500),
e.Request.Method,
uri(e.Request),
))
} else {
s.logger.Info(utils.Sprintf(
"%s <white+hb>%s</reset> %s <red>%s</reset>",
statusColor(log.Status),
log.Method,
log.URI,
log.Error,
statusColor(500),
e.Request.Method,
uri(e.Request),
e.Error,
))
}
}
Expand All @@ -58,7 +71,10 @@ func (s *debugger) listener(event int, ctx interface{}) {
// outputs
switch event {
case roadrunner.EventStderrOutput:
s.logger.Warning(strings.Trim(string(ctx.([]byte)), "\r\n"))
s.logger.Warning(utils.Sprintf(
"<yellow>%s</reset>",
strings.Trim(string(ctx.([]byte)), "\r\n"),
))
}

// rr server events
Expand Down Expand Up @@ -93,3 +109,12 @@ func statusColor(status int) string {

return utils.Sprintf("<red>%v</reset>", status)
}

// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled).
func uri(r *http.Request) string {
if r.TLS != nil {
return fmt.Sprintf("https://%s%s", r.Host, r.URL.String())
}

return fmt.Sprintf("http://%s%s", r.Host, r.URL.String())
}
2 changes: 1 addition & 1 deletion cmd/rr/http/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {

func reloadHandler(cmd *cobra.Command, args []string) error {
svc, st := rr.Container.Get(rpc.ID)
if st < service.StatusConfigured {
if st < service.StatusOK {
return errors.New("RPC service is not configured")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/rr/http/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func workersHandler(cmd *cobra.Command, args []string) (err error) {
}()

svc, st := rr.Container.Get(rrpc.ID)
if st < service.StatusConfigured {
if st < service.StatusOK {
return errors.New("RPC service is not configured")
}

Expand Down
3 changes: 3 additions & 0 deletions error_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func newErrBuffer() *errBuffer {
if len(eb.buf) > eb.last {
if eb.lsn != nil {
eb.lsn(EventStderrOutput, eb.buf[eb.last:])
eb.buf = eb.buf[0:0]
}

eb.last = len(eb.buf)
}
eb.mu.Unlock()
Expand All @@ -55,6 +57,7 @@ func newErrBuffer() *errBuffer {
if eb.lsn != nil {
eb.lsn(EventStderrOutput, eb.buf[eb.last:])
}

eb.last = len(eb.buf)
}
eb.mu.Unlock()
Expand Down
15 changes: 13 additions & 2 deletions error_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func TestErrBuffer_Write_Event(t *testing.T) {
<-tr

// messages are read
assert.Equal(t, 6, buf.Len())
assert.Equal(t, "hello\n", buf.String())
assert.Equal(t, 0, buf.Len())
}

func TestErrBuffer_Write_Event_Separated(t *testing.T) {
Expand All @@ -50,6 +49,18 @@ func TestErrBuffer_Write_Event_Separated(t *testing.T) {
buf.Write([]byte("ending"))

<-tr
assert.Equal(t, 0, buf.Len())
assert.Equal(t, "", buf.String())
}

func TestErrBuffer_Write_Event_Separated_NoListener(t *testing.T) {
buf := newErrBuffer()
defer buf.Close()

buf.Write([]byte("hel"))
buf.Write([]byte("lo\n"))
buf.Write([]byte("ending"))

assert.Equal(t, 12, buf.Len())
assert.Equal(t, "hello\nending", buf.String())
}
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
module github.com/spiral/roadrunner

require (
github.com/BurntSushi/toml v0.3.0
github.com/StackExchange/wmi v0.0.0-20180412205111-cdffdb33acae
github.com/buger/goterm v0.0.0-20180423150900-6d19e6a8df12
github.com/davecgh/go-spew v1.1.0
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e
github.com/fsnotify/fsnotify v1.4.7
github.com/go-ole/go-ole v1.2.1
github.com/golang/protobuf v1.1.0
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/inconshreveable/mousetrap v1.0.0
github.com/magiconair/properties v1.8.0
github.com/mattn/go-colorable v0.0.9
github.com/mattn/go-isatty v0.0.3
github.com/mattn/go-runewidth v0.0.2
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/mitchellh/mapstructure v0.0.0-20180511142126-bb74f1db0675
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84
github.com/onsi/ginkgo v1.5.0
github.com/onsi/gomega v1.4.0
github.com/pelletier/go-toml v1.2.0
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0
github.com/shirou/gopsutil v0.0.0-20180613084040-c23bcca55e77
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4
github.com/sirupsen/logrus v1.0.5
github.com/spf13/afero v1.1.1
github.com/spf13/cast v1.2.0
Expand All @@ -23,8 +33,13 @@ require (
github.com/spf13/pflag v1.0.1
github.com/spf13/viper v1.0.2
github.com/spiral/goridge v0.0.0-20180607130832-0351012be508
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20180614221331-a8fb68e7206f
golang.org/x/net v0.0.0-20180709032641-4d581e05a3ac
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
golang.org/x/sys v0.0.0-20180615093615-8014b7b116a6
golang.org/x/text v0.3.0
gopkg.in/airbrake/gobrake.v2 v2.0.9
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2
gopkg.in/yaml.v2 v2.2.1
)
35 changes: 27 additions & 8 deletions php-src/PSR7Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function acceptRequest()
$bodyStream->write($body);
}

$_SERVER = $this->configureServer($ctx);

$request = new Diactoros\ServerRequest(
$_SERVER,
$this->wrapUploads($ctx['uploads']),
Expand Down Expand Up @@ -105,6 +107,23 @@ public function respond(ResponseInterface $response)
]));
}

/**
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
*
* @param array $ctx
* @return array
*/
protected function configureServer(array $ctx): array
{
$server = $_SERVER;
$server['REQUEST_TIME'] = time();
$server['REQUEST_TIME_FLOAT'] = microtime(true);
$server['REMOTE_ADDR'] = $ctx['attributes']['ipAddress'] ?? $ctx['remoteAddr'] ?? '127.0.0.1';

return $server;
}

/**
* Wraps all uploaded files with UploadedFile.
*
Expand All @@ -119,18 +138,18 @@ private function wrapUploads($files): array
}

$result = [];
foreach ($files as $index => $file) {
if (!isset($file['name'])) {
$result[$index] = $this->wrapUploads($file);
foreach ($files as $index => $f) {
if (!isset($f['name'])) {
$result[$index] = $this->wrapUploads($f);
continue;
}

$result[$index] = new Diactoros\UploadedFile(
$file['tmpName'],
$file['size'],
$file['error'],
$file['name'],
$file['mime']
$f['tmpName'],
$f['size'],
$f['error'],
$f['name'],
$f['mime']
);
}

Expand Down
Loading

0 comments on commit 47ae9f4

Please sign in to comment.