-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
127 lines (105 loc) · 3.17 KB
/
handler.go
File metadata and controls
127 lines (105 loc) · 3.17 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// This file is part of kvlog.
//
// Copyright 2019, 2020, 2021 Alexander Metzner.
//
// 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 kvlog
import (
"bytes"
"io"
"sync"
)
var (
// Defines the size of an async handler's buffer that is preallocated.
AsyncHandlerBufferSize = 2048
// Defines the number of preallocated buffers in a pool of buffers.
AsyncHandlerPoolSize = 64
// Number of log events to buffer in an async handler's channel.
AsyncHandlerChannelSize = 1024
)
type syncHandler struct {
lock sync.Mutex
out io.Writer
formatter Formatter
}
// NewSyncHandler creates a new Handler that works synchronously by writing log events formatted with f to o.
// f works by directly writing bytes to o; no buffering is done between those.
func NewSyncHandler(o io.Writer, f Formatter) Handler {
return &syncHandler{
out: o,
formatter: f,
}
}
func (h *syncHandler) Close() {}
func (h *syncHandler) deliver(e *Event) {
h.lock.Lock()
defer h.lock.Unlock()
h.formatter.Format(h.out, e)
}
type asyncHandler struct {
lock sync.Mutex
formatter Formatter
pool *sync.Pool
bufferChan chan *bytes.Buffer
finishedChan chan struct{}
}
// NewAsyncHandler creates a new Handler that works asynchronously. f is applied on every event writing to a
// bytes.Buffer. This happens in the same goroutine as emitting the log. The resulting bytes are them send
// over a channel to a dedicated goroutine which copies the bytes onto o.
func NewAsyncHandler(o io.Writer, f Formatter) Handler {
bufferChan := make(chan *bytes.Buffer, AsyncHandlerChannelSize)
finishedChan := make(chan struct{})
pool := &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, AsyncHandlerBufferSize))
},
}
for i := 0; i < AsyncHandlerPoolSize; i++ {
pool.Put(bytes.NewBuffer(make([]byte, 0, AsyncHandlerBufferSize)))
}
go func() {
defer close(finishedChan)
for buf := range bufferChan {
// TODO: Handle error
o.Write(buf.Bytes())
buf.Reset()
pool.Put(buf)
}
}()
return &asyncHandler{
formatter: f,
bufferChan: bufferChan,
finishedChan: finishedChan,
pool: pool,
}
}
func (h *asyncHandler) Close() {
close(h.bufferChan)
<-h.finishedChan
}
func (h *asyncHandler) deliver(e *Event) {
h.lock.Lock()
defer h.lock.Unlock()
buf := h.pool.Get().(*bytes.Buffer)
h.formatter.Format(buf, e)
h.bufferChan <- buf
}
type noopHandler struct{}
func (*noopHandler) Close() {}
func (*noopHandler) deliver(*Event) {}
// NoOpHandler creates a no-operation handler that simply discards every event. Use this handler to silence
// logging output completely.
func NoOpHandler() Handler {
return &noopHandler{}
}