Separate from ion-sfu (#171)

* Separate from ion-sfu

changes:
1. extract pkg/buffer, twcc, sfu, relay, stats, logger

2. to solve cycle import, move ion-sfu/pkg/logger to pkg/sfu/logger

3. replace pion/ion-sfu => ./
reason: will change import pion/ion-sfu/pkg/* to livekit-server/pkg/*
after this pr merged. Just not change any code in this pr, because it
will confused with the separate code from ion-sfu in review.

* Move code from ion-sfu to pkg/sfu

* fix build error for resovle conflict

Co-authored-by: cnderrauber <zengjie9004@gmail.com>
This commit is contained in:
cnderrauber
2021-11-09 12:03:16 +08:00
committed by GitHub
co-authored by cnderrauber
parent 289ebd32ff
commit 1e1aaeb86b
58 changed files with 12210 additions and 384 deletions
+133
View File
@@ -0,0 +1,133 @@
// Copyright 2019 Jorn Friedrich Dreyer
// Modified 2021 Serhii Mikhno
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance 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 logger defines a default implementation of the github.com/go-logr/logr
// interfaces built on top of zerolog (github.com/rs/zerolog) and is the default
// implementation for ion-sfu released binaries.
// This package separates log level into two different concepts:
// - V-Level - verbosity level, number, that every logger has.
// A higher value that means more logs will be written.
// - Log-level - usual log level (TRACE|DEBUG|INFO).
// Every log row combines those two values.
// You can set log level to TRACE and see all general traces.
// To see more logs just add -v
package logger
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/go-logr/logr"
"github.com/go-logr/zerologr"
"github.com/rs/zerolog"
)
const (
timeFormat = "2006-01-02 15:04:05.000"
)
// GlobalConfig config contains global options
type GlobalConfig struct {
V int `mapstructure:"v"`
}
// SetGlobalOptions sets the global options, like level against which all info logs will be
// compared. If this is greater than or equal to the "V" of the logger, the
// message will be logged. Concurrent-safe.
func SetGlobalOptions(config GlobalConfig) {
lvl := 1 - config.V
if v := int(zerolog.TraceLevel); lvl < v {
lvl = v
} else if v := int(zerolog.InfoLevel); lvl > v {
lvl = v
}
zerolog.SetGlobalLevel(zerolog.Level(lvl))
}
// SetVLevelByStringGlobal does the same as SetGlobalOptions but
// trying to expose verbosity level as more familiar "word-based" log levels
func SetVLevelByStringGlobal(level string) {
if v, err := zerolog.ParseLevel(level); err == nil {
zerolog.SetGlobalLevel(v)
}
}
// Options that can be passed to NewWithOptions
type Options struct {
// Name is an optional name of the logger
Name string
TimeFormat string
Output io.Writer
// Logger is an instance of zerolog, if nil a default logger is used
Logger *zerolog.Logger
}
// New returns a logr.Logger, LogSink is implemented by zerolog.
func New() logr.Logger {
return NewWithOptions(Options{})
}
// NewWithOptions returns a logr.Logger, LogSink is implemented by zerolog.
func NewWithOptions(opts Options) logr.Logger {
if opts.TimeFormat != "" {
zerolog.TimeFieldFormat = opts.TimeFormat
} else {
zerolog.TimeFieldFormat = timeFormat
}
var out io.Writer
if opts.Output != nil {
out = opts.Output
} else {
out = getOutputFormat()
}
if opts.Logger == nil {
l := zerolog.New(out).With().Timestamp().Logger()
opts.Logger = &l
}
ls := zerologr.NewLogSink(opts.Logger)
if zerolog.LevelFieldName == "" {
// Restore field removed by Zerologr
zerolog.LevelFieldName = "level"
}
l := logr.New(ls)
if opts.Name != "" {
l = l.WithName(opts.Name)
}
return l
}
func getOutputFormat() zerolog.ConsoleWriter {
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false}
output.FormatTimestamp = func(i interface{}) string {
return "[" + i.(string) + "]"
}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("[%-3s]", i))
}
output.FormatMessage = func(i interface{}) string {
_, file, line, _ := runtime.Caller(10)
return fmt.Sprintf("[%s:%d] => %s", filepath.Base(file), line, i)
}
return output
}
+144
View File
@@ -0,0 +1,144 @@
package logger
import (
"bytes"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDefaultVerbosityLevel(t *testing.T) {
t.Run("info", func(t *testing.T) {
SetGlobalOptions(GlobalConfig{V: 0})
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("info")
assert.Equal(t, `{"level":"info","v":0,"time":"NOTIME","message":"info"}`+"\n", out.String())
})
t.Run("info-add-fields", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("", "test_field", 123)
assert.Equal(t, `{"level":"info","v":0,"test_field":123,"time":"NOTIME"}`+"\n", out.String())
})
t.Run("empty", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("")
assert.Equal(t, `{"level":"info","v":0,"time":"NOTIME"}`+"\n", out.String())
})
t.Run("disabled", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.V(1).Info("You should not see this")
assert.Equal(t, ``, out.String())
})
}
func TestVerbosityLevel2(t *testing.T) {
t.Run("info", func(t *testing.T) {
SetGlobalOptions(GlobalConfig{V: 2})
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("info")
assert.Equal(t, `{"level":"info","v":0,"time":"NOTIME","message":"info"}`+"\n", out.String())
})
t.Run("info-add-fields", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("", "test_field", 123)
assert.Equal(t, `{"level":"info","v":0,"test_field":123,"time":"NOTIME"}`+"\n", out.String())
})
t.Run("empty", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.Info("")
assert.Equal(t, `{"level":"info","v":0,"time":"NOTIME"}`+"\n", out.String())
})
t.Run("disabled", func(t *testing.T) {
out := &bytes.Buffer{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: out})
log.V(1).Info("")
assert.Equal(t, `{"level":"debug","v":1,"time":"NOTIME"}`+"\n", out.String())
})
}
type blackholeStream struct {
writeCount uint64
}
func (s *blackholeStream) WriteCount() uint64 {
return atomic.LoadUint64(&s.writeCount)
}
func (s *blackholeStream) Write(p []byte) (int, error) {
atomic.AddUint64(&s.writeCount, 1)
return len(p), nil
}
func BenchmarkLoggerLogs(b *testing.B) {
stream := &blackholeStream{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: stream})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Info("The quick brown fox jumps over the lazy dog")
}
})
if stream.WriteCount() != uint64(b.N) {
b.Fatalf("Log write count")
}
}
func BenchmarLoggerLog(b *testing.B) {
stream := &blackholeStream{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: stream})
b.ResetTimer()
SetGlobalOptions(GlobalConfig{V: 1})
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.V(1).Info("The quick brown fox jumps over the lazy dog")
}
})
if stream.WriteCount() != uint64(b.N) {
b.Fatalf("Log write count")
}
}
func BenchmarkLoggerLogWith10Fields(b *testing.B) {
stream := &blackholeStream{}
log := NewWithOptions(Options{TimeFormat: "NOTIME", Output: stream})
b.ResetTimer()
SetGlobalOptions(GlobalConfig{V: 1})
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.V(1).Info("The quick brown fox jumps over the lazy dog",
"test1", 1,
"test2", 2,
"test3", 3,
"test4", 4,
"test5", 5,
"test6", 6,
"test7", 7,
"test8", 8,
"test9", 9,
"test10", 10)
}
})
if stream.WriteCount() != uint64(b.N) {
b.Fatalf("Log write count")
}
}