2018-03-15 16:03:41 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-03-15 16:03:41 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPClientTarget - HTTP client target.
|
|
|
|
type HTTPClientTarget struct {
|
|
|
|
id event.TargetID
|
|
|
|
w http.ResponseWriter
|
|
|
|
eventCh chan []byte
|
|
|
|
DoneCh chan struct{}
|
|
|
|
stopCh chan struct{}
|
|
|
|
isStopped uint32
|
|
|
|
isRunning uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target HTTPClientTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - does nothing and available for interface compatibility.
|
|
|
|
func (target *HTTPClientTarget) IsActive() (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
func (target *HTTPClientTarget) start() {
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
atomic.AddUint32(&target.isRunning, 1)
|
|
|
|
|
|
|
|
// Close DoneCh to indicate we are done.
|
|
|
|
close(target.DoneCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
write := func(event []byte) error {
|
|
|
|
if _, err := target.w.Write(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
target.w.(http.Flusher).Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:43:20 -04:00
|
|
|
keepAliveTicker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer keepAliveTicker.Stop()
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-target.stopCh:
|
|
|
|
// We are asked to stop.
|
|
|
|
return
|
|
|
|
case event, ok := <-target.eventCh:
|
|
|
|
if !ok {
|
|
|
|
// Got read error. Exit the goroutine.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := write(event); err != nil {
|
|
|
|
// Got write error to the client. Exit the goroutine.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-keepAliveTicker.C:
|
|
|
|
if err := write([]byte(" ")); err != nil {
|
|
|
|
// Got write error to the client. Exit the goroutine.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:46:01 -04:00
|
|
|
// Save - sends event to HTTP client.
|
|
|
|
func (target *HTTPClientTarget) Save(eventData event.Event) error {
|
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (target *HTTPClientTarget) send(eventData event.Event) error {
|
2018-03-15 16:03:41 -04:00
|
|
|
if atomic.LoadUint32(&target.isRunning) != 0 {
|
|
|
|
return errors.New("closed http connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = append(data, byte('\n'))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case target.eventCh <- data:
|
|
|
|
return nil
|
|
|
|
case <-target.DoneCh:
|
|
|
|
return errors.New("error in sending event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:46:01 -04:00
|
|
|
// Send - interface compatible method does no-op.
|
|
|
|
func (target *HTTPClientTarget) Send(eventKey string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// Close - closes underneath goroutine.
|
|
|
|
func (target *HTTPClientTarget) Close() error {
|
|
|
|
atomic.AddUint32(&target.isStopped, 1)
|
|
|
|
if atomic.LoadUint32(&target.isStopped) == 1 {
|
|
|
|
close(target.stopCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:24:43 -04:00
|
|
|
func getNewUUID() (string, error) {
|
2018-03-15 16:03:41 -04:00
|
|
|
uuid, err := uuid.New()
|
|
|
|
if err != nil {
|
2018-04-19 20:24:43 -04:00
|
|
|
return "", err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:24:43 -04:00
|
|
|
return uuid.String(), nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHTTPClientTarget - creates new HTTP client target.
|
2018-04-19 20:24:43 -04:00
|
|
|
func NewHTTPClientTarget(host xnet.Host, w http.ResponseWriter) (*HTTPClientTarget, error) {
|
|
|
|
uuid, err := getNewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
c := &HTTPClientTarget{
|
2018-11-14 13:23:44 -05:00
|
|
|
id: event.TargetID{ID: "httpclient" + "+" + uuid + "+" + host.Name, Name: host.Port.String()},
|
2018-03-15 16:03:41 -04:00
|
|
|
w: w,
|
|
|
|
eventCh: make(chan []byte),
|
|
|
|
DoneCh: make(chan struct{}),
|
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
c.start()
|
2018-04-19 20:24:43 -04:00
|
|
|
return c, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|