Debian Codegraph @

gopls/doc/daemon.md

raw symbols SCIP
1 ---
2 title: "Gopls: Running as a daemon"
3 ---
4
5 **Note: this feature is new. If you encounter bugs, please [file an
6 issue](troubleshooting.md#file-an-issue).**
7
8 If you just want to try this out, skip ahead to the [quickstart](#quickstart).
9
10 ## Background: gopls execution modes
11
12 Gopls was originally implemented as an LSP sidecar: a process started by
13 editors or editor plugins, and communicated with using jsonrpc 2.0 over
14 stdin/stdout. By executing as a stateful process, gopls can maintain a
15 significant amount of cache and can eagerly perform analysis on the source code
16 being edited.
17
18 This execution mode does not work as well when there are many separate editor
19 processes or when editor processes are short-lived, as is often the case for
20 users of non-IDE editors such as Vim or Emacs. Having many processes means
21 having many caches, consuming a significant amount of system resources. Using
22 short-lived sessions means paying a start-up cost each time a session is
23 created.
24
25 To support these types of workflows, a new mode of gopls execution is supported
26 wherein a single, persistent, shared gopls "daemon" process is responsible for
27 managing all gopls sessions. In this mode, editors still start a gopls sidecar,
28 but this sidecar merely acts as a thin "forwarder", responsible for forwarding
29 the LSP to the shared gopls instance and recording metrics, logs, and rpc
30 traces.
31
32 ## Quickstart
33
34 To use a shared gopls instance you must either manage the daemon process
35 yourself, or let the gopls forwarder processes start the shared daemon as
36 needed.
37
38 ### Running with `-remote=auto`
39
40 Automatic management of the daemon is easiest, and can be done by passing the
41 flag `-remote=auto` to the gopls process started by your editor. This will
42 cause this process to auto-start the gopls daemon if needed, connect to it, and
43 forward the LSP. For example, here is a reasonable gopls invocation, that sets
44 some additional flags for easier [debugging](#debugging):
45
46 ```bash
47 gopls -remote=auto -logfile=auto -debug=:0 -remote.debug=:0 -rpc.trace
48 ```
49
50 Note that the shared gopls process will automatically shut down after one
51 minute with no connected clients.
52
53 ### Managing the daemon manually
54
55 To manage the gopls daemon process via external means rather than having the
56 forwarders manage it, you must start a gopls daemon process with the
57 `-listen=<addr>` flag, and then pass `-remote=<addr>` to the gopls processes
58 started by your editor.
59
60 For example, to host the daemon on the TCP port `37374`, do:
61
62 ```bash
63 gopls -listen=:37374 -logfile=auto -debug=:0
64 ```
65
66 And then from the editor, run
67
68 ```bash
69 gopls -remote=:37374 -logfile=auto -debug=:0 -rpc.trace
70 ```
71
72 If you are on a POSIX system, you can also use unix domain sockets by prefixing
73 the flag values with `unix;`. For example:
74
75 ```bash
76 gopls -listen="unix;/tmp/gopls-daemon-socket" -logfile=auto -debug=:0
77 ```
78
79 And connect via:
80
81 ```bash
82 gopls -remote="unix;/tmp/gopls-daemon-socket" -logfile=auto -debug=:0 -rpc.trace
83 ```
84
85 (Note that these flag values MUST be enclosed in quotes, because ';' is a
86 special shell character. For this reason, this syntax is subject to change in
87 the future.)
88
89 ## Debugging
90
91 Debugging a shared gopls session is more complicated than a singleton session,
92 because there are now two gopls processes involved with handling the LSP. Here
93 are some tips:
94
95 ### Finding logfiles and debug addresses
96
97 When running in daemon mode, you can use the `gopls inspect sessions` command
98 to find the logfile and debug port for your gopls daemon instance (as well as
99 for all its connected clients). By default, this inspects the default daemon
100 (i.e. `-remote=auto`). To inspect a different daemon, use the `-remote` flag
101 explicitly: `gopls -remote=localhost:12345 inspect sessions`.
102
103 This works whether or not you have enabled `-remote.debug`.
104
105 ### Traversing debug pages
106
107 When `-debug=:0` is passed to gopls, it runs a webserver that serves stateful
108 debug pages (see [troubleshooting.md](troubleshooting.md)). You can find the
109 actual port hosting these pages by either using the `gopls inspect sessions`
110 command, or by checking the start of the logfile -- it will be one of the first
111 log messages. For example, if using `-logfile=auto`, find the debug address by
112 checking `head /tmp/gopls-<pid>.log`.
113
114 By default, the gopls daemon is not started with `-debug`. To enable it, set
115 the `-remote.debug` flag on the forwarder instance, so that it invokes gopls
116 with `-debug` when starting the daemon.
117
118 The debug pages of the forwarder process will have a link to the debug pages of
119 the daemon server process. Correspondingly, the debug pages of the daemon
120 process will have a link to each of its clients.
121
122 This can help you find metrics, traces, and log files for all of the various
123 servers and clients.
124
125 ### Using logfiles
126
127 The gopls daemon is started with logging disabled by default. To customize
128 this, pass `-remote.logfile` to the gopls forwarder. Using
129 `-remote.logfile=auto`, the daemon will log to a default location (on posix
130 systems: `/tmp/gopls-daemon-<pid>.log`).
131
132 The gopls daemon does not log session-scoped messages: those are instead
133 reflected back to the forwarder so that they can be accessed by the editor.
134 Daemon logs will only contain global messages, for example logs when sessions
135 connect and disconnect.
136
137 It is recommended to start the forwarder gopls process with `-rpc.trace`, so
138 that its logfile will contain rpc trace logs specific to the LSP session.
139
140 ## Using multiple shared gopls instances
141
142 There may be environments where it is desirable to have more than one shared
143 gopls instance. If managing the daemon manually, this can be done by simply
144 choosing different `-listen` addresses for each distinct daemon process.
145
146 On POSIX systems, there is also support for automatic management of distinct
147 shared gopls processes: distinct daemons can be selected by passing
148 `-remote="auto;<id>"`. Any gopls forwarder passing the same value for `<id>`
149 will use the same shared daemon.
150
151 ## FAQ
152
153 **Q: Why am I not saving as much memory as I expected when using a shared gopls?**
154
155 A: As described in [implementation.md](design/implementation.md), gopls has a
156 concept of view/session/cache. Each session and view map onto exactly one
157 editor session (because they contain things like edited but unsaved buffers).
158 The cache contains things that are independent of any editor session, and can
159 therefore be shared.
160
161 When, for example, three editor session are sharing a single gopls process,
162 they will share the cache but will each have their own session and view. The
163 memory savings in this mode, when compared to three separate gopls processes,
164 corresponds to the amount of cache overlap across sessions.
165
166 Because this hasn't mattered much in the past, it is likely that there is state
167 that can be moved out of the session/view, and into the cache, thereby
168 increasing the amount of memory savings in the shared mode.
169
170 **Q: How do I customize the daemon instance when using `-remote=auto`?**
171
172 The daemon may be customized using flags of the form `-remote.*` on the
173 forwarder gopls. This causes the forwarder to invoke gopls with these settings
174 when starting the daemon. As of writing, we expose the following configuration:
175
176 * `-remote.logfile`: the location of the daemon logfile
177 * `-remote.debug`: the daemon's debug address
178 * `-remote.listen.timeout`: the amount of time the daemon should wait for new
179 connections while there are no current connections, before shutting down.
180 Must be set to a valid `time.Duration` (e.g. `30s` or `5m`). If `0`, listen
181 indefinitely. Default: `1m`.
182
183 Note that once the daemon is already running, setting these flags will not
184 change its configuration. These flags only matter for the forwarder process
185 that actually starts the daemon.