Add exclude-listeners parameter
This commit is contained in:
parent
ebc48e3615
commit
c0382eaad9
10 changed files with 47 additions and 22 deletions
|
@ -3,6 +3,7 @@ package terminator
|
|||
import (
|
||||
"github.com/jouir/pgterminate/base"
|
||||
"github.com/jouir/pgterminate/log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -11,7 +12,7 @@ import (
|
|||
type Terminator struct {
|
||||
config *base.Config
|
||||
db *base.Db
|
||||
sessions chan base.Session
|
||||
sessions chan *base.Session
|
||||
done chan bool
|
||||
}
|
||||
|
||||
|
@ -64,16 +65,16 @@ func (t *Terminator) Run() {
|
|||
}
|
||||
|
||||
// notify sends sessions to channel
|
||||
func (t *Terminator) notify(sessions []base.Session) {
|
||||
func (t *Terminator) notify(sessions []*base.Session) {
|
||||
for _, session := range sessions {
|
||||
t.sessions <- session
|
||||
}
|
||||
}
|
||||
|
||||
// filter removes sessions according to include and exclude users settings
|
||||
// filterUsers removes sessions according to include and exclude users settings
|
||||
// when include users slice and regex are not set, append all sessions except excluded users
|
||||
// otherwise, append included users
|
||||
func (t *Terminator) filter(sessions []base.Session) (filtered []base.Session) {
|
||||
func (t *Terminator) filterUsers(sessions []*base.Session) (filtered []*base.Session) {
|
||||
includeUsers, includeRegex := t.config.IncludeUsers, t.config.IncludeUsersRegexCompiled
|
||||
excludeUsers, excludeRegex := t.config.ExcludeUsers, t.config.ExcludeUsersRegexCompiled
|
||||
|
||||
|
@ -94,6 +95,24 @@ func (t *Terminator) filter(sessions []base.Session) (filtered []base.Session) {
|
|||
return filtered
|
||||
}
|
||||
|
||||
// filterListeners excludes sessions with last query starting with "LISTEN"
|
||||
func (t *Terminator) filterListeners(sessions []*base.Session) (filtered []*base.Session) {
|
||||
for _, session := range sessions {
|
||||
if (session.Query == "") || (session.Query != "" && !strings.HasPrefix(strings.ToUpper(session.Query), "LISTEN")) {
|
||||
filtered = append(filtered, session)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// filter executes all filter functions on a list of sessions
|
||||
func (t *Terminator) filter(sessions []*base.Session) (filtered []*base.Session) {
|
||||
filtered = sessions
|
||||
filtered = t.filterUsers(filtered)
|
||||
filtered = t.filterListeners(filtered)
|
||||
return filtered
|
||||
}
|
||||
|
||||
// terminate terminates gracefully
|
||||
func (t *Terminator) terminate() {
|
||||
log.Info("Disconnecting from instance")
|
||||
|
@ -103,7 +122,7 @@ func (t *Terminator) terminate() {
|
|||
// activeSessions returns a list of active sessions
|
||||
// A session is active when state is "active" and state has changed before elapsed seconds
|
||||
// seconds
|
||||
func activeSessions(sessions []base.Session, elapsed float64) (result []base.Session) {
|
||||
func activeSessions(sessions []*base.Session, elapsed float64) (result []*base.Session) {
|
||||
for _, session := range sessions {
|
||||
if session.State == "active" && session.StateDuration > elapsed {
|
||||
result = append(result, session)
|
||||
|
@ -115,7 +134,7 @@ func activeSessions(sessions []base.Session, elapsed float64) (result []base.Ses
|
|||
// idleSessions returns a list of idle sessions
|
||||
// A sessions is idle when state is "idle", "idle in transaction" or "idle in transaction
|
||||
// (aborted)"and state has changed before elapsed seconds
|
||||
func idleSessions(sessions []base.Session, elapsed float64) (result []base.Session) {
|
||||
func idleSessions(sessions []*base.Session, elapsed float64) (result []*base.Session) {
|
||||
for _, session := range sessions {
|
||||
if session.IsIdle() && session.StateDuration > elapsed {
|
||||
result = append(result, session)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue