Skip to content

Commit d9bfe89

Browse files
authored
Merge branch 'main' into add_drpc_metrics
2 parents 8f47786 + 4b35ab8 commit d9bfe89

3 files changed

Lines changed: 64 additions & 87 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
go-version: '1.25.x'
3434
- name: Install staticcheck
3535
run: go install honnef.co/go/tools/cmd/staticcheck@latest
36-
- uses: golangci/golangci-lint-action@v4
36+
- uses: golangci/golangci-lint-action@v7
3737
with:
3838
version: latest
3939
- name: Lint

.golangci.yml

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
version: "2"
2+
13
run:
2-
deadline: 10m
4+
timeout: 10m
35
issues-exit-code: 1
46
tests: true
57

6-
78
linters:
89
enable:
910
- bodyclose # find unclosed http response bodies
1011
- dogsled # checks for too many ignored arguments
1112
- durationcheck # verifies whether durations are multiplied, usually a mistake
1213
- gocritic # checks for style, performance issues, and common programming errors
13-
- gofmt # sanity check formatting
14-
- goprintffuncname # checks that printf-like functions are named with `f` at the end [fast: true, auto-fix: false]
14+
- goprintffuncname # checks that printf-like functions are named with `f` at the end
1515
- govet # check standard vet rules
1616
- importas # verify that imports are consistent
1717
- ineffassign # find ineffective assignments
@@ -44,114 +44,64 @@ linters:
4444
- exhaustive # doesn't handle default case
4545
- forbidigo # not useful
4646
- funlen # no limit on func length
47-
- gci # we have custom import checking
4847
- gocognit # this complexity is not a good metric
4948
- goconst # check for things that could be replaced by constants
5049
- gocyclo # this complexity is not a good metric
5150
- godox # too many false positivies
5251
- goheader # separate tool
53-
- goimports # disabled, because it's slow, using scripts/check-imports.go instead.
5452
- gomoddirectives # not useful
5553
- gomodguard # not useful
5654
- gosec # needs tweaking
57-
- gosimple # part of staticcheck
5855
- lll # don't need this check
5956
- nlreturn # non-important code style
6057
- paralleltest # too many false positives
6158
- predeclared # kind of useful, but not critical
6259
- promlinter # not relevant
63-
- rowserrcheck # checks if sql.Rows.Err is checked correctly - Disabled because it reports false positive with defer statements after Query call
6460
- sqlclosecheck # we have tagsql, which checks this better
6561
- staticcheck # we use the separate staticcheck binary already
66-
- stylecheck # has false positives
6762
- tagliatelle # not our style
6863
- testpackage # sometimes it's useful to have tests on private funcs
6964
- thelper # too many false positives
7065
- tparallel # false positivies
7166
- unused # part of staticcheck
7267
- wrapcheck # too much noise and false positives
7368
- wsl # too much noise
74-
fast: false
69+
settings:
70+
errcheck:
71+
check-type-assertions: false
72+
check-blank: false
73+
govet: {}
74+
gocritic:
75+
disabled-checks:
76+
- ifElseChain
77+
gocyclo:
78+
min-complexity: 10
79+
dupl:
80+
threshold: 150
81+
goconst:
82+
min-len: 3
83+
min-occurrences: 3
84+
lll:
85+
line-length: 140
86+
tab-width: 1
87+
unparam:
88+
check-exported: false
89+
nakedret:
90+
max-func-lines: 30
91+
prealloc:
92+
simple: true
93+
range-loops: true
94+
for-loops: false
95+
96+
formatters:
97+
enable:
98+
- gofmt # sanity check formatting
7599

