fe0435011c
Git over HTTPS has a high request startup latency, since the SSL negotiation can take up to a second. In order to reduce this latency, connections should be left open to the Git server across requests (or invocations of the git commandline). Reduce SSL startup latency by running a daemon job that keeps connections open to a Git server. The daemon job (git-remote-persistent-https--proxy) is started on the first request through the client binary (git-remote-persistent-https) and remains running for 24 hours after the last request, or until a new daemon binary is placed in the PATH. The client determines the daemon's HTTP address by communicating over a UNIX socket with the daemon. From there, the rest of the Git protocol work is delegated to the "git-remote-http" binary, with the environment's http_proxy set to the daemon. Accessing /pub/scm/linux/kernel/git/torvalds/linux repository hosted at kernel.googlesource.com with "git ls-remote" over https:// and persistent-https:// 5 times shows that the first request takes about the same time (0.193s vs 0.208s---there is a slight set-up cost for the local proxy); as expected, the other four requests are much faster (~0.18s vs ~0.08s). Incidentally, this also has the benefit of HTTP keep-alive working across Git command invocations. Its common for servers to use a 5 minute keep-alive on an HTTP 1.1 connection. Git-over-HTTP commonly uses Transfer-Encoding: chunked on replies, so keep-alive will generally just work, even though a pack stream's length isn't known in advance. Because the helper is an external process holding that connection open, we also benefit from being able to reuse an existing TCP connection to the server. The same "git ls-remote" test against http:// vs persistent-https:// URL shows that the former takes ~0.09s while the first request for the latter is about 0.134s with set-up cost, and subsequent requests are ~0.065s, shaving around one RTT to the server. Signed-off-by: Colby Ranger <cranger@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
)
|
|
|
|
// A Socket is a wrapper around a Unix socket that verifies directory
|
|
// permissions.
|
|
type Socket struct {
|
|
Dir string
|
|
}
|
|
|
|
func defaultDir() string {
|
|
sockPath := ".git-credential-cache"
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
return filepath.Join(home, sockPath)
|
|
}
|
|
log.Printf("socket: cannot find HOME path. using relative directory %q for socket", sockPath)
|
|
return sockPath
|
|
}
|
|
|
|
// DefaultSocket is a Socket in the $HOME/.git-credential-cache directory.
|
|
var DefaultSocket = Socket{Dir: defaultDir()}
|
|
|
|
// Listen announces the local network address of the unix socket. The
|
|
// permissions on the socket directory are verified before attempting
|
|
// the actual listen.
|
|
func (s Socket) Listen() (net.Listener, error) {
|
|
network, addr := "unix", s.Path()
|
|
if err := s.mkdir(); err != nil {
|
|
return nil, &net.OpError{Op: "listen", Net: network, Addr: &net.UnixAddr{Name: addr, Net: network}, Err: err}
|
|
}
|
|
return net.Listen(network, addr)
|
|
}
|
|
|
|
// Dial connects to the unix socket. The permissions on the socket directory
|
|
// are verified before attempting the actual dial.
|
|
func (s Socket) Dial() (net.Conn, error) {
|
|
network, addr := "unix", s.Path()
|
|
if err := s.checkPermissions(); err != nil {
|
|
return nil, &net.OpError{Op: "dial", Net: network, Addr: &net.UnixAddr{Name: addr, Net: network}, Err: err}
|
|
}
|
|
return net.Dial(network, addr)
|
|
}
|
|
|
|
// Path returns the fully specified file name of the unix socket.
|
|
func (s Socket) Path() string {
|
|
return filepath.Join(s.Dir, "persistent-https-proxy-socket")
|
|
}
|
|
|
|
func (s Socket) mkdir() error {
|
|
if err := s.checkPermissions(); err == nil {
|
|
return nil
|
|
} else if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(s.Dir, 0700); err != nil {
|
|
return err
|
|
}
|
|
return s.checkPermissions()
|
|
}
|
|
|
|
func (s Socket) checkPermissions() error {
|
|
fi, err := os.Stat(s.Dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !fi.IsDir() {
|
|
return fmt.Errorf("socket: got file, want directory for %q", s.Dir)
|
|
}
|
|
if fi.Mode().Perm() != 0700 {
|
|
return fmt.Errorf("socket: got perm %o, want 700 for %q", fi.Mode().Perm(), s.Dir)
|
|
}
|
|
if st := fi.Sys().(*syscall.Stat_t); int(st.Uid) != os.Getuid() {
|
|
return fmt.Errorf("socket: got uid %d, want %d for %q", st.Uid, os.Getuid(), s.Dir)
|
|
}
|
|
return nil
|
|
}
|