Skip to content
Permalink
Browse files

Add support for overriding and detecting HSS timeouts (#6)

* Add support for overriding and detecting HSS timeouts

* go get -u ./...
  • Loading branch information...
slimsag committed Oct 3, 2019
1 parent 1205f5e commit e91d603ba4eb817e27912037f95306215b127873
Showing with 49 additions and 5 deletions.
  1. +13 −3 cmd/gosyntect/main.go
  2. +2 −1 go.mod
  3. +12 −0 go.sum
  4. +22 −1 gosyntect.go
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/sourcegraph/gosyntect"
)
@@ -43,11 +44,20 @@ func main() {
log.Fatal(err)
}

var stabilizeTimeout time.Duration
if v := os.Getenv("STABILIZE_TIMEOUT"); v != "" {
stabilizeTimeout, err = time.ParseDuration(v)
if err != nil {
log.Fatal(err)
}
}

cl := gosyntect.New(server)
resp, err := cl.Highlight(context.Background(), &gosyntect.Query{
Filepath: filepath.Base(file),
Theme: theme,
Code: string(data),
Filepath: filepath.Base(file),
Theme: theme,
Code: string(data),
StabilizeTimeout: stabilizeTimeout,
})
if err != nil {
log.Fatal(err)
3 go.mod
@@ -3,7 +3,8 @@ module github.com/sourcegraph/gosyntect
go 1.12

require (
github.com/opentracing-contrib/go-stdlib v0.0.0-20190510164024-2b2d2700a3b7
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0 // indirect
)
12 go.sum
@@ -1,6 +1,18 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/opentracing-contrib/go-stdlib v0.0.0-20190510164024-2b2d2700a3b7 h1:sRfr13qLClTOCZ8f2tFb0k9m1TZayEUz1fpWL38ocnE=
github.com/opentracing-contrib/go-stdlib v0.0.0-20190510164024-2b2d2700a3b7/go.mod h1:PLldrQSroqzH70Xl+1DQcGnefIbqsKR7UDaiux3zV+w=
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9 h1:QsgXACQhd9QJhEmRumbsMQQvBtmdS0mafoVEBplWXEg=
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9/go.mod h1:PLldrQSroqzH70Xl+1DQcGnefIbqsKR7UDaiux3zV+w=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing "github.com/opentracing/opentracing-go"
@@ -31,6 +32,14 @@ type Query struct {

// Code is the literal code to highlight.
Code string `json:"code"`

// StabilizeTimeout, if non-zero, overrides the default syntect_server
// http-server-stabilizer timeout of 10s. This is most useful when a user
// is requesting to highlight a very large file and is willing to wait
// longer, but it is important this not _always_ be a long duration because
// the worker's threads could get stuck at 100% CPU for this amount of
// time if the user's request ends up being a problematic one.
StabilizeTimeout time.Duration `json:"-"`
}

// Response represents a response to a code highlighting query.
@@ -54,6 +63,13 @@ var (
// most often occurs when Syntect does not support e.g. an obscure or
// relatively unused sublime-syntax feature and as a result panics.
ErrPanic = errors.New("syntect panic while highlighting")

// ErrHSSWorkerTimeout occurs when syntect_server's wrapper,
// http-server-stabilizer notices syntect_server is taking too long to
// serve a request, has most likely gotten stuck, and as such has been
// restarted. This occurs rarely on certain files syntect_server cannot yet
// handle for some reason.
ErrHSSWorkerTimeout = errors.New("HSS worker timeout while serving request")
)

type response struct {
@@ -83,6 +99,9 @@ func (c *Client) Highlight(ctx context.Context, q *Query) (*Response, error) {
return nil, errors.Wrap(err, "building request")
}
req.Header.Set("Content-Type", "application/json")
if q.StabilizeTimeout != 0 {
req.Header.Set("X-Stabilize-Timeout", q.StabilizeTimeout.String())
}

// Add tracing to the request.
req = req.WithContext(ctx)
@@ -103,7 +122,7 @@ func (c *Client) Highlight(ctx context.Context, q *Query) (*Response, error) {
return nil, ErrRequestTooLarge
}

// Can only call ht.Span() after the request has been exected, so add our span tags in now.
// Can only call ht.Span() after the request has been executed, so add our span tags in now.
ht.Span().SetTag("Filepath", q.Filepath)
ht.Span().SetTag("Theme", q.Theme)

@@ -123,6 +142,8 @@ func (c *Client) Highlight(ctx context.Context, q *Query) (*Response, error) {
err = errors.New("gosyntect internal error: resource_not_found")
case "panic":
err = ErrPanic
case "hss_worker_timeout":
err = ErrHSSWorkerTimeout
default:
err = fmt.Errorf("unknown error=%q code=%q", r.Error, r.Code)
}

0 comments on commit e91d603

Please sign in to comment.
You can’t perform that action at this time.