-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_buffer.go
More file actions
49 lines (43 loc) · 813 Bytes
/
ip_buffer.go
File metadata and controls
49 lines (43 loc) · 813 Bytes
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
package main
import (
"fmt"
"net/netip"
"os"
)
type ipBuffer struct {
path string
buffer []netip.Addr
bufferSize int
bufferCap int
}
func newIPBuffer(path string, cap int) (*ipBuffer) {
return &ipBuffer{
path: path,
buffer: make([]netip.Addr, cap),
bufferSize: 0,
bufferCap: cap,
}
}
func (b *ipBuffer) flush() error {
file, err := os.OpenFile("ip.txt", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
for i := range b.bufferSize {
_, err := fmt.Fprintln(file, b.buffer[i])
if err != nil {
return err
}
}
b.bufferSize = 0
return nil
}
func (b *ipBuffer) append(addr netip.Addr) error {
b.buffer[b.bufferSize] = addr
b.bufferSize++
if b.bufferSize == b.bufferCap {
return b.flush()
}
return nil
}