-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
1 parent
0d7ab40
commit 726e039
Showing
41 changed files
with
850 additions
and
90 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
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,64 @@ | ||
package api | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
) | ||
|
||
func (c *Sys) Monitor(logLevel string, stopCh chan struct{}) (chan string, error) { | ||
r := c.c.NewRequest("GET", "/v1/sys/monitor") | ||
|
||
if logLevel == "" { | ||
r.Params.Add("log_level", "INFO") | ||
} else { | ||
r.Params.Add("log_level", logLevel) | ||
} | ||
|
||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
resp, err := c.c.RawRequestWithContext(ctx, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logCh := make(chan string, 64) | ||
go func() { | ||
defer resp.Body.Close() | ||
scanner := bufio.NewScanner(resp.Body) | ||
|
||
for { | ||
select { | ||
case <-stopCh: | ||
close(logCh) | ||
cancelFunc() | ||
return | ||
case <-ctx.Done(): | ||
stopCh <- struct{}{} | ||
close(logCh) | ||
cancelFunc() | ||
return | ||
default: | ||
} | ||
|
||
if scanner.Scan() { | ||
// An empty string signals to the caller that | ||
// the scan is done, so make sure we only emit | ||
// that when the scanner says it's done, not if | ||
// we happen to ingest an empty line. | ||
if text := scanner.Text(); text != "" { | ||
logCh <- text | ||
} else { | ||
logCh <- " " | ||
} | ||
} else { | ||
// If Scan() returns false, that means the context deadline was exceeded, so | ||
// terminate this routine and start a new request. | ||
stopCh <- struct{}{} | ||
close(logCh) | ||
cancelFunc() | ||
return | ||
} | ||
} | ||
}() | ||
|
||
return logCh, 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
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
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
Oops, something went wrong.