Skip to content

Commit 2adaa49

Browse files
committed
log: console setup in a sep go routine
1 parent 6b6d6a3 commit 2adaa49

4 files changed

Lines changed: 53 additions & 26 deletions

File tree

intra/log/log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func SetConsoleLevel(level LogLevel) {
8484
Glogger.SetConsoleLevel(level)
8585
}
8686

87+
func ConsoleReady(ctx context.Context) {
88+
Glogger.ConsoleReady(ctx)
89+
}
90+
8791
// SetConsole sets external console to redirect log output to.
8892
func SetConsole(consoleCtx context.Context, c Console) {
8993
Glogger.SetConsole(c)

intra/log/logger.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Logger interface {
4949
SetLevel(level LogLevel)
5050
SetConsoleLevel(level LogLevel)
5151
SetConsole(c Console)
52+
ConsoleReady(ctx context.Context)
5253
Usr(msg string)
5354
Printf(msg string, args ...any)
5455
VeryVerbosef(at int, msg string, args ...any)
@@ -254,7 +255,6 @@ func defaultLogger() *simpleLogger {
254255
o: golog.New(os.Stdout, "", defaultFlags),
255256
q: newRing[string](context.TODO(), qSize),
256257
}
257-
go l.consoleDispatcher()
258258
return l
259259
}
260260

@@ -291,6 +291,12 @@ func (l *simpleLogger) SetConsole(c Console) {
291291
l.c.set(c) // c may point to nil impl
292292
}
293293

294+
func (l *simpleLogger) ConsoleReady(ctx context.Context) {
295+
go l.consoleDispatcher(ctx)
296+
// TODO: close l.msgC when ctx is done
297+
// TODO: wireup ctx to l.q
298+
}
299+
294300
func (l *simpleLogger) clearStCounts() {
295301
l.stmu.Lock()
296302
defer l.stmu.Unlock()
@@ -320,8 +326,13 @@ func (l *simpleLogger) incrStCount(id string) (c uint32) {
320326
// consoleDispatcher sends msgs from l.msgC to external log console.
321327
// It may drop logs on high load (50% for conNorm, 80% for conErr).
322328
// Must be called once from a goroutine.
323-
func (l *simpleLogger) consoleDispatcher() {
329+
func (l *simpleLogger) consoleDispatcher(ctx context.Context) {
324330
for m := range l.cmsgC {
331+
select {
332+
case <-ctx.Done():
333+
return
334+
default:
335+
}
325336
if m == nil || len(m.m) <= 0 { // no msg
326337
continue
327338
}

intra/log/ring.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ func newRing[T any](ctx context.Context, capacity int) *ring[T] {
2929
inC: make(chan T, capacity/2),
3030
}
3131
go r.process()
32+
context.AfterFunc(ctx, func() { close(r.inC) })
3233
return r
3334
}
3435

35-
func (r *ring[T]) closeWaiter() {
36-
defer close(r.inC)
37-
38-
<-r.ctx.Done()
39-
}
40-
4136
// Push adds an element to the ring buffer
4237
func (r *ring[T]) Push(v T) (ok bool) {
4338
select {
@@ -56,8 +51,6 @@ func (r *ring[T]) Push(v T) (ok bool) {
5651
// process reads from the input channel and adds elements to the ring buffer.
5752
// Must be run in a goroutine.
5853
func (r *ring[T]) process() {
59-
go r.closeWaiter()
60-
6154
for v := range r.inC {
6255
r.Lock()
6356
r.b[r.head] = v

intra/tunnel.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,34 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
186186

187187
const dualstack = settings.IP46
188188

189-
logfd := false
190-
if r, c, err := log.NewFilebased(); err == nil {
191-
closeall := func() {
192-
core.Close(c)
193-
core.Close(r)
189+
logch := make(chan bool, 1)
190+
crashch := make(chan bool, 1)
191+
go func() {
192+
logfd := false
193+
if r, c, err := log.NewFilebased(); err == nil {
194+
closeall := func() {
195+
core.Close(c)
196+
core.Close(r)
197+
}
198+
if logfd = bdg.LogFD(int(r.Fd())); logfd {
199+
log.SetConsole(ctx, c)
200+
context.AfterFunc(ctx, closeall)
201+
} else {
202+
closeall()
203+
}
194204
}
195-
if logfd = bdg.LogFD(int(r.Fd())); logfd {
196-
log.SetConsole(ctx, c)
197-
context.AfterFunc(ctx, closeall)
198-
} else {
199-
closeall()
205+
if !logfd {
206+
log.SetConsole(ctx, &clogAdapter{bdg})
200207
}
201-
}
202-
if !logfd {
203-
log.SetConsole(ctx, &clogAdapter{bdg})
204-
}
208+
log.D("tun: <<< new >>>; log out ok; fd? %t", logfd)
209+
logch <- logfd
210+
}()
205211

206-
crashfd := pipeCrashOutput(bdg)
212+
go func() {
213+
crashfd := pipeCrashOutput(bdg)
214+
crashch <- crashfd
215+
log.D("tun: <<< new >>>; crash out ok; fd? %t", crashfd)
216+
}()
207217

208218
natpt := x64.NewNatPt2(ctx)
209219
proxies := ipn.NewProxifier(ctx, dualstack, linkmtu, bdg, bdg)
@@ -214,12 +224,21 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
214224
proxies == nil, services == nil)
215225
}
216226

227+
// kickstart may call into ProxyFor which has a multi-second wait time
228+
// when proxies are not found
217229
if err := dtr.kickstart(proxies); err != nil {
218230
log.W("tun: <<< new >>>; kickstart err(%v)", err)
219231
return nil, err
220232
}
221233

222-
log.D("tun: <<< new >>>; proxies, svcs, bootstrap: ok; fds (log? %t / crash? %t)", logfd, crashfd)
234+
log.D("tun: <<< new >>>; default dns: ok")
235+
236+
logfd := <-logch
237+
crashfd := <-crashch
238+
239+
log.ConsoleReady(ctx)
240+
241+
log.D("tun: <<< new >>>; logger, proxies, svcs, bootstrap: ok; fds (log? %t / crash? %t)", logfd, crashfd)
223242

224243
resolver := dnsx.NewResolver(ctx, fakedns, dtr, bdg, natpt)
225244
resolver.Add(newGoosTransport(ctx, proxies)) // os-resolver; fixed

0 commit comments

Comments
 (0)