Skip to content

Commit

Permalink
Merge pull request #575 from ripienaar/api_limit_reached
Browse files Browse the repository at this point in the history
Schema and structures for io.nats.jetstream.advisory.v1.api_limit_reached
  • Loading branch information
ripienaar authored Sep 20, 2024
2 parents 782ac92 + 90ff27b commit 9c54a17
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
46 changes: 46 additions & 0 deletions api/jetstream/advisory/api_limit_reached.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package advisory

import (
"github.com/nats-io/jsm.go/api/event"
)

// JSAPILimitReachedAdvisoryV1 is an advisory published when the system drops incoming
// API requests in order to protect itself against denial of service.
//
// NATS Schema: io.nats.jetstream.advisory.v1.api_limit_reached
type JSAPILimitReachedAdvisoryV1 struct {
event.NATSEvent

Server string `json:"server"` // Server that created the event, name or ID
Domain string `json:"domain,omitempty"` // Domain the server belongs to
Dropped int64 `json:"dropped"` // How many messages did we drop from the queue
}

func init() {
err := event.RegisterTextCompactTemplate("io.nats.jetstream.advisory.v1.api_limit_reached", `{{ .Time | ShortTime }} [JS API Limit] {{ .Server }} {{ if .Domain }}in domain {{.Domain}} {{ end }}discarded {{ .Dropped | Int64Commas }} messages`)
if err != nil {
panic(err)
}

err = event.RegisterTextExtendedTemplate("io.nats.jetstream.advisory.v1.api_limit_reached", `
[{{ .Time | ShortTime }}] [{{ .ID }}] API Limit Reached
Dropped: {{ .Dropped }}
Domain: {{ .Domain }}`)
if err != nil {
panic(err)
}
}
43 changes: 43 additions & 0 deletions schema_source/jetstream/advisory/v1/api_limit_reached.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://nats.io/schemas/jetstream/advisory/v1/api_limit_reached.json",
"description": "An Advisory when the server takes action after very high number of pending API operations",
"title": "io.nats.jetstream.advisory.v1.api_limit_reached",
"type": "object",
"required": [
"type",
"id",
"timestamp",
"server",
"dropped"
],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "io.nats.jetstream.advisory.v1.api_limit_reached"
},
"id": {
"type": "string",
"description": "Unique correlation ID for this event"
},
"timestamp": {
"type": "string",
"description": "The time this event was created in RFC3339 format"
},
"server": {
"type": "string",
"description": "The server this event originates from, either a generated ID or the configured name",
"minLength": 1
},
"domain": {
"type": "string",
"minimum": 1,
"description": "The domain of the JetStreamServer"
},
"dropped": {
"type": "integer",
"description": "The number of messages removed from the QPI queue"
}
}
}
43 changes: 43 additions & 0 deletions schemas/jetstream/advisory/v1/api_limit_reached.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://nats.io/schemas/jetstream/advisory/v1/api_limit_reached.json",
"description": "An Advisory when the server takes action after very high number of pending API operations",
"title": "io.nats.jetstream.advisory.v1.api_limit_reached",
"type": "object",
"required": [
"type",
"id",
"timestamp",
"server",
"dropped"
],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"const": "io.nats.jetstream.advisory.v1.api_limit_reached"
},
"id": {
"type": "string",
"description": "Unique correlation ID for this event"
},
"timestamp": {
"type": "string",
"description": "The time this event was created in RFC3339 format"
},
"server": {
"type": "string",
"description": "The server this event originates from, either a generated ID or the configured name",
"minLength": 1
},
"domain": {
"type": "string",
"minimum": 1,
"description": "The domain of the JetStreamServer"
},
"dropped": {
"type": "integer",
"description": "The number of messages removed from the QPI queue"
}
}
}

0 comments on commit 9c54a17

Please sign in to comment.