diode

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2018 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a io.Writer wrapper that uses a diode to make Write lock-free, non-blocking and thread safe.

func NewWriter

func NewWriter(w io.Writer, manyToOneDiode *diodes.ManyToOne, poolInterval time.Duration) Writer

NewWriter creates a writer wrapping w with a many-to-one diode in order to never block log producers and drop events if the writer can't keep up with the flow of data.

Use a diode.Writer when

d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
    log.Printf("Dropped %d messages", missed)
}))
w := diode.NewWriter(w, d, 10 * time.Millisecond)
log := zerolog.New(w)

See code.cloudfoundry.org/go-diodes for more info on diode.

Example
package main

import (
	"fmt"
	"os"
	"time"

	diodes "code.cloudfoundry.org/go-diodes"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/diode"
)

func main() {
	d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
		fmt.Printf("Dropped %d messages\n", missed)
	}))
	w := diode.NewWriter(os.Stdout, d, 10*time.Millisecond)
	log := zerolog.New(w)
	log.Print("test")

	w.Close()

}
Output:

{"level":"debug","message":"test"}

func (Writer) Close

func (dw Writer) Close() error

Close releases the diode poller and call Close on the wrapped writer if io.Closer is implemented.

func (Writer) Write

func (dw Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL