Skip to content

Commit

Permalink
revise runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Nov 9, 2024
1 parent 1d89c43 commit 6cbdcb1
Show file tree
Hide file tree
Showing 38 changed files with 1,223 additions and 3,256 deletions.
47 changes: 5 additions & 42 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/hasura/ndc-rest/connector/internal"
"github.com/hasura/ndc-rest/ndc-rest-schema/configuration"
rest "github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"github.com/hasura/ndc-sdk-go/connector"
"github.com/hasura/ndc-sdk-go/schema"
"gopkg.in/yaml.v3"
)

// RESTConnector implements the SDK interface of NDC specification
type RESTConnector struct {
config *configuration.Configuration
metadata internal.MetadataCollection
capabilities *schema.RawCapabilitiesResponse
rawSchema *schema.RawSchemaResponse
Expand Down Expand Up @@ -53,7 +52,7 @@ func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir
}
c.capabilities = schema.NewRawCapabilitiesResponseUnsafe(rawCapabilities)

config, err := parseConfiguration(configurationDir)
config, err := configuration.ReadConfigurationFile(configurationDir)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +65,7 @@ func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir

var errs map[string][]string
if schemas == nil {
schemas, errs = configuration.BuildSchemaFiles(config, configurationDir, logger)
schemas, errs = configuration.BuildSchemaFromConfig(config, configurationDir, logger)
if len(errs) > 0 {
printSchemaValidationError(logger, errs)
return nil, errBuildSchemaFailed
Expand All @@ -77,6 +76,8 @@ func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir
return nil, errInvalidSchema
}

c.config = config

return config, nil
}

Expand Down Expand Up @@ -106,41 +107,3 @@ func (c *RESTConnector) HealthCheck(ctx context.Context, configuration *configur
func (c *RESTConnector) GetCapabilities(configuration *configuration.Configuration) schema.CapabilitiesResponseMarshaler {
return c.capabilities
}

func parseConfiguration(configurationDir string) (*configuration.Configuration, error) {
var config configuration.Configuration
jsonBytes, err := os.ReadFile(configurationDir + "/config.json")
if err == nil {
if err = json.Unmarshal(jsonBytes, &config); err != nil {
return nil, err
}
return &config, nil
}

if !os.IsNotExist(err) {
return nil, err
}

// try to read and parse yaml file
yamlBytes, err := os.ReadFile(configurationDir + "/config.yaml")
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
yamlBytes, err = os.ReadFile(configurationDir + "/config.yml")
}

if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("the config.{json,yaml,yml} file does not exist at %s", configurationDir)
} else {
return nil, err
}
}

if err = yaml.Unmarshal(yamlBytes, &config); err != nil {
return nil, err
}

return &config, nil
}
48 changes: 24 additions & 24 deletions connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,37 +309,37 @@ func TestRESTConnector_distribution(t *testing.T) {
}, connector.WithoutRecovery())
assert.NilError(t, err)

timeout, err := rc.metadata[0].Settings.Servers[0].Timeout.Value()
assert.NilError(t, err)
assert.Equal(t, int64(30), *timeout)
// timeout, err := rc.metadata[0].Settings.Servers[0].Timeout.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(30), *timeout)

retryTimes, err := rc.metadata[0].Settings.Servers[0].Retry.Times.Value()
assert.NilError(t, err)
assert.Equal(t, int64(2), *retryTimes)
// retryTimes, err := rc.metadata[0].Settings.Servers[0].Retry.Times.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(2), *retryTimes)

retryDelay, err := rc.metadata[0].Settings.Servers[0].Retry.Delay.Value()
assert.NilError(t, err)
assert.Equal(t, int64(1000), *retryDelay)
// retryDelay, err := rc.metadata[0].Settings.Servers[0].Retry.Delay.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(1000), *retryDelay)

retryStatus, err := rc.metadata[0].Settings.Servers[0].Retry.HTTPStatus.Value()
assert.NilError(t, err)
assert.DeepEqual(t, []int64{429, 500}, retryStatus)
// retryStatus, err := rc.metadata[0].Settings.Servers[0].Retry.HTTPStatus.Value()
// assert.NilError(t, err)
// assert.DeepEqual(t, []int64{429, 500}, retryStatus)

