-
Notifications
You must be signed in to change notification settings - Fork 19
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
8 changed files
with
197 additions
and
116 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
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,104 @@ | ||
package workload | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/projecteru2/agent/types" | ||
coreutils "github.com/projecteru2/core/utils" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type subscriber struct { | ||
buf *bufio.ReadWriter | ||
unsubscribe func() | ||
} | ||
|
||
// logBroadcaster receives log and broadcasts to subscribers | ||
type logBroadcaster struct { | ||
logC chan *types.Log | ||
subscribers map[string]map[string]*subscriber | ||
} | ||
|
||
func newLogBroadcaster() *logBroadcaster { | ||
return &logBroadcaster{ | ||
logC: make(chan *types.Log), | ||
subscribers: map[string]map[string]*subscriber{}, | ||
} | ||
} | ||
|
||
// subscribe subscribes logs of the specific app. | ||
func (l *logBroadcaster) subscribe(app string, buf *bufio.ReadWriter) { | ||
if _, ok := l.subscribers[app]; !ok { | ||
l.subscribers[app] = map[string]*subscriber{} | ||
} | ||
|
||
ID := coreutils.RandomString(8) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
l.subscribers[app][ID] = &subscriber{buf, cancel} | ||
logrus.Infof("%s %s log subscribed", app, ID) | ||
<-ctx.Done() | ||
|
||
delete(l.subscribers[app], ID) | ||
if len(l.subscribers[app]) == 0 { | ||
delete(l.subscribers, app) | ||
} | ||
} | ||
|
||
func (l *logBroadcaster) broadcast(log *types.Log) { | ||
if _, ok := l.subscribers[log.Name]; !ok { | ||
return | ||
} | ||
data, err := json.Marshal(log) | ||
if err != nil { | ||
logrus.Error(err) | ||
return | ||
} | ||
line := fmt.Sprintf("%X\r\n%s\r\n\r\n", len(data)+2, string(data)) | ||
for ID, subscriber := range l.subscribers[log.Name] { | ||
if _, err := subscriber.buf.WriteString(line); err != nil { | ||
logrus.Error(err) | ||
logrus.Infof("%s %s detached", log.Name, ID) | ||
subscriber.unsubscribe() | ||
} | ||
subscriber.buf.Flush() | ||
logrus.Debugf("sub %s get %s", ID, line) | ||
} | ||
} | ||
|
||
func (l *logBroadcaster) run(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
logrus.Infof("[logBroadcaster] stops") | ||
return | ||
case log := <-l.logC: | ||
l.broadcast(log) | ||
} | ||
} | ||
} | ||
|
||
func (l *logBroadcaster) handler(w http.ResponseWriter, req *http.Request) { | ||
app := req.URL.Query().Get("app") | ||
if app == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
// fuck httpie | ||
w.WriteHeader(http.StatusOK) | ||
if hijack, ok := w.(http.Hijacker); ok { | ||
conn, buf, err := hijack.Hijack() | ||
if err != nil { | ||
logrus.Errorf("[apiLog] connect failed %v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
l.subscribe(app, buf) | ||
} | ||
} |
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,60 @@ | ||
package workload | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/projecteru2/agent/types" | ||
|
||
"github.com/bmizerany/pat" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogBroadcaster(t *testing.T) { | ||
l := newLogBroadcaster() | ||
|
||
go func() { | ||
restfulAPIServer := pat.New() | ||
restfulAPIServer.Add("GET", "/log/", http.HandlerFunc(l.handler)) | ||
http.Handle("/", restfulAPIServer) | ||
http.ListenAndServe(":12310", nil) | ||
}() | ||
|
||
go func() { | ||
time.Sleep(3 * time.Second) | ||
l.logC <- &types.Log{ | ||
ID: "Rei", | ||
Name: "nerv", | ||
Type: "stdout", | ||
EntryPoint: "eva0", | ||
Ident: "", | ||
Data: "data0", | ||
} | ||
l.logC <- &types.Log{ | ||
ID: "Rei", | ||
Name: "nerv", | ||
Type: "stdout", | ||
EntryPoint: "eva0", | ||
Ident: "", | ||
Data: "data1", | ||
} | ||
}() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
defer cancel() | ||
go l.run(ctx) | ||
|
||
time.Sleep(2 * time.Second) | ||
resp, err := http.Get("http://127.0.0.1:12310/log/?app=nerv") | ||
assert.Nil(t, err) | ||
|
||
reader := bufio.NewReader(resp.Body) | ||
for i := 0; i < 2; i++ { | ||
line, err := reader.ReadBytes('\n') | ||
assert.Nil(t, err) | ||
t.Log(string(line)) | ||
} | ||
} |
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.