Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Unified Diff: client/internal/logdog/butler/streamserver/npipe_posix.go

Issue 1429993002: LogDog: Add Butler stream server package. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-butlerproto
Patch Set: Cleanup, comments. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: client/internal/logdog/butler/streamserver/npipe_posix.go
diff --git a/client/internal/logdog/butler/streamserver/npipe_posix.go b/client/internal/logdog/butler/streamserver/npipe_posix.go
new file mode 100644
index 0000000000000000000000000000000000000000..204c419fcd5913c6f36cf3e033412912f36860c6
--- /dev/null
+++ b/client/internal/logdog/butler/streamserver/npipe_posix.go
@@ -0,0 +1,91 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// +build darwin dragonfly freebsd linux netbsd openbsd
+
+package streamserver
+
+import (
+ "fmt"
+ "net"
+ "os"
+
+ log "github.com/luci/luci-go/common/logging"
+ "golang.org/x/net/context"
+)
+
+type namedPipeServer struct {
+ namedPipeServerBase
+
+ path string // The path on the filesystem of the UNIX domain named socket
+}
+
+func (s *namedPipeServer) GoString() string {
+ return fmt.Sprintf("namedPipeServer[%s]", s.path)
+}
+
+// Wrapper around the "unix"-type Listener that cleans up the named pipe on
+// creation and 'Close()'
+type selfCleaningUNIXListener struct {
+ path string
+ listener net.Listener
+}
+
+// NewNamedPipeServer instantiates a new POSIX named pipe server instance.
+func NewNamedPipeServer(ctx context.Context, path string) StreamServer {
+ s := &namedPipeServer{
+ path: path,
+ }
+ s.initBase(ctx)
+ return s
+}
+
+// OS-specific override to create a UNIX named socket listener
+func (s *namedPipeServer) createListener() (net.Listener, error) {
+ log.Fields{
+ "path": s.path,
+ }.Infof(s.ctx, "Creating POSIX server socket Listener.")
+ return newSelfCleaningUNIXListener(s.path)
+}
+
+//
+// selfCleaningUNIXListener
+//
+
+func newSelfCleaningUNIXListener(path string) (net.Listener, error) {
+ // Cleanup any previous entry
+ os.Remove(path)
iannucci 2015/11/12 22:44:24 error?
dnj 2015/11/13 20:17:15 Done.
+
+ // Create a UNIX listener
+ listener, err := net.Listen("unix", path)
+ if err != nil {
+ return nil, err
+ }
+
+ l := selfCleaningUNIXListener{
+ path: path,
+ listener: listener,
+ }
+ return &l, nil
+}
+
+// Implements net.Listner.Accept
+func (l *selfCleaningUNIXListener) Accept() (c net.Conn, err error) {
+ return l.listener.Accept()
+}
+
+// Implements net.Listener.Close
+func (l *selfCleaningUNIXListener) Close() error {
+ if err := l.listener.Close(); err != nil {
+ return err
+ }
+
+ os.Remove(l.path)
+ return nil
+}
+
+// Implements net.Listener.Addr
+func (l *selfCleaningUNIXListener) Addr() net.Addr {
+ return l.listener.Addr()
+}

Powered by Google App Engine
This is Rietveld 408576698