X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a58e9ca25aaa0963545f256985eb75b2f840e80f..f672f727fe79bf6642a2daab641a1ef5c84648df:/services/ws/event_source.go diff --git a/services/ws/event_source.go b/services/ws/event_source.go index fe1876cc27..309dab7a40 100644 --- a/services/ws/event_source.go +++ b/services/ws/event_source.go @@ -1,10 +1,13 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( "context" "database/sql" "strconv" - "strings" "sync" "sync/atomic" "time" @@ -13,24 +16,10 @@ import ( "github.com/lib/pq" ) -type pgConfig map[string]string - -func (c pgConfig) ConnectionString() string { - s := "" - for k, v := range c { - s += k - s += "='" - s += strings.Replace( - strings.Replace(v, `\`, `\\`, -1), - `'`, `\'`, -1) - s += "' " - } - return s -} - type pgEventSource struct { - DataSource string - QueueSize int + DataSource string + MaxOpenConns int + QueueSize int db *sql.DB pqListener *pq.Listener @@ -115,6 +104,10 @@ func (ps *pgEventSource) Run() { logger(nil).WithError(err).Error("sql.Open failed") return } + if ps.MaxOpenConns <= 0 { + logger(nil).Warn("no database connection limit configured -- consider setting PostgresPool>0 in arvados-ws configuration file") + } + db.SetMaxOpenConns(ps.MaxOpenConns) if err = db.Ping(); err != nil { logger(nil).WithError(err).Error("db.Ping failed") return @@ -234,9 +227,17 @@ func (ps *pgEventSource) NewSink() eventSink { } func (ps *pgEventSource) DB() *sql.DB { + ps.WaitReady() return ps.db } +func (ps *pgEventSource) DBHealth() error { + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second)) + defer cancel() + var i int + return ps.db.QueryRowContext(ctx, "SELECT 1").Scan(&i) +} + func (ps *pgEventSource) DebugStatus() interface{} { ps.mtx.Lock() defer ps.mtx.Unlock() @@ -252,6 +253,7 @@ func (ps *pgEventSource) DebugStatus() interface{} { "QueueDelay": stats.Duration(ps.lastQDelay), "Sinks": len(ps.sinks), "SinksBlocked": blocked, + "DBStats": ps.db.Stats(), } } @@ -270,7 +272,7 @@ func (sink *pgEventSink) Stop() { // Ensure this sink cannot fill up and block the // server-side queue (which otherwise could in turn // block our mtx.Lock() here) - for _ = range sink.channel { + for range sink.channel { } }() sink.source.mtx.Lock()