Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add channel statistics data return command #1494

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions nsqd/protocol_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
frameTypeResponse int32 = 0
frameTypeError int32 = 1
frameTypeMessage int32 = 2
frameTypeStats int32 = 3
)

var separatorBytes = []byte(" ")
Expand Down Expand Up @@ -196,6 +197,8 @@ func (p *protocolV2) Exec(client *clientV2, params [][]byte) ([]byte, error) {
return p.CLS(client, params)
case bytes.Equal(params[0], []byte("AUTH")):
return p.AUTH(client, params)
case bytes.Equal(params[0], []byte("STATS")):
return p.STATS(client, params)
}
return nil, protocol.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("invalid command %s", params[0]))
}
Expand Down Expand Up @@ -701,6 +704,52 @@ func (p *protocolV2) FIN(client *clientV2, params [][]byte) ([]byte, error) {
return nil, nil
}

func (p *protocolV2) STATS(client *clientV2, params [][]byte) ([]byte, error) {
var (
state = atomic.LoadInt32(&client.State)
channel = client.Channel
)
if state != stateSubscribed && state != stateClosing {
return nil, protocol.NewFatalClientErr(nil, "E_INVALID", "cannot STATS in current state")
}

channel.inFlightMutex.Lock()
inflight := len(channel.inFlightMessages)
channel.inFlightMutex.Unlock()
channel.deferredMutex.Lock()
deferred := len(channel.deferredMessages)
channel.deferredMutex.Unlock()
resp, err := json.Marshal(struct {
ChannelName string `json:"channel_name"`
Depth int64 `json:"depth"`
BackendDepth int64 `json:"backend_depth"`
InFlightCount int `json:"in_flight_count"`
DeferredCount int `json:"deferred_count"`
MessageCount uint64 `json:"message_count"`
RequeueCount uint64 `json:"requeue_count"`
TimeoutCount uint64 `json:"timeout_count"`
Paused bool `json:"paused"`
}{
ChannelName: channel.name,
Depth: channel.Depth(),
BackendDepth: channel.backend.Depth(),
InFlightCount: inflight,
DeferredCount: deferred,
MessageCount: atomic.LoadUint64(&channel.messageCount),
RequeueCount: atomic.LoadUint64(&channel.requeueCount),
TimeoutCount: atomic.LoadUint64(&channel.timeoutCount),
Paused: channel.IsPaused(),
})
if err != nil {
return nil, protocol.NewFatalClientErr(err, "E_STATS_ERROR", "STATS error "+err.Error())
}

if err = p.Send(client, frameTypeStats, resp); err != nil {
return nil, protocol.NewFatalClientErr(err, "E_STATS_FAILED", "STATS failed "+err.Error())
}
return nil, nil
}

func (p *protocolV2) REQ(client *clientV2, params [][]byte) ([]byte, error) {
state := atomic.LoadInt32(&client.State)
if state != stateSubscribed && state != stateClosing {
Expand Down