1
- // Copyright 2022-2024 EMQ Technologies Co., Ltd.
1
+ // Copyright 2022-2025 EMQ Technologies Co., Ltd.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import (
20
20
"crypto/sha256"
21
21
"crypto/sha512"
22
22
b64 "encoding/base64"
23
+ "encoding/gob"
23
24
"encoding/json"
24
25
"fmt"
25
26
"hash/crc32"
@@ -43,6 +44,7 @@ import (
43
44
)
44
45
45
46
func registerMiscFunc () {
47
+ gob .Register (& ringqueue {})
46
48
builtins ["bypass" ] = builtinFunc {
47
49
fType : ast .FuncTypeScalar ,
48
50
exec : func (ctx api.FunctionContext , args []interface {}) (interface {}, bool ) {
@@ -692,19 +694,19 @@ func jsonCall(ctx api.StreamContext, args []interface{}) (interface{}, error) {
692
694
// page Rotate storage for in memory cache
693
695
// Not thread safe!
694
696
type ringqueue struct {
695
- data []interface {}
696
- h int
697
- t int
698
- l int
699
- size int
697
+ Data []any
698
+ H int
699
+ T int
700
+ L int
701
+ Size int
700
702
}
701
703
702
704
func newRingqueue (size int ) * ringqueue {
703
705
return & ringqueue {
704
- data : make ([]interface {}, size ),
705
- h : 0 , // When deleting, head++, if tail == head, it is empty
706
- t : 0 , // When append, tail++, if tail== head, it is full
707
- size : size ,
706
+ Data : make ([]interface {}, size ),
707
+ H : 0 , // When deleting, head++, if tail == head, it is empty
708
+ T : 0 , // When append, tail++, if tail== head, it is full
709
+ Size : size ,
708
710
}
709
711
}
710
712
@@ -719,41 +721,41 @@ func (p *ringqueue) fill(item interface{}) {
719
721
720
722
// append item if list is not full and return true; otherwise return false
721
723
func (p * ringqueue ) append (item interface {}) bool {
722
- if p .l == p .size { // full
724
+ if p .L == p .Size { // full
723
725
return false
724
726
}
725
- p .data [p .t ] = item
726
- p .t ++
727
- if p .t == p .size {
728
- p .t = 0
727
+ p .Data [p .T ] = item
728
+ p .T ++
729
+ if p .T == p .Size {
730
+ p .T = 0
729
731
}
730
- p .l ++
732
+ p .L ++
731
733
return true
732
734
}
733
735
734
736
// fetch get the first item in the cache and remove
735
737
func (p * ringqueue ) fetch () (interface {}, bool ) {
736
- if p .l == 0 {
738
+ if p .L == 0 {
737
739
return nil , false
738
740
}
739
- result := p .data [p .h ]
740
- p .h ++
741
- if p .h == p .size {
742
- p .h = 0
741
+ result := p .Data [p .H ]
742
+ p .H ++
743
+ if p .H == p .Size {
744
+ p .H = 0
743
745
}
744
- p .l --
746
+ p .L --
745
747
return result , true
746
748
}
747
749
748
750
// peek get the first item in the cache but keep it
749
751
func (p * ringqueue ) peek () (interface {}, bool ) {
750
- if p .l == 0 {
752
+ if p .L == 0 {
751
753
return nil , false
752
754
}
753
- result := p .data [p .h ]
755
+ result := p .Data [p .H ]
754
756
return result , true
755
757
}
756
758
757
759
func (p * ringqueue ) isFull () bool {
758
- return p .l == p .size
760
+ return p .L == p .Size
759
761
}
0 commit comments