timeout1, err := rc.metadata[0].Settings.Servers[1].Timeout.Value()
assert.NilError(t, err)
assert.Equal(t, int64(10), *timeout1)
// timeout1, err := rc.metadata[0].Settings.Servers[1].Timeout.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(10), *timeout1)

retryTimes1, err := rc.metadata[0].Settings.Servers[1].Retry.Times.Value()
assert.NilError(t, err)
assert.Equal(t, int64(1), *retryTimes1)
// retryTimes1, err := rc.metadata[0].Settings.Servers[1].Retry.Times.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(1), *retryTimes1)

retryDelay1, err := rc.metadata[0].Settings.Servers[1].Retry.Delay.Value()
assert.NilError(t, err)
assert.Equal(t, int64(500), *retryDelay1)
// retryDelay1, err := rc.metadata[0].Settings.Servers[1].Retry.Delay.Value()
// assert.NilError(t, err)
// assert.Equal(t, int64(500), *retryDelay1)

retryStatus1, err := rc.metadata[0].Settings.Servers[1].Retry.HTTPStatus.Value()
assert.NilError(t, err)
assert.DeepEqual(t, []int64{429, 500, 501, 502}, retryStatus1)
// retryStatus1, err := rc.metadata[0].Settings.Servers[1].Retry.HTTPStatus.Value()
// assert.NilError(t, err)
// assert.DeepEqual(t, []int64{429, 500, 501, 502}, retryStatus1)

testServer := connServer.BuildTestServer()
defer testServer.Close()
Expand Down
8 changes: 5 additions & 3 deletions connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"log/slog"
"math"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -149,8 +150,9 @@ func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequ
logger.Debug("sending request to remote server...", logAttrs...)
}

times := int(request.Retry.Times)
for i := 0; i <= times; i++ {
times := int(math.Max(float64(request.Runtime.Retry.Times), 1))
delayMs := int(math.Max(float64(request.Runtime.Retry.Delay), 100))
for i := range times {
req, cancel, reqError := request.CreateRequest(ctx)
if reqError != nil {
cancel()
Expand Down Expand Up @@ -195,7 +197,7 @@ func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequ
)
}

time.Sleep(time.Duration(request.Retry.Delay) * time.Millisecond)
time.Sleep(time.Duration(delayMs) * time.Millisecond)
}

if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions connector/internal/metadata.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package internal

import (
"github.com/hasura/ndc-rest/ndc-rest-schema/configuration"
rest "github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"github.com/hasura/ndc-sdk-go/schema"
)

// MetadataCollection stores list of REST metadata with helper methods
type MetadataCollection []rest.NDCRestSchema
type MetadataCollection []configuration.NDCRestRuntimeSchema

// GetFunction gets the NDC function by name
func (rms MetadataCollection) GetFunction(name string) (*rest.OperationInfo, *rest.NDCRestSettings, error) {
func (rms MetadataCollection) GetFunction(name string) (*rest.OperationInfo, configuration.NDCRestRuntimeSchema, error) {
for _, rm := range rms {
fn := rm.GetFunction(name)
if fn != nil {
return fn, rm.Settings, nil
return fn, rm, nil
}
}
return nil, nil, schema.UnprocessableContentError("unsupported query: "+name, nil)
return nil, configuration.NDCRestRuntimeSchema{}, schema.UnprocessableContentError("unsupported query: "+name, nil)
}

// GetProcedure gets the NDC procedure by name
func (rms MetadataCollection) GetProcedure(name string) (*rest.OperationInfo, *rest.NDCRestSettings, error) {
func (rms MetadataCollection) GetProcedure(name string) (*rest.OperationInfo, configuration.NDCRestRuntimeSchema, error) {
for _, rm := range rms {
fn := rm.GetProcedure(name)
if fn != nil {
return fn, rm.Settings, nil
return fn, rm, nil
}
}
return nil, nil, schema.UnprocessableContentError("unsupported mutation: "+name, nil)
return nil, configuration.NDCRestRuntimeSchema{}, schema.UnprocessableContentError("unsupported mutation: "+name, nil)
}
Loading

0 comments on commit 6cbdcb1

Please sign in to comment.