-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogstream.go
82 lines (73 loc) · 1.83 KB
/
logstream.go
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
package logstream
import (
"errors"
"fmt"
"runtime"
"time"
"github.com/diy-cloud/logstream/v2/log"
"github.com/diy-cloud/logstream/v2/log/logbuffer"
"github.com/snowmerak/gopool"
)
var receiveSignal = make(chan string, 64)
var goroutinePool = gopool.New(int64(runtime.NumCPU() * 4096))
func init() {
go func() {
for topic := range receiveSignal {
t := topic
goroutinePool.Go(
func() interface{} {
key := []byte(t)
value, ok := trie.Lookup(key)
if !ok {
fmt.Println(time.Now().Format(time.RFC3339), "logstream: topic buffer not registered")
return nil
}
buffer := value.(logbuffer.LogBuffer)
log, err := buffer.Pop()
if err != nil {
fmt.Println(time.Now().Format(time.RFC3339), "logstream: topic buffer has any error: ", err.Error())
return nil
}
value, ok = consumersMap.Lookup(key)
if !ok {
fmt.Println(time.Now().Format(time.RFC3339), "logstream: topic consumers not registered")
return nil
}
consumers := value.(*Consumers)
consumers.Lock()
for _, consumer := range consumers.list {
err := consumer.Write(log)
if err != nil {
fmt.Println(time.Now().Format(time.RFC3339), "logstream: topic consumer has any error: ", err.Error())
}
}
consumers.Unlock()
return nil
},
)
}
}()
}
func SetGoroutineMaxSize(size int64) {
goroutinePool.SetMax(size)
}
func Write(topic string, log log.Log) error {
key := []byte(topic)
if _, ok := trie.Lookup(key); !ok {
return errors.New("topic not registered")
}
value, ok := trie.Lookup(key)
if !ok {
return errors.New("topic buffer not registered")
}
buffer := value.(logbuffer.LogBuffer)
buffer.Push(log)
if !ok {
return errors.New("topic signal not registered")
}
receiveSignal <- topic
return nil
}
func Wait() {
goroutinePool.Wait()
}