Skip to content

Commit 110f7e0

Browse files
authored
feat: reusable http client and enhance backoff retry (#64)
1 parent e7d3d5a commit 110f7e0

File tree

154 files changed

+6032
-2884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+6032
-2884
lines changed

.github/workflows/lint.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ on:
88
- "go.sum"
99
- ".github/workflows/*.yaml"
1010

11-
env:
12-
GO_VERSION: 1.23
13-
1411
jobs:
1512
detect-modules:
1613
runs-on: ubuntu-latest
@@ -34,13 +31,13 @@ jobs:
3431
- uses: actions/checkout@v4
3532
- uses: actions/setup-go@v5
3633
with:
37-
go-version: ${{ env.GO_VERSION }}
34+
go-version-file: ./go.mod
3835
- name: Format
3936
run: |
4037
diff -u <(echo -n) <(gofmt -d -s .)
4138
cd ndc-http-schema && diff -u <(echo -n) <(gofmt -d -s .)
4239
- name: golangci-lint ${{ matrix.modules }}
43-
uses: golangci/golangci-lint-action@v6
40+
uses: golangci/golangci-lint-action@v7
4441
with:
4542
args: --timeout=5m
4643
working-directory: ${{ matrix.modules }}

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112
uses: actions/checkout@v4
113113
- uses: actions/setup-go@v5
114114
with:
115-
go-version: "1.23"
115+
go-version-file: ./go.mod
116116
- name: Build the CLI
117117
run: |
118118
VERSION="$GITHUB_REF_NAME" make ci-build-cli

.github/workflows/test.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ jobs:
88
test-go:
99
name: Run unit tests
1010
runs-on: ubuntu-latest
11-
permissions:
12-
pull-requests: write
1311
steps:
1412
- name: Checkout
1513
uses: actions/checkout@v4
1614
- uses: actions/setup-go@v5
1715
with:
18-
go-version: ${{ env.GO_VERSION }}
16+
go-version-file: ./go.mod
1917
- name: Run integration tests
2018
run: |
2119
./scripts/test.sh

.golangci.yml

Lines changed: 98 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,115 @@
1+
version: "2"
2+
13
linters:
2-
enable-all: true
4+
default: all
35
disable:
46
- err113
57
- lll
6-
- gocognit
7-
- funlen
88
- gomoddirectives
99
- depguard
10-
- gosec
11-
- revive
12-
- cyclop
13-
- nestif
14-
- wsl
1510
- wrapcheck
1611
- varnamelen
1712
- exhaustive
1813
- exhaustruct
1914
- ireturn
2015
- gochecknoglobals
21-
- stylecheck
2216
- nilnil
2317
- mnd
2418
- tagliatelle
25-
- goconst
26-
- noctx
2719
- recvcheck
20+
- funcorder
21+
- tagalign
22+
23+
settings:
24+
nestif:
25+
# Minimal complexity of if statements to report.
26+
# Default: 5
27+
min-complexity: 15
28+
29+
gocritic:
30+
disabled-checks:
31+
- appendAssign
32+
gocyclo:
33+
min-complexity: 40
34+
35+
cyclop:
36+
max-complexity: 30
37+
38+
revive:
39+
# Maximum number of open files at the same time.
40+
max-open-files: 2048
41+
severity: error
42+
rules:
43+
- name: var-naming
44+
disabled: true
45+
- name: exported
46+
disabled: true
47+
48+
gosec:
49+
excludes:
50+
- G115
51+
- G301
52+
- G304
53+
- G306
54+
- G404
55+
56+
funlen:
57+
lines: 200
58+
statements: 90
59+
60+
gocognit:
61+
min-complexity: 60
62+
63+
# Defines a set of rules to ignore issues.
64+
# It does not skip the analysis, and so does not ignore "typecheck" errors.
65+
exclusions:
66+
# Mode of the generated files analysis.
67+
#
68+
# - `strict`: sources are excluded by strictly following the Go generated file convention.
69+
# Source files that have lines matching only the following regular expression will be excluded: `^// Code generated .* DO NOT EDIT\.$`
70+
# This line must appear before the first non-comment, non-blank text in the file.
71+
# https://go.dev/s/generatedcode
72+
# - `lax`: sources are excluded if they contain lines like `autogenerated file`, `code generated`, `do not edit`, etc.
73+
# - `disable`: disable the generated files exclusion.
74+
#
75+
# Default: lax
76+
generated: strict
77+
# Log a warning if an exclusion rule is unused.
78+
warn-unused: false
79+
rules:
80+
- path: ndc-http-schema
81+
linters:
82+
- noctx
83+
- goconst
84+
85+
# Which file paths to exclude: they will be analyzed, but issues from them won't be reported.
86+
# "/" will be replaced by the current OS file path separator to properly work on Windows.
87+
paths:
88+
- ".*_test\\.go$"
2889

29-
linters-settings:
30-
nestif:
31-
# Minimal complexity of if statements to report.
32-
# Default: 5
33-
min-complexity: 10
34-
35-
gocritic:
36-
disabled-checks:
37-
- appendAssign
38-
gocyclo:
39-
min-complexity: 40
40-
41-
issues:
42-
exclude-files:
43-
- ".*_test\\.go$"
90+
formatters:
91+
# Enable specific formatter.
92+
# Default: [] (uses standard Go formatting)
93+
enable:
94+
- gci
95+
- gofmt
96+
- gofumpt
97+
- goimports
98+
- golines
99+
# Formatters settings.
100+
settings: {}
101+
exclusions:
102+
# Mode of the generated files analysis.
103+
#
104+
# - `strict`: sources are excluded by strictly following the Go generated file convention.
105+
# Source files that have lines matching only the following regular expression will be excluded: `^// Code generated .* DO NOT EDIT\.$`
106+
# This line must appear before the first non-comment, non-blank text in the file.
107+
# https://go.dev/s/generatedcode
108+
# - `lax`: sources are excluded if they contain lines like `autogenerated file`, `code generated`, `do not edit`, etc.
109+
# - `disable`: disable the generated files exclusion.
110+
#
111+
# Default: lax
112+
generated: strict
113+
# Which file paths to exclude.
114+
# Default: []
115+
paths: []

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ OUTPUT_DIR := _output
33

44
.PHONY: format
55
format:
6-
gofmt -w -s .
7-
cd ndc-http-schema && gofmt -w -s .
6+
golangci-lint fmt
87

98
.PHONY: test
109
test:
@@ -15,7 +14,14 @@ test:
1514
# https://golangci-lint.run/usage/install
1615
.PHONY: lint
1716
lint:
17+
golangci-lint run
18+
cd exhttp && golangci-lint run
19+
cd ndc-http-schema && golangci-lint run
20+
21+
.PHONY: lint-fix
22+
lint-fix:
1823
golangci-lint run --fix
24+
cd exhttp && golangci-lint run --fix
1925
cd ndc-http-schema && golangci-lint run --fix
2026

2127
# clean the output directory

connector/connector.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"path/filepath"
99

1010
"github.com/hasura/ndc-http/connector/internal"
11+
"github.com/hasura/ndc-http/exhttp"
1112
"github.com/hasura/ndc-http/ndc-http-schema/configuration"
1213
rest "github.com/hasura/ndc-http/ndc-http-schema/schema"
1314
"github.com/hasura/ndc-sdk-go/connector"
1415
"github.com/hasura/ndc-sdk-go/schema"
16+
"go.opentelemetry.io/otel/attribute"
1517
)
1618

