Merge branch '20511-aborted-boot'
[arvados.git] / lib / controller / localdb / conn.go
index 5a3faa72790899aa69ba4408fbf47df7997c27af..6ab9e1450b2375da95fa57766f7e95e774742670 100644 (file)
@@ -8,6 +8,7 @@ import (
        "context"
        "encoding/json"
        "fmt"
+       "net"
        "net/http"
        "os"
        "sync"
@@ -19,6 +20,7 @@ import (
        "git.arvados.org/arvados.git/sdk/go/ctxlog"
        "git.arvados.org/arvados.git/sdk/go/httpserver"
        "github.com/hashicorp/yamux"
+       "github.com/jmoiron/sqlx"
        "github.com/sirupsen/logrus"
 )
 
@@ -27,6 +29,7 @@ type railsProxy = rpc.Conn
 type Conn struct {
        cluster                    *arvados.Cluster
        *railsProxy                // handles API methods that aren't defined on Conn itself
+       getdb                      func(context.Context) (*sqlx.DB, error)
        vocabularyCache            *arvados.Vocabulary
        vocabularyFileModTime      time.Time
        lastVocabularyRefreshCheck time.Time
@@ -37,16 +40,21 @@ type Conn struct {
        activeUsers      map[string]bool
        activeUsersLock  sync.Mutex
        activeUsersReset time.Time
+
+       wantContainerPriorityUpdate chan struct{}
 }
 
-func NewConn(cluster *arvados.Cluster) *Conn {
+func NewConn(bgCtx context.Context, cluster *arvados.Cluster, getdb func(context.Context) (*sqlx.DB, error)) *Conn {
        railsProxy := railsproxy.NewConn(cluster)
        railsProxy.RedactHostInErrors = true
        conn := Conn{
-               cluster:    cluster,
-               railsProxy: railsProxy,
+               cluster:                     cluster,
+               railsProxy:                  railsProxy,
+               getdb:                       getdb,
+               wantContainerPriorityUpdate: make(chan struct{}, 1),
        }
        conn.loginController = chooseLoginController(cluster, &conn)
+       go conn.runContainerPriorityUpdateThread(bgCtx)
        return &conn
 }
 
@@ -165,6 +173,26 @@ func (conn *Conn) UserAuthenticate(ctx context.Context, opts arvados.UserAuthent
        return conn.loginController.UserAuthenticate(ctx, opts)
 }
 
+var privateNetworks = func() (nets []*net.IPNet) {
+       for _, s := range []string{
+               "127.0.0.0/8",
+               "10.0.0.0/8",
+               "172.16.0.0/12",
+               "192.168.0.0/16",
+               "169.254.0.0/16",
+               "::1/128",
+               "fe80::/10",
+               "fc00::/7",
+       } {
+               _, n, err := net.ParseCIDR(s)
+               if err != nil {
+                       panic(fmt.Sprintf("privateNetworks: %q: %s", s, err))
+               }
+               nets = append(nets, n)
+       }
+       return
+}()
+
 func httpErrorf(code int, format string, args ...interface{}) error {
        return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)
 }