Skip to content

Commit

Permalink
rebase with main
Browse files Browse the repository at this point in the history
  • Loading branch information
rsafonseca committed Aug 2, 2024
2 parents 3e429b2 + 75dae29 commit 3664bf0
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 483 deletions.
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
]
extensions = ['myst_parser']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
5 changes: 4 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
sphinx-markdown-tables==0.0.3
sphinx-markdown-tables==0.0.17
recommonmark==0.7.1
sphinx-rtd-theme==2.0.0
myst-parser==3.0.1
611 changes: 135 additions & 476 deletions go.work.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions xk6-pitaya/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the k6 binary with the extension
FROM golang:1.20 as builder
FROM golang:1.22 as builder

ARG pitaya_revision

Expand All @@ -8,7 +8,7 @@ RUN go install go.k6.io/xk6/cmd/xk6@latest
# Feel free to add other extensions using the '--with ...'.
RUN xk6 build \
--with github.com/topfreegames/pitaya/xk6-pitaya@$pitaya_revision \
--with github.com/topfreegames/pitaya/v3@$pitaya_revision \
--replace github.com/topfreegames/pitaya/v3=github.com/topfreegames/pitaya/v3@$pitaya_revision \
--output /k6

# Use the operator's base image and override the k6 binary
Expand Down
54 changes: 52 additions & 2 deletions xk6-pitaya/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pitaya

import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -32,18 +34,26 @@ type Client struct {
pushes map[string]chan []byte
timeout time.Duration
metrics *pitayaMetrics
useTLS bool
}

// Connect connects to the server
// addr is the address of the server to connect to
func (c *Client) Connect(addr string) error { //TODO: tls Options
func (c *Client) Connect(addr string) error {
vuState := c.vu.State()

if vuState == nil {
return errors.New("connecting to a pitaya server in the init context is not supported")
}

err := c.client.ConnectTo(addr)
var err error
if c.useTLS {
tlsConfig := &tls.Config{GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) { return nil, nil }, InsecureSkipVerify: true}
err = c.client.ConnectTo(addr, tlsConfig)
} else {
err = c.client.ConnectTo(addr)
}

if err != nil {
return err
}
Expand Down Expand Up @@ -100,6 +110,46 @@ func (c *Client) Notify(route string, msg interface{}) error {
return c.client.SendNotify(route, data)
}

// RequestB64 sends a request to the server using a base64 string
// route is the route to send the request to
// str is the string passed in request
// returns a promise that will be resolved when the response is received
// the promise will be rejected if the timeout is reached before a response is received
func (c *Client) RequestB64(route string, b64msg string) *goja.Promise { // TODO: add custom timeout
promise, resolve, reject := c.makeHandledPromise()
data, err := base64.StdEncoding.DecodeString(b64msg)
if err != nil {
reject(err)
return promise
}

timeNow := time.Now()
mid, err := c.client.SendRequest(route, data)
if err != nil {
c.pushRequestMetrics(route, time.Since(timeNow), false, false)
reject(err)
return promise
}
responseChan := c.getResponseChannelForID(mid)
go func() {
select {
case responseData := <-responseChan:
c.pushRequestMetrics(route, time.Since(timeNow), true, false)
var ret Response
if err := json.Unmarshal(responseData, &ret); err != nil {
resolve(responseData)
return
}
resolve(ret)
return
case <-time.After(c.timeout):
c.pushRequestMetrics(route, time.Since(timeNow), false, true)
reject(fmt.Errorf("Timeout waiting for response on route %s", route))
}
}()
return promise
}

// Request sends a request to the server
// route is the route to send the request to
// msg is the message to send
Expand Down
1 change: 1 addition & 0 deletions xk6-pitaya/examples/scenario1.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const opts = {
}
},
requestTimeoutMs: 1000,
useTLS: false,
}

const pitayaClient = new pitaya.Client(opts)
Expand Down
2 changes: 2 additions & 0 deletions xk6-pitaya/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (mi *ModuleInstance) NewClient(call goja.ConstructorCall) *goja.Object {
pushes: make(map[string]chan []byte, 100),
timeout: time.Duration(opts.RequestTimeoutMs) * time.Millisecond,
metrics: mi.metrics,
useTLS: opts.UseTLS,
}

client.client = pitayaclient.New(logrus.InfoLevel)
Expand All @@ -96,6 +97,7 @@ func (mi *ModuleInstance) NewClient(call goja.ConstructorCall) *goja.Object {
type options struct {
HandshakeData *session.HandshakeData `json:"handshakeData"`
RequestTimeoutMs int `json:"requestTimeoutMs"`
UseTLS bool `json:"useTLS"`
}

// newOptionsFrom validates and instantiates an options struct from its map representation
Expand Down

0 comments on commit 3664bf0

Please sign in to comment.