+
+var (
+ arvadosClientFromEnv = arvados.NewClientFromEnv()
+ siteFS arvados.CustomFileSystem
+ siteFSMtx sync.Mutex
+)
+
+func open(fnm string) (io.ReadCloser, error) {
+ if os.Getenv("ARVADOS_API_HOST") == "" {
+ return os.Open(fnm)
+ }
+ m := collectionInPathRe.FindStringSubmatch(fnm)
+ if m == nil {
+ return os.Open(fnm)
+ }
+ uuid := m[2]
+ mnt := "/mnt/" + uuid + "/"
+ if !strings.HasPrefix(fnm, mnt) {
+ return os.Open(fnm)
+ }
+
+ siteFSMtx.Lock()
+ defer siteFSMtx.Unlock()
+ if siteFS == nil {
+ log.Info("setting up Arvados client")
+ ac, err := arvadosclient.New(arvadosClientFromEnv)
+ if err != nil {
+ return nil, err
+ }
+ ac.Client = arvados.DefaultSecureClient
+ kc := keepclient.New(ac)
+ // Don't use keepclient's default short timeouts.
+ kc.HTTPClient = arvados.DefaultSecureClient
+ // Guess max concurrent readers, hope to avoid cache
+ // thrashing.
+ kc.BlockCache = &keepclient.BlockCache{MaxBlocks: runtime.NumCPU() * 3}
+ siteFS = arvadosClientFromEnv.SiteFileSystem(kc)
+ }
+
+ log.Infof("reading %q from %s using Arvados client", fnm[len(mnt):], uuid)
+ return siteFS.Open("by_id/" + uuid + "/" + fnm[len(mnt):])
+}