| 1 |
// Copyright 2013-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"time" |
| 18 |
) |
| 19 |
|
| 20 |
// ConnInfos represents a connection info list. We use pointers since it will be sorted. |
| 21 |
type ConnInfos []*ConnInfo |
| 22 |
|
| 23 |
// For sorting |
| 24 |
// Len returns length for sorting. |
| 25 |
func (cl ConnInfos) Len() int { return len(cl) } |
| 26 |
|
| 27 |
// Swap will sawap the elements. |
| 28 |
func (cl ConnInfos) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] } |
| 29 |
|
| 30 |
// SortOpt is a helper type to sort clients |
| 31 |
type SortOpt string |
| 32 |
|
| 33 |
// Possible sort options |
| 34 |
const ( |
| 35 |
ByCid SortOpt = "cid" // By connection ID |
| 36 |
ByStart SortOpt = "start" // By connection start time, same as CID |
| 37 |
BySubs SortOpt = "subs" // By number of subscriptions |
| 38 |
ByPending SortOpt = "pending" // By amount of data in bytes waiting to be sent to client |
| 39 |
ByOutMsgs SortOpt = "msgs_to" // By number of messages sent |
| 40 |
ByInMsgs SortOpt = "msgs_from" // By number of messages received |
| 41 |
ByOutBytes SortOpt = "bytes_to" // By amount of bytes sent |
| 42 |
ByInBytes SortOpt = "bytes_from" // By amount of bytes received |
| 43 |
ByLast SortOpt = "last" // By the last activity |
| 44 |
ByIdle SortOpt = "idle" // By the amount of inactivity |
| 45 |
ByUptime SortOpt = "uptime" // By the amount of time connections exist |
| 46 |
ByStop SortOpt = "stop" // By the stop time for a closed connection |
| 47 |
ByReason SortOpt = "reason" // By the reason for a closed connection |
| 48 |
ByRTT SortOpt = "rtt" // By the round trip time |
| 49 |
) |
| 50 |
|
| 51 |
// Individual sort options provide the Less for sort.Interface. Len and Swap are on cList. |
| 52 |
// CID |
| 53 |
type SortByCid struct{ ConnInfos } |
| 54 |
|
| 55 |
func (l SortByCid) Less(i, j int) bool { return l.ConnInfos[i].Cid < l.ConnInfos[j].Cid } |
| 56 |
|
| 57 |
// Number of Subscriptions |
| 58 |
type SortBySubs struct{ ConnInfos } |
| 59 |
|
| 60 |
func (l SortBySubs) Less(i, j int) bool { return l.ConnInfos[i].NumSubs < l.ConnInfos[j].NumSubs } |
| 61 |
|
| 62 |
// Pending Bytes |
| 63 |
type SortByPending struct{ ConnInfos } |
| 64 |
|
| 65 |
func (l SortByPending) Less(i, j int) bool { return l.ConnInfos[i].Pending < l.ConnInfos[j].Pending } |
| 66 |
|
| 67 |
// Outbound Msgs |
| 68 |
type SortByOutMsgs struct{ ConnInfos } |
| 69 |
|
| 70 |
func (l SortByOutMsgs) Less(i, j int) bool { return l.ConnInfos[i].OutMsgs < l.ConnInfos[j].OutMsgs } |
| 71 |
|
| 72 |
// Inbound Msgs |
| 73 |
type SortByInMsgs struct{ ConnInfos } |
| 74 |
|
| 75 |
func (l SortByInMsgs) Less(i, j int) bool { return l.ConnInfos[i].InMsgs < l.ConnInfos[j].InMsgs } |
| 76 |
|
| 77 |
// Outbound Bytes |
| 78 |
type SortByOutBytes struct{ ConnInfos } |
| 79 |
|
| 80 |
func (l SortByOutBytes) Less(i, j int) bool { return l.ConnInfos[i].OutBytes < l.ConnInfos[j].OutBytes } |
| 81 |
|
| 82 |
// Inbound Bytes |
| 83 |
type SortByInBytes struct{ ConnInfos } |
| 84 |
|
| 85 |
func (l SortByInBytes) Less(i, j int) bool { return l.ConnInfos[i].InBytes < l.ConnInfos[j].InBytes } |
| 86 |
|
| 87 |
// Last Activity |
| 88 |
type SortByLast struct{ ConnInfos } |
| 89 |
|
| 90 |
func (l SortByLast) Less(i, j int) bool { |
| 91 |
return l.ConnInfos[i].LastActivity.UnixNano() < l.ConnInfos[j].LastActivity.UnixNano() |
| 92 |
} |
| 93 |
|
| 94 |
// Idle time |
| 95 |
type SortByIdle struct { |
| 96 |
ConnInfos |
| 97 |
now time.Time |
| 98 |
} |
| 99 |
|
| 100 |
func (l SortByIdle) Less(i, j int) bool { |
| 101 |
return l.now.Sub(l.ConnInfos[i].LastActivity) < l.now.Sub(l.ConnInfos[j].LastActivity) |
| 102 |
} |
| 103 |
|
| 104 |
// Uptime |
| 105 |
type SortByUptime struct { |
| 106 |
ConnInfos |
| 107 |
now time.Time |
| 108 |
} |
| 109 |
|
| 110 |
func (l SortByUptime) Less(i, j int) bool { |
| 111 |
ci := l.ConnInfos[i] |
| 112 |
cj := l.ConnInfos[j] |
| 113 |
var upi, upj time.Duration |
| 114 |
if ci.Stop == nil || ci.Stop.IsZero() { |
| 115 |
upi = l.now.Sub(ci.Start) |
| 116 |
} else { |
| 117 |
upi = ci.Stop.Sub(ci.Start) |
| 118 |
} |
| 119 |
if cj.Stop == nil || cj.Stop.IsZero() { |
| 120 |
upj = l.now.Sub(cj.Start) |
| 121 |
} else { |
| 122 |
upj = cj.Stop.Sub(cj.Start) |
| 123 |
} |
| 124 |
return upi < upj |
| 125 |
} |
| 126 |
|
| 127 |
// Stop |
| 128 |
type SortByStop struct{ ConnInfos } |
| 129 |
|
| 130 |
func (l SortByStop) Less(i, j int) bool { |
| 131 |
ciStop := l.ConnInfos[i].Stop |
| 132 |
cjStop := l.ConnInfos[j].Stop |
| 133 |
return ciStop.Before(*cjStop) |
| 134 |
} |
| 135 |
|
| 136 |
// Reason |
| 137 |
type SortByReason struct{ ConnInfos } |
| 138 |
|
| 139 |
func (l SortByReason) Less(i, j int) bool { |
| 140 |
return l.ConnInfos[i].Reason < l.ConnInfos[j].Reason |
| 141 |
} |
| 142 |
|
| 143 |
// RTT - Default is descending |
| 144 |
type SortByRTT struct{ ConnInfos } |
| 145 |
|
| 146 |
func (l SortByRTT) Less(i, j int) bool { return l.ConnInfos[i].rtt < l.ConnInfos[j].rtt } |
| 147 |
|
| 148 |
// IsValid determines if a sort option is valid |
| 149 |
func (s SortOpt) IsValid() bool { |
| 150 |
switch s { |
| 151 |
case _EMPTY_, ByCid, ByStart, BySubs, ByPending, ByOutMsgs, ByInMsgs, ByOutBytes, ByInBytes, ByLast, ByIdle, ByUptime, ByStop, ByReason, ByRTT: |
| 152 |
return true |
| 153 |
default: |
| 154 |
return false |
| 155 |
} |
| 156 |
} |