Skip to content

Commit b5d2981

Browse files
committed
replaced With*{PanicRecover,PanicRecoverWriter,ExitCodeOnPanic} options to With*PanicCallback
1 parent c41d03a commit b5d2981

File tree

13 files changed

+286
-1120
lines changed

13 files changed

+286
-1120
lines changed

CHANGELOG.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}PanicRecovery` options
2-
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}PanicRecoveryWriter` options
3-
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}ExitCodeOnPanic` options
4-
* Added `ydb.WithPanicRecovery`, `ydb.WithPanicRecoveryWriter` and `ydb.WithExitCodeOnPanic` options
1+
* Refactored gtrace tool for generate `Compose` options
52
* Added panic recover on trace calls in `Compose` call step
6-
* Removed `trace.Context{Driver,Retry,Table}` and `trace.With{Driver,Retry,Table}` as redundant
3+
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}PanicCallback` options
4+
* Added `ydb.WithPanicCallback` option
75

86
## v3.16.12
97
* Fixed bug with check acquire error over `ydb.IsRatelimiterAcquireError`

cmd/gtrace/writer.go

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,6 @@ func (w *Writer) funcImports(dst []dep, fn *Func) []dep {
198198
}
199199

200200
func (w *Writer) traceImports(dst []dep, t *Trace) []dep {
201-
dst = append(
202-
dst,
203-
dep{
204-
pkgPath: "io",
205-
pkgName: "io",
206-
typName: "io.Writer",
207-
},
208-
)
209-
if len(t.Hooks) > 0 {
210-
dst = append(
211-
dst,
212-
dep{
213-
pkgPath: "fmt",
214-
pkgName: "fmt",
215-
typName: "Sprintf",
216-
},
217-
dep{
218-
pkgPath: "os",
219-
pkgName: "os",
220-
typName: "os.Stderr",
221-
},
222-
dep{
223-
pkgPath: "runtime/debug",
224-
pkgName: "debug",
225-
typName: "Stack",
226-
},
227-
)
228-
}
229201
for _, h := range t.Hooks {
230202
dst = w.funcImports(dst, h.Func)
231203
}
@@ -349,11 +321,7 @@ func (w *Writer) compose(trace *Trace) {
349321
w.line(`(`, ret, ` `, trace.Name, `) {`)
350322
w.block(func() {
351323
if len(trace.Hooks) > 0 {
352-
w.line(`options := `, unexported(trace.Name), `ComposeOptions{`)
353-
w.block(func() {
354-
w.line(`recoverPanicWriter: os.Stderr,`)
355-
})
356-
w.line(`}`)
324+
w.line(`options := `, unexported(trace.Name), `ComposeOptions{}`)
357325
w.line(`for _, opt := range opts {`)
358326
w.block(func() {
359327
w.line(`opt(&options)`)
@@ -391,22 +359,13 @@ func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
391359
args := w.funcParams(fn.Params)
392360
w.funcResults(fn)
393361
w.line(`{`)
394-
w.line(`if options.recoverPanic {`)
362+
w.line(`if options.panicCallback != nil {`)
395363
w.block(func() {
396364
w.line("defer func() {")
397365
w.block(func() {
398366
w.line("if e := recover(); e != nil {")
399367
w.block(func() {
400-
w.line(`if options.recoverPanicWriter != nil {`)
401-
w.block(func() {
402-
w.line(`fmt.Fprintf(options.recoverPanicWriter, "panic recovered:%v:\n%s", e, debug.Stack())`)
403-
})
404-
w.line("}")
405-
w.line("if options.exitCodeOnPanic != nil {")
406-
w.block(func() {
407-
w.line(`os.Exit(*options.exitCodeOnPanic)`)
408-
})
409-
w.line("}")
368+
w.line(`options.panicCallback(e)`)
410369
})
411370
w.line("}")
412371
})
@@ -467,9 +426,7 @@ func (w *Writer) options(trace *Trace) {
467426
w.line(fmt.Sprintf(`// %sComposeOptions is a holder of options`, unexported(trace.Name)))
468427
w.line(fmt.Sprintf(`type %sComposeOptions struct {`, unexported(trace.Name)))
469428
w.block(func() {
470-
w.line(`recoverPanic bool`)
471-
w.line(`exitCodeOnPanic *int`)
472-
w.line(`recoverPanicWriter io.Writer`)
429+
w.line(`panicCallback func(e interface{})`)
473430
})
474431
w.line(`}`)
475432
_ = w.bw.WriteByte('\n')
@@ -480,39 +437,12 @@ func (w *Writer) options(trace *Trace) {
480437
_ = w.bw.WriteByte('\n')
481438
})
482439
w.newScope(func() {
483-
w.line(fmt.Sprintf(`// With%sRecoverPanic specified behavior on panic - recover or not`, trace.Name))
484-
w.line(fmt.Sprintf(`func With%sRecoverPanic(b bool) %sComposeOption {`, trace.Name, trace.Name))
485-
w.block(func() {
486-
w.line(fmt.Sprintf(`return func(o *%sComposeOptions) {`, unexported(trace.Name)))
487-
w.block(func() {
488-
w.line(`o.recoverPanic = b`)
489-
})
490-
w.line(`}`)
491-
})
492-
w.line(`}`)
493-
_ = w.bw.WriteByte('\n')
494-
})
495-
w.newScope(func() {
496-
w.line(fmt.Sprintf(`// With%sRecoverPanicWriter specified writer for print panic details`, trace.Name))
497-
w.line(fmt.Sprintf(`func With%sRecoverPanicWriter(w io.Writer) %sComposeOption {`, trace.Name, trace.Name))
498-
w.block(func() {
499-
w.line(fmt.Sprintf(`return func(o *%sComposeOptions) {`, unexported(trace.Name)))
500-
w.block(func() {
501-
w.line(`o.recoverPanicWriter = w`)
502-
})
503-
w.line(`}`)
504-
})
505-
w.line(`}`)
506-
_ = w.bw.WriteByte('\n')
507-
})
508-
w.newScope(func() {
509-
w.line(fmt.Sprintf(`// With%sExitCodeOnPanic specified code for exit on panic`, trace.Name))
510-
w.line(`// If nil - no exiting on panic`)
511-
w.line(fmt.Sprintf(`func With%sExitCodeOnPanic(code *int) %sComposeOption {`, trace.Name, trace.Name))
440+
w.line(fmt.Sprintf(`// With%sPanicCallback specified behavior on panic`, trace.Name))
441+
w.line(fmt.Sprintf(`func With%sPanicCallback(cb func(e interface{})) %sComposeOption {`, trace.Name, trace.Name))
512442
w.block(func() {
513443
w.line(fmt.Sprintf(`return func(o *%sComposeOptions) {`, unexported(trace.Name)))
514444
w.block(func() {
515-
w.line(`o.exitCodeOnPanic = code`)
445+
w.line(`o.panicCallback = cb`)
516446
})
517447
w.line(`}`)
518448
})

connection.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ydb
22

33
import (
44
"context"
5-
"io"
65
"os"
76
"sync"
87

@@ -93,9 +92,7 @@ type connection struct {
9392
childrenMtx sync.Mutex
9493
onClose []func(c *connection)
9594

96-
recoverPanic bool
97-
exitCodeOnPanic *int
98-
recoverPanicWriter io.Writer
95+
panicCallback func(e interface{})
9996
}
10097

10198
func (c *connection) Close(ctx context.Context) error {

options.go

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/x509"
66
"encoding/pem"
77
"fmt"
8-
"io"
98
"io/ioutil"
109
"os"
1110
"path/filepath"
@@ -353,27 +352,12 @@ func WithSessionPoolDeleteTimeout(deleteTimeout time.Duration) Option {
353352
}
354353
}
355354

356-
// WithRecoverPanic specified flag for use panic recover on calls user-defined funcs
357-
func WithRecoverPanic() Option {
355+
// WithPanicCallback specified behavior on panic
356+
// Warning: WithPanicCallback must be defined on start of all options
357+
// (before `WithTrace{Driver,Table,Scheme,Scripting,Coordination,Ratelimiter}` and other options)
358+
func WithPanicCallback(cb func(e interface{})) Option {
358359
return func(ctx context.Context, c *connection) error {
359-
c.recoverPanic = true
360-
return nil
361-
}
362-
}
363-
364-
// WithRecoverPanicWriter specified `io.Writer` for logging panic details
365-
func WithRecoverPanicWriter(w io.Writer) Option {
366-
return func(ctx context.Context, c *connection) error {
367-
c.recoverPanicWriter = w
368-
return nil
369-
}
370-
}
371-
372-
// WithExitCodeOnPanic specified code for exit on panic
373-
// If nil - no exiting on panic
374-
func WithExitCodeOnPanic(code int) Option {
375-
return func(ctx context.Context, c *connection) error {
376-
c.exitCodeOnPanic = &code
360+
c.panicCallback = cb
377361
return nil
378362
}
379363
}
@@ -387,9 +371,7 @@ func WithTraceTable(t trace.Table, opts ...trace.TableComposeOption) Option {
387371
t,
388372
append(
389373
[]trace.TableComposeOption{
390-
trace.WithTableRecoverPanic(c.recoverPanic),
391-
trace.WithTableRecoverPanicWriter(c.recoverPanicWriter),
392-
trace.WithTableExitCodeOnPanic(c.exitCodeOnPanic),
374+
trace.WithTablePanicCallback(c.panicCallback),
393375
},
394376
opts...,
395377
)...,
@@ -408,9 +390,7 @@ func WithTraceScripting(t trace.Scripting, opts ...trace.ScriptingComposeOption)
408390
t,
409391
append(
410392
[]trace.ScriptingComposeOption{
411-
trace.WithScriptingRecoverPanic(c.recoverPanic),
412-
trace.WithScriptingRecoverPanicWriter(c.recoverPanicWriter),
413-
trace.WithScriptingExitCodeOnPanic(c.exitCodeOnPanic),
393+
trace.WithScriptingPanicCallback(c.panicCallback),
414394
},
415395
opts...,
416396
)...,
@@ -429,9 +409,7 @@ func WithTraceScheme(t trace.Scheme, opts ...trace.SchemeComposeOption) Option {
429409
t,
430410
append(
431411
[]trace.SchemeComposeOption{
432-
trace.WithSchemeRecoverPanic(c.recoverPanic),
433-
trace.WithSchemeRecoverPanicWriter(c.recoverPanicWriter),
434-
trace.WithSchemeExitCodeOnPanic(c.exitCodeOnPanic),
412+
trace.WithSchemePanicCallback(c.panicCallback),
435413
},
436414
opts...,
437415
)...,
@@ -450,9 +428,7 @@ func WithTraceCoordination(t trace.Coordination, opts ...trace.CoordinationCompo
450428
t,
451429
append(
452430
[]trace.CoordinationComposeOption{
453-
trace.WithCoordinationRecoverPanic(c.recoverPanic),
454-
trace.WithCoordinationRecoverPanicWriter(c.recoverPanicWriter),
455-
trace.WithCoordinationExitCodeOnPanic(c.exitCodeOnPanic),
431+
trace.WithCoordinationPanicCallback(c.panicCallback),
456432
},
457433
opts...,
458434
)...,
@@ -471,9 +447,7 @@ func WithTraceRatelimiter(t trace.Ratelimiter, opts ...trace.RatelimiterComposeO
471447
t,
472448
append(
473449
[]trace.RatelimiterComposeOption{
474-
trace.WithRatelimiterRecoverPanic(c.recoverPanic),
475-
trace.WithRatelimiterRecoverPanicWriter(c.recoverPanicWriter),
476-
trace.WithRatelimiterExitCodeOnPanic(c.exitCodeOnPanic),
450+
trace.WithRatelimiterPanicCallback(c.panicCallback),
477451
},
478452
opts...,
479453
)...,
@@ -499,9 +473,7 @@ func WithTraceDiscovery(t trace.Discovery, opts ...trace.DiscoveryComposeOption)
499473
t,
500474
append(
501475
[]trace.DiscoveryComposeOption{
502-
trace.WithDiscoveryRecoverPanic(c.recoverPanic),
503-
trace.WithDiscoveryRecoverPanicWriter(c.recoverPanicWriter),
504-
trace.WithDiscoveryExitCodeOnPanic(c.exitCodeOnPanic),
476+
trace.WithDiscoveryPanicCallback(c.panicCallback),
505477
},
506478
opts...,
507479
)...,

test/table_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"path"
15+
"runtime/debug"
1516
"strings"
1617
"sync"
1718
"sync/atomic"
@@ -245,9 +246,10 @@ func TestTable(t *testing.T) {
245246
ydb.WithErrWriter(os.Stdout),
246247
ydb.WithMinLevel(log.WARN),
247248
),
248-
ydb.WithRecoverPanic(),
249-
ydb.WithRecoverPanicWriter(os.Stderr),
250-
ydb.WithExitCodeOnPanic(1),
249+
ydb.WithPanicCallback(func(e interface{}) {
250+
fmt.Fprintf(os.Stderr, "panic recovered:%v:\n%s", e, debug.Stack())
251+
os.Exit(1)
252+
}),
251253
ydb.WithTraceTable(
252254
shutdownTrace.Compose(
253255
trace.Table{

trace/coordination_gtrace.go

Lines changed: 4 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)