File: context.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (65 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package protocol

import (
	"bytes"
	"context"
	"sync"

	"cuelang.org/go/internal/golangorgx/tools/event"
	"cuelang.org/go/internal/golangorgx/tools/event/core"
	"cuelang.org/go/internal/golangorgx/tools/event/export"
	"cuelang.org/go/internal/golangorgx/tools/event/label"
	"cuelang.org/go/internal/golangorgx/tools/xcontext"
)

type contextKey int

const (
	clientKey = contextKey(iota)
)

func WithClient(ctx context.Context, client Client) context.Context {
	return context.WithValue(ctx, clientKey, client)
}

func LogEvent(ctx context.Context, ev core.Event, lm label.Map, mt MessageType) context.Context {
	client, ok := ctx.Value(clientKey).(Client)
	if !ok {
		return ctx
	}
	buf := &bytes.Buffer{}
	p := export.Printer{}
	p.WriteEvent(buf, ev, lm)
	msg := &LogMessageParams{Type: mt, Message: buf.String()}
	// Handle messages generated via event.Error, which won't have a level Label.
	if event.IsError(ev) {
		msg.Type = Error
	}

	// The background goroutine lives forever once started,
	// and ensures log messages are sent in order (#61216).
	startLogSenderOnce.Do(func() {
		go func() {
			for f := range logQueue {
				f()
			}
		}()
	})

	// Add the log item to a queue, rather than sending a
	// window/logMessage request to the client synchronously,
	// which would slow down this thread.
	ctx2 := xcontext.Detach(ctx)
	logQueue <- func() { client.LogMessage(ctx2, msg) }

	return ctx
}

var (
	startLogSenderOnce sync.Once
	logQueue           = make(chan func(), 100) // big enough for a large transient burst
)