-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Jeromy <[email protected]>
- Loading branch information
1 parent
152247d
commit a676b5a
Showing
5 changed files
with
77 additions
and
7 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
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,42 @@ | ||
package corehttp | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
"github.com/ipfs/go-ipfs/thirdparty/eventlog" | ||
) | ||
|
||
type writeErrNotifier struct { | ||
w io.Writer | ||
errs chan error | ||
} | ||
|
||
func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) { | ||
ch := make(chan error, 1) | ||
return &writeErrNotifier{ | ||
w: w, | ||
errs: ch, | ||
}, ch | ||
} | ||
|
||
func (w *writeErrNotifier) Write(b []byte) (int, error) { | ||
n, err := w.w.Write(b) | ||
if err != nil { | ||
w.errs <- err | ||
} | ||
return n, err | ||
} | ||
|
||
func LogOption() ServeOption { | ||
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) { | ||
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
wnf, errs := newWriteErrNotifier(w) | ||
eventlog.WriterGroup.AddWriter(wnf) | ||
<-errs | ||
}) | ||
return mux, 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,31 @@ | ||
package eventlog | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
type MirrorWriter struct { | ||
writers []io.Writer | ||
lk sync.Mutex | ||
} | ||
|
||
func (mw *MirrorWriter) Write(b []byte) (int, error) { | ||
mw.lk.Lock() | ||
var filter []io.Writer | ||
for _, w := range mw.writers { | ||
_, err := w.Write(b) | ||
if err == nil { | ||
filter = append(filter, w) | ||
} | ||
} | ||
mw.writers = filter | ||
mw.lk.Unlock() | ||
return len(b), nil | ||
} | ||
|
||
func (mw *MirrorWriter) AddWriter(w io.Writer) { | ||
mw.lk.Lock() | ||
mw.writers = append(mw.writers, w) | ||
mw.lk.Unlock() | ||
} |