-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadbalancer_endpoint_pool.go
More file actions
79 lines (63 loc) · 2.04 KB
/
loadbalancer_endpoint_pool.go
File metadata and controls
79 lines (63 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2023 xgfone
//
// 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 loadbalancer
import "sync"
var (
eppool4 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 4)) }}
eppool8 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 8)) }}
eppool16 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 16)) }}
eppool32 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 32)) }}
eppool64 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 64)) }}
eppool128 = sync.Pool{New: func() any { return NewStatic(make(Endpoints, 0, 128)) }}
)
// Acquire acquires a preallocated zero-length endpoints from the pool.
func Acquire(expectedMaxCap int) *Static {
switch {
case expectedMaxCap <= 4:
return eppool4.Get().(*Static)
case expectedMaxCap <= 8:
return eppool8.Get().(*Static)
case expectedMaxCap <= 16:
return eppool16.Get().(*Static)
case expectedMaxCap <= 32:
return eppool32.Get().(*Static)
case expectedMaxCap <= 64:
return eppool64.Get().(*Static)
default:
return eppool128.Get().(*Static)
}
}
// Release releases the static endpoints back into the pool.
func Release(eps *Static) {
if eps == nil || cap(eps.Endpoints) == 0 {
return
}
clear(eps.Endpoints)
eps.Endpoints = eps.Endpoints[:0]
cap := cap(eps.Endpoints)
switch {
case cap < 8:
eppool4.Put(eps)
case cap < 16:
eppool8.Put(eps)
case cap < 32:
eppool16.Put(eps)
case cap < 64:
eppool32.Put(eps)
case cap < 128:
eppool64.Put(eps)
default:
eppool128.Put(eps)
}
}