// Copyright 2020-2022 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 parserimport ()const (AckDomainTokenPos = iota + 2AckAccHashTokenPosAckStreamTokenPosAckConsumerTokenPosAckNumDeliveredTokenPosAckStreamSeqTokenPosAckConsumerSeqTokenPosAckTimestampSeqTokenPosAckNumPendingTokenPos)const v1TokenCounts, v2TokenCounts = 9, 12varErrInvalidSubjectFormat = errors.New("invalid format of ACK subject")// Quick parser for positive numbers in ack reply encoding.// NOTE: This parser does not detect uint64 overflowfunc ( string) ( uint64) {iflen() == 0 {return0 }// ASCII numbers 0-9const ( = 48 = 57 )for , := range {if < || > {return0 } = *10 + uint64() - }return}func ( string) ([AckNumPendingTokenPos + 1]string, error) {var [AckNumPendingTokenPos + 1]string// // Newer server will include the domain name and account hash in the subject, // and a token at the end. // // Old subject was: // $JS.ACK.<stream>.<consumer>.<delivered>.<sseq>.<cseq>.<tm>.<pending> // // New subject would be: // $JS.ACK.<domain>.<account hash>.<stream>.<consumer>.<delivered>.<sseq>.<cseq>.<tm>.<pending>.<a token with a random value> // // v1 has 9 tokens, v2 has 12, but we must not be strict on the 12th since // it may be removed in the future. Also, the library has no use for it. // The point is that a v2 ACK subject is valid if it has at least 11 tokens. // := strings.Count(, ".") + 1// If lower than 9 or more than 9 but less than 11, report an errorif < v1TokenCounts || ( > v1TokenCounts && < v2TokenCounts-1) {return , ErrInvalidSubjectFormat }if !strings.HasPrefix(, "$JS.ACK.") {return , fmt.Errorf("%w: subject should start with $JS.ACK", ErrInvalidSubjectFormat) }for := range {// For v1 style, leave the domain and hash tokens empty so that the // rest of the library references known fields at a constant location.if == v1TokenCounts && ( == AckDomainTokenPos || == AckAccHashTokenPos) {continue } [], , _ = strings.Cut(, ".") }// If domain is "_", replace with empty value.if [AckDomainTokenPos] == "_" { [AckDomainTokenPos] = "" }return , nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.