Skip to content

Commit

Permalink
bootstrap: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jul 1, 2024
1 parent c3d1a3f commit d48aa52
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/bootstrap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bootstrap

import (
"context"
"fmt"
"log/slog"
"net"
"net/netip"
Expand Down Expand Up @@ -69,37 +70,38 @@ func (r ParallelResolver) LookupNetIP(
}

// recoverAndLog is a deferred helper that recovers from a panic and logs the
// panic value with the logger from context or with a default logger.
// panic value with the logger from context or with a default logger. Sends the
// recovered value into resCh.
//
// TODO(a.garipov): Move this helper to golibs.
func recoverAndLog(ctx context.Context) {
func recoverAndLog(ctx context.Context, resCh chan<- any) {
v := recover()
if v == nil {
return
}

var args []any
if err, ok := v.(error); ok {
args = []any{slogutil.KeyError, err}
} else {
args = []any{"value", v}
err, ok := v.(error)
if !ok {
err = fmt.Errorf("error value: %v", v)
}

l, ok := slogutil.LoggerFromContext(ctx)
if !ok {
l = slog.Default()
}

l.ErrorContext(ctx, "recovered from panic", args...)
l.ErrorContext(ctx, "recovered panic", slogutil.KeyError, err)
slogutil.PrintStack(ctx, l, slog.LevelError)

resCh <- err
}

// lookupAsync performs a lookup for ip of host with r and sends the result into
// resCh. It is intended to be used as a goroutine.
func lookupAsync(ctx context.Context, r Resolver, network, host string, resCh chan<- any) {
// TODO(d.kolyshev): Propose better solution to recover without requiring
// logger in the context.
defer recoverAndLog(ctx)
defer recoverAndLog(ctx, resCh)

addrs, err := lookup(ctx, r, network, host)
if err != nil {
Expand Down

0 comments on commit d48aa52

Please sign in to comment.