Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
statsd: add support for Windows Pipes (#182)
This change adds support for Windows Named Pipes. They may be used when this option is [enabled in the Datadog Agent](DataDog/datadog-agent#6830). Co-authored-by: Lucas Pimentel-Ordyna <lucas.pimentel@datadoghq.com>
- Loading branch information
1 parent
c869ea2
commit 2c5dbc8
Showing
4 changed files
with
106 additions
and
9 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,9 @@ | ||
// +build !windows | ||
|
||
package statsd | ||
|
||
import "errors" | ||
|
||
func newWindowsPipeWriter(pipepath string) (statsdWriter, error) { | ||
return nil, errors.New("Windows Named Pipes are only supported on Windows") | ||
} |
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,22 @@ | ||
// +build windows | ||
|
||
package statsd | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"time" | ||
|
||
"github.com/Microsoft/go-winio" | ||
) | ||
|
||
type pipeWriter struct{ net.Conn } | ||
|
||
func (pipeWriter) SetWriteTimeout(_ time.Duration) error { | ||
return errors.New("SetWriteTimeout is not supported on Windows Named Pipes") | ||
} | ||
|
||
func newWindowsPipeWriter(pipepath string) (*pipeWriter, error) { | ||
conn, err := winio.DialPipe(pipepath, nil) | ||
return &pipeWriter{conn}, err | ||
} |
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,54 @@ | ||
// +build windows | ||
|
||
package statsd | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/Microsoft/go-winio" | ||
) | ||
|
||
func TestPipeWriter(t *testing.T) { | ||
f, err := ioutil.TempFile("", "test-pipe-") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(f.Name()) | ||
pipepath := WindowsPipeAddressPrefix + f.Name() | ||
ln, err := winio.ListenPipe(pipepath, &winio.PipeConfig{ | ||
SecurityDescriptor: "D:AI(A;;GA;;;WD)", | ||
InputBufferSize: 1_000_000, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
out := make(chan string) | ||
go func() { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
buf := make([]byte, 512) | ||
n, err := conn.Read(buf) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
conn.Close() | ||
out <- string(buf[:n]) | ||
}() | ||
|
||
client, err := New(pipepath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := client.Gauge("metric", 1, []string{"key:val"}, 1); err != nil { | ||
log.Fatal(err) | ||
} | ||
got := <-out | ||
if exp := "metric:1|g|#key:val"; got != exp { | ||
t.Fatalf("Expected %q, got %q", exp, got) | ||
} | ||
} |
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