Skip to content

Commit b303e13

Browse files
committed
gopls/internal/golang: view pkg doc: display when "disconnected"
This CL causes the "View package doc" pages to display a banner when the gopls server dies, at which point the page is no longer operative. (A new server will have a different port.) Change-Id: Icde10a7b4c25a28177d043c573dfdab4ce7c7964 Reviewed-on: https://go-review.googlesource.com/c/tools/+/575536 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 41a9213 commit b303e13

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

gopls/internal/golang/pkgdoc.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ package golang
1919
// - add option for doc.AllDecls: show non-exported symbols too.
2020
// - abbreviate long signatures by replacing parameters 4 onwards with "...".
2121
// - style the <li> bullets in the index as invisible.
22-
// - add push notifications (using hanging GET, server-side events,
23-
// or polling) like didChange (=> auto reload)
24-
// and server death (=> display "server disconnected" banner).
22+
// - add push notifications such as didChange -> reload.
2523

2624
import (
2725
"bytes"
@@ -131,9 +129,21 @@ function httpGET(url) {
131129
xhttp.send();
132130
return false; // disable usual <a href=...> behavior
133131
}
132+
133+
// Start a GET /hang request. If it ever completes, the server
134+
// has disconnected. Show a banner in that case.
135+
{
136+
var x = new XMLHttpRequest();
137+
x.open("GET", "/hang", true);
138+
x.onloadend = () => {
139+
document.getElementById("disconnected").style.display = 'block';
140+
};
141+
x.send();
142+
};
134143
</script>
135144
</head>
136145
<body>
146+
<div id='disconnected'>Gopls server has terminated. Page is inactive.</div>
137147
`)
138148

139149
escape := html.EscapeString
@@ -541,4 +551,14 @@ a:hover > * {
541551
.lit { color: darkgreen; }
542552
543553
#pkgsite { height: 1.5em; }
554+
555+
#disconnected {
556+
position: fixed;
557+
top: 1em;
558+
left: 1em;
559+
display: none; /* initially */
560+
background-color: white;
561+
border: thick solid red;
562+
padding: 2em;
563+
}
544564
`

gopls/internal/server/server.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,16 @@ func (s *server) initWeb() (*web, error) {
254254
rootMux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
255255
http.Redirect(w, req, "/assets/favicon.ico", http.StatusMovedPermanently)
256256
})
257+
rootMux.HandleFunc("/hang", func(w http.ResponseWriter, req *http.Request) {
258+
// This endpoint hangs until cancelled.
259+
// It is used by JS to detect server disconnect.
260+
<-req.Context().Done()
261+
})
262+
rootMux.Handle("/assets/", http.FileServer(http.FS(assets)))
257263

258264
secret := "/gopls/" + base64.RawURLEncoding.EncodeToString(token)
259265
webMux := http.NewServeMux()
260266
rootMux.Handle(secret+"/", http.StripPrefix(secret, webMux))
261-
rootMux.Handle("/assets/", http.FileServer(http.FS(assets)))
262267

263268
webServer := &http.Server{Addr: listener.Addr().String(), Handler: rootMux}
264269
go func() {

0 commit comments

Comments
 (0)