-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
523 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package agent | ||
|
||
type Agent struct { | ||
} | ||
|
||
func (a *Agent) Leave() error { | ||
return nil | ||
} | ||
|
||
func (a *Agent) Shutdown() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package agent | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
// GatedWriter is an io.Writer implementation that buffers all of its | ||
// data into an internal buffer until it is told to let data through. | ||
type GatedWriter struct { | ||
Writer io.Writer | ||
|
||
buf [][]byte | ||
flush bool | ||
lock sync.RWMutex | ||
} | ||
|
||
// Flush tells the GatedWriter to flush any buffered data and to stop | ||
// buffering. | ||
func (w *GatedWriter) Flush() { | ||
w.lock.Lock() | ||
w.flush = true | ||
w.lock.Unlock() | ||
|
||
for _, p := range w.buf { | ||
w.Write(p) | ||
} | ||
w.buf = nil | ||
} | ||
|
||
func (w *GatedWriter) Write(p []byte) (n int, err error) { | ||
w.lock.RLock() | ||
defer w.lock.RUnlock() | ||
|
||
if w.flush { | ||
return w.Writer.Write(p) | ||
} | ||
|
||
p2 := make([]byte, len(p)) | ||
copy(p2, p) | ||
w.buf = append(w.buf, p2) | ||
return len(p), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package agent | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestGatedWriter_impl(t *testing.T) { | ||
var _ io.Writer = new(GatedWriter) | ||
} | ||
|
||
func TestGatedWriter(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
w := &GatedWriter{Writer: buf} | ||
w.Write([]byte("foo\n")) | ||
w.Write([]byte("bar\n")) | ||
|
||
if buf.String() != "" { | ||
t.Fatalf("bad: %s", buf.String()) | ||
} | ||
|
||
w.Flush() | ||
|
||
if buf.String() != "foo\nbar\n" { | ||
t.Fatalf("bad: %s", buf.String()) | ||
} | ||
|
||
w.Write([]byte("baz\n")) | ||
|
||
if buf.String() != "foo\nbar\nbaz\n" { | ||
t.Fatalf("bad: %s", buf.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package agent | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/hashicorp/logutils" | ||
) | ||
|
||
// LevelFilter returns a LevelFilter that is configured with the log | ||
// levels that we use. | ||
func LevelFilter() *logutils.LevelFilter { | ||
return &logutils.LevelFilter{ | ||
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERR"}, | ||
MinLevel: "INFO", | ||
Writer: ioutil.Discard, | ||
} | ||
} | ||
|
||
// ValidateLevelFilter verifies that the log levels within the filter | ||
// are valid. | ||
func ValidateLevelFilter(minLevel logutils.LogLevel, filter *logutils.LevelFilter) bool { | ||
for _, level := range filter.Levels { | ||
if level == minLevel { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.