76100
output:
77101
formats:
78-
- format: colored-line-number
79-
print-issued-lines: true
80-
print-linter-name: true
81-
82-
linters-settings:
83-
errcheck:
84-
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
85-
# default is false: such cases aren't reported by default.
86-
check-type-assertions: false
87-
88-
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
89-
# default is false: such cases aren't reported by default.
90-
check-blank: false
91-
govet:
92-
# report about shadowed variables
93-
#TODO# check-shadowing: true
94-
95-
# Obtain type information from installed (to $GOPATH/pkg) package files:
96-
# golangci-lint will execute `go install -i` and `go test -i` for analyzed packages
97-
# before analyzing them.
98-
# Enable this option only if all conditions are met:
99-
# 1. you use only "fast" linters (--fast e.g.): no program loading occurs
100-
# 2. you use go >= 1.10
101-
# 3. you do repeated runs (false for CI) or cache $GOPATH/pkg or `go env GOCACHE` dir in CI.
102-
use-installed-packages: false
103-
gocritic:
104-
disabled-checks:
105-
- ifElseChain
106-
goimports:
107-
local: "storj.io"
108-
golint:
109-
min-confidence: 0.8
110-
gofmt:
111-
simplify: true
112-
gocyclo:
113-
min-complexity: 10
114-
dupl:
115-
threshold: 150
116-
goconst:
117-
min-len: 3
118-
min-occurrences: 3
119-
misspell:
120-
lll:
121-
line-length: 140
122-
tab-width: 1
123-
unused:
124-
# treat code as a program (not a library) and report unused exported identifiers; default is false.
125-
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
126-
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
127-
# with golangci-lint call it on a directory with the changed file.
128-
check-exported: false
129-
unparam:
130-
# call graph construction algorithm (cha, rta). In general, use cha for libraries,
131-
# and rta for programs with main packages. Default is cha.
132-
algo: cha
133-
134-
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
135-
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
136-
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
137-
# with golangci-lint call it on a directory with the changed file.
138-
check-exported: false
139-
nakedret:
140-
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
141-
max-func-lines: 30
142-
prealloc:
143-
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
144-
# True by default.
145-
simple: true
146-
range-loops: true # Report preallocation suggestions on range loops, true by default
147-
for-loops: false # Report preallocation suggestions on for loops, false by default
102+
text: {}
148103

149104
issues:
150105
max-issues-per-linter: 0
151106
max-same-issues: 0
152107
new: false
153-
exclude-use-default: false
154-
exclude-files:
155-
- ".*\\.pb\\.go$"
156-
- ".*\\.dbx\\.go$"
157-
- "cmd/protoc-gen-go-drpc/.*"

drpcserver/server.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ type Options struct {
3434
// CollectStats controls whether the server should collect stats on the
3535
// rpcs it serves.
3636
CollectStats bool
37+
38+
// TLSConfig, if non-nil, is used to wrap the listener with tls.NewListener
39+
// in Serve(). The TLS handshake is performed explicitly in ServeOne before
40+
// processing requests.
41+
TLSConfig *tls.Config
42+
43+
// TLSCipherRestrict, if non-nil, is called in ServeOne immediately after
44+
// a successful TLS handshake. It receives the net.Conn (which is a
45+
// *tls.Conn) and may inspect ConnectionState to enforce cipher suite
46+
// restrictions. If it returns a non-nil error the connection is rejected.
47+
TLSCipherRestrict func(conn net.Conn) error
3748

3849
// Metrics holds optional metrics the server will populate. If nil, no
3950
// metrics are recorded.
@@ -151,6 +162,12 @@ func New(handler drpc.Handler) *Server {
151162
// NewWithOptions constructs a new Server using the provided options to tune
152163
// how the drpc connections are handled.
153164
func NewWithOptions(handler drpc.Handler, opts Options) *Server {
165+
// Clone the TLS config so the server owns its copy and the caller cannot
166+
// mutate it after construction.
167+
if opts.TLSConfig != nil {
168+
opts.TLSConfig = opts.TLSConfig.Clone()
169+
}
170+
154171
s := &Server{
155172
opts: opts,
156173
handler: handler,
@@ -206,11 +223,17 @@ func (s *Server) ServeOne(ctx context.Context, tr drpc.Transport) (err error) {
206223
// interrupting any ongoing communication. Even if we didn't call it
207224
// explicitly, the first read/write operation would call it internally
208225
// anyway.
209-
err := tlsConn.Handshake()
226+
err := tlsConn.HandshakeContext(ctx)
210227
if err != nil {
211228
s.opts.Metrics.addTLSHandshakeError()
212229
return drpc.ConnectionError.New("server handshake [%q] failed: %w", tlsConn.RemoteAddr(), err)
213230
}
231+
if s.opts.TLSCipherRestrict != nil {
232+
if err := s.opts.TLSCipherRestrict(tlsConn); err != nil {
233+
s.opts.Metrics.addTLSHandshakeError()
234+
return drpc.ConnectionError.New("server handshake [%q] failed: %w", tlsConn.RemoteAddr(), err)
235+
}
236+
}
214237
state := tlsConn.ConnectionState()
215238
if len(state.PeerCertificates) > 0 {
216239
ctx = drpcctx.WithPeerConnectionInfo(
@@ -247,6 +270,10 @@ var temporarySleep = 500 * time.Millisecond
247270
// Serve listens for connections on the listener and serves the drpc request
248271
// on new connections.
249272
func (s *Server) Serve(ctx context.Context, lis net.Listener) (err error) {
273+
if s.opts.TLSConfig != nil {
274+
lis = tls.NewListener(lis, s.opts.TLSConfig)
275+
}
276+
250277
tracker := drpcctx.NewTracker(ctx)
251278
defer tracker.Wait()
252279
defer tracker.Cancel()

0 commit comments

Comments
 (0)