This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathhandler_common.go
58 lines (51 loc) · 1.61 KB
/
handler_common.go
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
package langserver
import (
"errors"
"fmt"
"log"
"sync"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sourcegraph/go-langserver/langserver/util"
"github.com/sourcegraph/go-lsp"
)
// HandlerCommon contains functionality that both the build and lang
// handlers need. They do NOT share the memory of this HandlerCommon
// struct; it is just common functionality. (Unlike HandlerCommon,
// HandlerShared is shared in-memory.)
type HandlerCommon struct {
mu sync.Mutex // guards all fields
RootFSPath string // root path of the project's files in the (possibly virtual) file system, without the "file://" prefix (typically /src/github.com/foo/bar)
shutdown bool
tracer opentracing.Tracer
}
func (h *HandlerCommon) Reset(rootURI lsp.DocumentURI) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.shutdown {
return errors.New("unable to reset a server that is shutting down")
}
if !util.IsURI(rootURI) {
return fmt.Errorf("invalid root path %q: must be file:// URI", rootURI)
}
h.RootFSPath = util.UriToPath(rootURI) // retain leading slash
return nil
}
// ShutDown marks this server as being shut down and causes all future calls to checkReady to return an error.
func (h *HandlerCommon) ShutDown() {
h.mu.Lock()
if h.shutdown {
log.Printf("Warning: server received a shutdown request after it was already shut down.")
}
h.shutdown = true
h.mu.Unlock()
}
// CheckReady returns an error if the handler has been shut
// down.
func (h *HandlerCommon) CheckReady() error {
h.mu.Lock()
defer h.mu.Unlock()
if h.shutdown {
return errors.New("server is shutting down")
}
return nil
}