1719
// HTTPConnector implements the SDK interface of NDC specification.
@@ -38,7 +40,10 @@ func NewHTTPConnector(opts ...Option) *HTTPConnector {
3840

3941
// ParseConfiguration validates the configuration files provided by the user, returning a validated 'Configuration',
4042
// or throwing an error to prevents Connector startup.
41-
func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir string) (*configuration.Configuration, error) {
43+
func (c *HTTPConnector) ParseConfiguration(
44+
ctx context.Context,
45+
configurationDir string,
46+
) (*configuration.Configuration, error) {
4247
restCapabilities := schema.CapabilitiesResponse{
4348
Version: "0.1.6",
4449
Capabilities: schema.Capabilities{
@@ -52,10 +57,12 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
5257
},
5358
},
5459
}
60+
5561
rawCapabilities, err := json.Marshal(restCapabilities)
5662
if err != nil {
5763
return nil, fmt.Errorf("failed to encode capabilities: %w", err)
5864
}
65+
5966
c.capabilities = schema.NewRawCapabilitiesResponseUnsafe(rawCapabilities)
6067

6168
config, err := configuration.ReadConfigurationFile(configurationDir)
@@ -64,6 +71,7 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
6471
}
6572

6673
logger := connector.GetLogger(ctx)
74+
6775
schemas, err := configuration.ReadSchemaOutputFile(configurationDir, config.Output, logger)
6876
if err != nil {
6977
return nil, err
@@ -72,7 +80,12 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
7280
var errs map[string][]string
7381

7482
if schemas == nil {
75-
logger.Debug(fmt.Sprintf("output file at %s does not exist. Parsing files...", filepath.Join(configurationDir, config.Output)))
83+
logger.Debug(
84+
fmt.Sprintf(
85+
"output file at %s does not exist. Parsing files...",
86+
filepath.Join(configurationDir, config.Output),
87+
),
88+
)
7689

7790
schemas, errs = configuration.BuildSchemaFromConfig(config, configurationDir, logger)
7891
if len(errs) > 0 {
@@ -82,7 +95,16 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
8295
}
8396
}
8497

98+
c.httpClient.Transport = exhttp.NewTelemetryTransport(
99+
c.httpClient.Transport,
100+
exhttp.TelemetryConfig{
101+
Logger: logger,
102+
Attributes: []attribute.KeyValue{attribute.String("db.system", "http")},
103+
},
104+
)
105+
85106
c.config = config
107+
86108
c.upstreams, err = internal.NewUpstreamManager(c.httpClient, config)
87109
if err != nil {
88110
return nil, err
@@ -102,7 +124,11 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
102124
//
103125
// In addition, this function should register any
104126
// connector-specific metrics with the metrics registry.
105-
func (c *HTTPConnector) TryInitState(ctx context.Context, configuration *configuration.Configuration, metrics *connector.TelemetryState) (*State, error) {
127+
func (c *HTTPConnector) TryInitState(
128+
ctx context.Context,
129+
configuration *configuration.Configuration,
130+
metrics *connector.TelemetryState,
131+
) (*State, error) {
106132
return &State{
107133
Tracer: metrics.Tracer,
108134
}, nil
@@ -114,11 +140,17 @@ func (c *HTTPConnector) TryInitState(ctx context.Context, configuration *configu
114140
// is able to reach its data source over the network.
115141
//
116142
// Should throw if the check fails, else resolve.
117-
func (c *HTTPConnector) HealthCheck(ctx context.Context, configuration *configuration.Configuration, state *State) error {
143+
func (c *HTTPConnector) HealthCheck(
144+
ctx context.Context,
145+
configuration *configuration.Configuration,
146+
state *State,
147+
) error {
118148
return nil
119149
}
120150

121151
// GetCapabilities get the connector's capabilities.
122-
func (c *HTTPConnector) GetCapabilities(configuration *configuration.Configuration) schema.CapabilitiesResponseMarshaler {
152+
func (c *HTTPConnector) GetCapabilities(
153+
configuration *configuration.Configuration,
154+
) schema.CapabilitiesResponseMarshaler {
123155
return c.capabilities
124156
}

0 commit comments

Comments
 (0)