forked from sourcegraph/go-langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_shared.go
42 lines (36 loc) · 1.32 KB
/
handler_shared.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
package langserver
import (
"fmt"
"strings"
"sync"
"github.com/sourcegraph/ctxvfs"
)
// HandlerShared contains data structures that a build server and its
// wrapped lang server may share in memory.
type HandlerShared struct {
Mu sync.Mutex // guards all fields
Shared bool // true if this struct is shared with a build server
FS ctxvfs.NameSpace // full filesystem (mounts both deps and overlay)
overlayFSMu sync.Mutex // guards overlayFS map
overlayFS map[string][]byte // files to overlay
OverlayMountPath string // mount point of overlay on fs (usually /src/github.com/foo/bar)
}
func (h *HandlerShared) Reset(overlayRootURI string, useOSFS bool) error {
h.Mu.Lock()
defer h.Mu.Unlock()
h.overlayFSMu.Lock()
defer h.overlayFSMu.Unlock()
h.overlayFS = map[string][]byte{}
h.FS = ctxvfs.NameSpace{}
if !strings.HasPrefix(overlayRootURI, "file:///") {
return fmt.Errorf("invalid overlay root URI %q: must be file:///", overlayRootURI)
}
h.OverlayMountPath = strings.TrimPrefix(overlayRootURI, "file://")
if useOSFS {
// The overlay FS takes precedence, but we fall back to the OS
// file system.
h.FS.Bind("/", ctxvfs.OS("/"), "/", ctxvfs.BindAfter)
}
h.FS.Bind("/", ctxvfs.Sync(&h.overlayFSMu, ctxvfs.Map(h.overlayFS)), "/", ctxvfs.BindBefore)
return nil
}