mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-05 23:20:01 +00:00
Refactor subscribe
This commit is contained in:
1
go.sum
1
go.sum
@@ -68,6 +68,7 @@ github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB
|
|||||||
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c h1:eSfnfIuwhxZyULg1NNuZycJcYkjYVGYe7FczwQReM6U=
|
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c h1:eSfnfIuwhxZyULg1NNuZycJcYkjYVGYe7FczwQReM6U=
|
||||||
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
|
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
|
||||||
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
|
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
24
vendor/github.com/pkg/errors/.gitignore
generated
vendored
Normal file
24
vendor/github.com/pkg/errors/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
*.test
|
||||||
|
*.prof
|
15
vendor/github.com/pkg/errors/.travis.yml
generated
vendored
Normal file
15
vendor/github.com/pkg/errors/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
language: go
|
||||||
|
go_import_path: github.com/pkg/errors
|
||||||
|
go:
|
||||||
|
- 1.4.x
|
||||||
|
- 1.5.x
|
||||||
|
- 1.6.x
|
||||||
|
- 1.7.x
|
||||||
|
- 1.8.x
|
||||||
|
- 1.9.x
|
||||||
|
- 1.10.x
|
||||||
|
- 1.11.x
|
||||||
|
- tip
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v ./...
|
23
vendor/github.com/pkg/errors/LICENSE
generated
vendored
Normal file
23
vendor/github.com/pkg/errors/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
52
vendor/github.com/pkg/errors/README.md
generated
vendored
Normal file
52
vendor/github.com/pkg/errors/README.md
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# errors [](https://travis-ci.org/pkg/errors) [](https://ci.appveyor.com/project/davecheney/errors/branch/master) [](http://godoc.org/github.com/pkg/errors) [](https://goreportcard.com/report/github.com/pkg/errors) [](https://sourcegraph.com/github.com/pkg/errors?badge)
|
||||||
|
|
||||||
|
Package errors provides simple error handling primitives.
|
||||||
|
|
||||||
|
`go get github.com/pkg/errors`
|
||||||
|
|
||||||
|
The traditional error handling idiom in Go is roughly akin to
|
||||||
|
```go
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
```
|
||||||
|
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
|
||||||
|
|
||||||
|
## Adding context to an error
|
||||||
|
|
||||||
|
The errors.Wrap function returns a new error that adds context to the original error. For example
|
||||||
|
```go
|
||||||
|
_, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "read failed")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
## Retrieving the cause of an error
|
||||||
|
|
||||||
|
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
|
||||||
|
```go
|
||||||
|
type causer interface {
|
||||||
|
Cause() error
|
||||||
|
}
|
||||||
|
```
|
||||||
|
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
|
||||||
|
```go
|
||||||
|
switch err := errors.Cause(err).(type) {
|
||||||
|
case *MyError:
|
||||||
|
// handle specifically
|
||||||
|
default:
|
||||||
|
// unknown error
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.
|
||||||
|
|
||||||
|
Before proposing a change, please discuss your change by raising an issue.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
BSD-2-Clause
|
32
vendor/github.com/pkg/errors/appveyor.yml
generated
vendored
Normal file
32
vendor/github.com/pkg/errors/appveyor.yml
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
version: build-{build}.{branch}
|
||||||
|
|
||||||
|
clone_folder: C:\gopath\src\github.com\pkg\errors
|
||||||
|
shallow_clone: true # for startup speed
|
||||||
|
|
||||||
|
environment:
|
||||||
|
GOPATH: C:\gopath
|
||||||
|
|
||||||
|
platform:
|
||||||
|
- x64
|
||||||
|
|
||||||
|
# http://www.appveyor.com/docs/installed-software
|
||||||
|
install:
|
||||||
|
# some helpful output for debugging builds
|
||||||
|
- go version
|
||||||
|
- go env
|
||||||
|
# pre-installed MinGW at C:\MinGW is 32bit only
|
||||||
|
# but MSYS2 at C:\msys64 has mingw64
|
||||||
|
- set PATH=C:\msys64\mingw64\bin;%PATH%
|
||||||
|
- gcc --version
|
||||||
|
- g++ --version
|
||||||
|
|
||||||
|
build_script:
|
||||||
|
- go install -v ./...
|
||||||
|
|
||||||
|
test_script:
|
||||||
|
- set PATH=C:\gopath\bin;%PATH%
|
||||||
|
- go test -v ./...
|
||||||
|
|
||||||
|
#artifacts:
|
||||||
|
# - path: '%GOPATH%\bin\*.exe'
|
||||||
|
deploy: off
|
282
vendor/github.com/pkg/errors/errors.go
generated
vendored
Normal file
282
vendor/github.com/pkg/errors/errors.go
generated
vendored
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
// Package errors provides simple error handling primitives.
|
||||||
|
//
|
||||||
|
// The traditional error handling idiom in Go is roughly akin to
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// which when applied recursively up the call stack results in error reports
|
||||||
|
// without context or debugging information. The errors package allows
|
||||||
|
// programmers to add context to the failure path in their code in a way
|
||||||
|
// that does not destroy the original value of the error.
|
||||||
|
//
|
||||||
|
// Adding context to an error
|
||||||
|
//
|
||||||
|
// The errors.Wrap function returns a new error that adds context to the
|
||||||
|
// original error by recording a stack trace at the point Wrap is called,
|
||||||
|
// together with the supplied message. For example
|
||||||
|
//
|
||||||
|
// _, err := ioutil.ReadAll(r)
|
||||||
|
// if err != nil {
|
||||||
|
// return errors.Wrap(err, "read failed")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If additional control is required, the errors.WithStack and
|
||||||
|
// errors.WithMessage functions destructure errors.Wrap into its component
|
||||||
|
// operations: annotating an error with a stack trace and with a message,
|
||||||
|
// respectively.
|
||||||
|
//
|
||||||
|
// Retrieving the cause of an error
|
||||||
|
//
|
||||||
|
// Using errors.Wrap constructs a stack of errors, adding context to the
|
||||||
|
// preceding error. Depending on the nature of the error it may be necessary
|
||||||
|
// to reverse the operation of errors.Wrap to retrieve the original error
|
||||||
|
// for inspection. Any error value which implements this interface
|
||||||
|
//
|
||||||
|
// type causer interface {
|
||||||
|
// Cause() error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
|
||||||
|
// the topmost error that does not implement causer, which is assumed to be
|
||||||
|
// the original cause. For example:
|
||||||
|
//
|
||||||
|
// switch err := errors.Cause(err).(type) {
|
||||||
|
// case *MyError:
|
||||||
|
// // handle specifically
|
||||||
|
// default:
|
||||||
|
// // unknown error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Although the causer interface is not exported by this package, it is
|
||||||
|
// considered a part of its stable public interface.
|
||||||
|
//
|
||||||
|
// Formatted printing of errors
|
||||||
|
//
|
||||||
|
// All error values returned from this package implement fmt.Formatter and can
|
||||||
|
// be formatted by the fmt package. The following verbs are supported:
|
||||||
|
//
|
||||||
|
// %s print the error. If the error has a Cause it will be
|
||||||
|
// printed recursively.
|
||||||
|
// %v see %s
|
||||||
|
// %+v extended format. Each Frame of the error's StackTrace will
|
||||||
|
// be printed in detail.
|
||||||
|
//
|
||||||
|
// Retrieving the stack trace of an error or wrapper
|
||||||
|
//
|
||||||
|
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
|
||||||
|
// invoked. This information can be retrieved with the following interface:
|
||||||
|
//
|
||||||
|
// type stackTracer interface {
|
||||||
|
// StackTrace() errors.StackTrace
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// The returned errors.StackTrace type is defined as
|
||||||
|
//
|
||||||
|
// type StackTrace []Frame
|
||||||
|
//
|
||||||
|
// The Frame type represents a call site in the stack trace. Frame supports
|
||||||
|
// the fmt.Formatter interface that can be used for printing information about
|
||||||
|
// the stack trace of this error. For example:
|
||||||
|
//
|
||||||
|
// if err, ok := err.(stackTracer); ok {
|
||||||
|
// for _, f := range err.StackTrace() {
|
||||||
|
// fmt.Printf("%+s:%d", f)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Although the stackTracer interface is not exported by this package, it is
|
||||||
|
// considered a part of its stable public interface.
|
||||||
|
//
|
||||||
|
// See the documentation for Frame.Format for more details.
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns an error with the supplied message.
|
||||||
|
// New also records the stack trace at the point it was called.
|
||||||
|
func New(message string) error {
|
||||||
|
return &fundamental{
|
||||||
|
msg: message,
|
||||||
|
stack: callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf formats according to a format specifier and returns the string
|
||||||
|
// as a value that satisfies error.
|
||||||
|
// Errorf also records the stack trace at the point it was called.
|
||||||
|
func Errorf(format string, args ...interface{}) error {
|
||||||
|
return &fundamental{
|
||||||
|
msg: fmt.Sprintf(format, args...),
|
||||||
|
stack: callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fundamental is an error that has a message and a stack, but no caller.
|
||||||
|
type fundamental struct {
|
||||||
|
msg string
|
||||||
|
*stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fundamental) Error() string { return f.msg }
|
||||||
|
|
||||||
|
func (f *fundamental) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
io.WriteString(s, f.msg)
|
||||||
|
f.stack.Format(s, verb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's':
|
||||||
|
io.WriteString(s, f.msg)
|
||||||
|
case 'q':
|
||||||
|
fmt.Fprintf(s, "%q", f.msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithStack annotates err with a stack trace at the point WithStack was called.
|
||||||
|
// If err is nil, WithStack returns nil.
|
||||||
|
func WithStack(err error) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type withStack struct {
|
||||||
|
error
|
||||||
|
*stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *withStack) Cause() error { return w.error }
|
||||||
|
|
||||||
|
func (w *withStack) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
fmt.Fprintf(s, "%+v", w.Cause())
|
||||||
|
w.stack.Format(s, verb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's':
|
||||||
|
io.WriteString(s, w.Error())
|
||||||
|
case 'q':
|
||||||
|
fmt.Fprintf(s, "%q", w.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap returns an error annotating err with a stack trace
|
||||||
|
// at the point Wrap is called, and the supplied message.
|
||||||
|
// If err is nil, Wrap returns nil.
|
||||||
|
func Wrap(err error, message string) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: message,
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrapf returns an error annotating err with a stack trace
|
||||||
|
// at the point Wrapf is called, and the format specifier.
|
||||||
|
// If err is nil, Wrapf returns nil.
|
||||||
|
func Wrapf(err error, format string, args ...interface{}) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: fmt.Sprintf(format, args...),
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMessage annotates err with a new message.
|
||||||
|
// If err is nil, WithMessage returns nil.
|
||||||
|
func WithMessage(err error, message string) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMessagef annotates err with the format specifier.
|
||||||
|
// If err is nil, WithMessagef returns nil.
|
||||||
|
func WithMessagef(err error, format string, args ...interface{}) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: fmt.Sprintf(format, args...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type withMessage struct {
|
||||||
|
cause error
|
||||||
|
msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
|
||||||
|
func (w *withMessage) Cause() error { return w.cause }
|
||||||
|
|
||||||
|
func (w *withMessage) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
fmt.Fprintf(s, "%+v\n", w.Cause())
|
||||||
|
io.WriteString(s, w.msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's', 'q':
|
||||||
|
io.WriteString(s, w.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cause returns the underlying cause of the error, if possible.
|
||||||
|
// An error value has a cause if it implements the following
|
||||||
|
// interface:
|
||||||
|
//
|
||||||
|
// type causer interface {
|
||||||
|
// Cause() error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If the error does not implement Cause, the original error will
|
||||||
|
// be returned. If the error is nil, nil will be returned without further
|
||||||
|
// investigation.
|
||||||
|
func Cause(err error) error {
|
||||||
|
type causer interface {
|
||||||
|
Cause() error
|
||||||
|
}
|
||||||
|
|
||||||
|
for err != nil {
|
||||||
|
cause, ok := err.(causer)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = cause.Cause()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
147
vendor/github.com/pkg/errors/stack.go
generated
vendored
Normal file
147
vendor/github.com/pkg/errors/stack.go
generated
vendored
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Frame represents a program counter inside a stack frame.
|
||||||
|
type Frame uintptr
|
||||||
|
|
||||||
|
// pc returns the program counter for this frame;
|
||||||
|
// multiple frames may have the same PC value.
|
||||||
|
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
|
||||||
|
|
||||||
|
// file returns the full path to the file that contains the
|
||||||
|
// function for this Frame's pc.
|
||||||
|
func (f Frame) file() string {
|
||||||
|
fn := runtime.FuncForPC(f.pc())
|
||||||
|
if fn == nil {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
file, _ := fn.FileLine(f.pc())
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
// line returns the line number of source code of the
|
||||||
|
// function for this Frame's pc.
|
||||||
|
func (f Frame) line() int {
|
||||||
|
fn := runtime.FuncForPC(f.pc())
|
||||||
|
if fn == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
_, line := fn.FileLine(f.pc())
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format formats the frame according to the fmt.Formatter interface.
|
||||||
|
//
|
||||||
|
// %s source file
|
||||||
|
// %d source line
|
||||||
|
// %n function name
|
||||||
|
// %v equivalent to %s:%d
|
||||||
|
//
|
||||||
|
// Format accepts flags that alter the printing of some verbs, as follows:
|
||||||
|
//
|
||||||
|
// %+s function name and path of source file relative to the compile time
|
||||||
|
// GOPATH separated by \n\t (<funcname>\n\t<path>)
|
||||||
|
// %+v equivalent to %+s:%d
|
||||||
|
func (f Frame) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 's':
|
||||||
|
switch {
|
||||||
|
case s.Flag('+'):
|
||||||
|
pc := f.pc()
|
||||||
|
fn := runtime.FuncForPC(pc)
|
||||||
|
if fn == nil {
|
||||||
|
io.WriteString(s, "unknown")
|
||||||
|
} else {
|
||||||
|
file, _ := fn.FileLine(pc)
|
||||||
|
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
io.WriteString(s, path.Base(f.file()))
|
||||||
|
}
|
||||||
|
case 'd':
|
||||||
|
fmt.Fprintf(s, "%d", f.line())
|
||||||
|
case 'n':
|
||||||
|
name := runtime.FuncForPC(f.pc()).Name()
|
||||||
|
io.WriteString(s, funcname(name))
|
||||||
|
case 'v':
|
||||||
|
f.Format(s, 's')
|
||||||
|
io.WriteString(s, ":")
|
||||||
|
f.Format(s, 'd')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
|
||||||
|
type StackTrace []Frame
|
||||||
|
|
||||||
|
// Format formats the stack of Frames according to the fmt.Formatter interface.
|
||||||
|
//
|
||||||
|
// %s lists source files for each Frame in the stack
|
||||||
|
// %v lists the source file and line number for each Frame in the stack
|
||||||
|
//
|
||||||
|
// Format accepts flags that alter the printing of some verbs, as follows:
|
||||||
|
//
|
||||||
|
// %+v Prints filename, function, and line number for each Frame in the stack.
|
||||||
|
func (st StackTrace) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
switch {
|
||||||
|
case s.Flag('+'):
|
||||||
|
for _, f := range st {
|
||||||
|
fmt.Fprintf(s, "\n%+v", f)
|
||||||
|
}
|
||||||
|
case s.Flag('#'):
|
||||||
|
fmt.Fprintf(s, "%#v", []Frame(st))
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(s, "%v", []Frame(st))
|
||||||
|
}
|
||||||
|
case 's':
|
||||||
|
fmt.Fprintf(s, "%s", []Frame(st))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stack represents a stack of program counters.
|
||||||
|
type stack []uintptr
|
||||||
|
|
||||||
|
func (s *stack) Format(st fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
switch {
|
||||||
|
case st.Flag('+'):
|
||||||
|
for _, pc := range *s {
|
||||||
|
f := Frame(pc)
|
||||||
|
fmt.Fprintf(st, "\n%+v", f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stack) StackTrace() StackTrace {
|
||||||
|
f := make([]Frame, len(*s))
|
||||||
|
for i := 0; i < len(f); i++ {
|
||||||
|
f[i] = Frame((*s)[i])
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func callers() *stack {
|
||||||
|
const depth = 32
|
||||||
|
var pcs [depth]uintptr
|
||||||
|
n := runtime.Callers(3, pcs[:])
|
||||||
|
var st stack = pcs[0:n]
|
||||||
|
return &st
|
||||||
|
}
|
||||||
|
|
||||||
|
// funcname removes the path prefix component of a function's name reported by func.Name().
|
||||||
|
func funcname(name string) string {
|
||||||
|
i := strings.LastIndex(name, "/")
|
||||||
|
name = name[i+1:]
|
||||||
|
i = strings.Index(name, ".")
|
||||||
|
return name[i+1:]
|
||||||
|
}
|
4
vendor/github.com/rancher/norman/pkg/parse/collection.go
generated
vendored
4
vendor/github.com/rancher/norman/pkg/parse/collection.go
generated
vendored
@@ -19,7 +19,9 @@ func QueryOptions(apiOp *types.APIRequest, schema *types.Schema) types.QueryOpti
|
|||||||
return types.QueryOptions{}
|
return types.QueryOptions{}
|
||||||
}
|
}
|
||||||
|
|
||||||
result := &types.QueryOptions{}
|
result := &types.QueryOptions{
|
||||||
|
Options: map[string]string{},
|
||||||
|
}
|
||||||
|
|
||||||
result.Sort = parseSort(schema, apiOp)
|
result.Sort = parseSort(schema, apiOp)
|
||||||
result.Pagination = parsePagination(apiOp)
|
result.Pagination = parsePagination(apiOp)
|
||||||
|
2
vendor/github.com/rancher/norman/pkg/store/empty/empty_store.go
generated
vendored
2
vendor/github.com/rancher/norman/pkg/store/empty/empty_store.go
generated
vendored
@@ -27,6 +27,6 @@ func (e *Store) Update(apiOp *types.APIRequest, schema *types.Schema, data types
|
|||||||
return types.APIObject{}, nil
|
return types.APIObject{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
|
func (e *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
4
vendor/github.com/rancher/norman/pkg/store/proxy/error_wrapper.go
generated
vendored
4
vendor/github.com/rancher/norman/pkg/store/proxy/error_wrapper.go
generated
vendored
@@ -38,8 +38,8 @@ func (e *errorStore) Delete(apiOp *types.APIRequest, schema *types.Schema, id st
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
|
func (e *errorStore) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
|
||||||
data, err := e.Store.Watch(apiOp, schema, opt)
|
data, err := e.Store.Watch(apiOp, schema, wr)
|
||||||
return data, translateError(err)
|
return data, translateError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
67
vendor/github.com/rancher/norman/pkg/store/proxy/proxy_store.go
generated
vendored
67
vendor/github.com/rancher/norman/pkg/store/proxy/proxy_store.go
generated
vendored
@@ -3,6 +3,8 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
errors2 "github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/rancher/norman/pkg/types"
|
"github.com/rancher/norman/pkg/types"
|
||||||
"github.com/rancher/norman/pkg/types/convert/merge"
|
"github.com/rancher/norman/pkg/types/convert/merge"
|
||||||
"github.com/rancher/norman/pkg/types/values"
|
"github.com/rancher/norman/pkg/types/values"
|
||||||
@@ -107,61 +109,78 @@ func (s *Store) listNamespace(namespace string, apiOp types.APIRequest, schema *
|
|||||||
return k8sClient.List(metav1.ListOptions{})
|
return k8sClient.List(metav1.ListOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
|
func returnErr(err error, c chan types.APIEvent) {
|
||||||
k8sClient, err := s.clientGetter.Client(apiOp, schema)
|
c <- types.APIEvent{
|
||||||
if err != nil {
|
Name: "resource.error",
|
||||||
return nil, err
|
Error: err,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := k8sClient.List(metav1.ListOptions{})
|
func (s *Store) listAndWatch(apiOp *types.APIRequest, k8sClient dynamic.ResourceInterface, schema *types.Schema, w types.WatchRequest, result chan types.APIEvent) {
|
||||||
|
rev := w.Revision
|
||||||
|
if rev == "" {
|
||||||
|
list, err := k8sClient.List(metav1.ListOptions{
|
||||||
|
Limit: 1,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
returnErr(errors2.Wrapf(err, "failed to list %s", schema.ID), result)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
rev = list.GetResourceVersion()
|
||||||
result := make(chan types.APIEvent)
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
logrus.Debugf("closing watcher for %s", schema.ID)
|
|
||||||
close(result)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for i, obj := range list.Items {
|
|
||||||
result <- s.toAPIEvent(apiOp, schema, i, len(list.Items), false, &obj)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout := int64(60 * 30)
|
timeout := int64(60 * 30)
|
||||||
watcher, err := k8sClient.Watch(metav1.ListOptions{
|
watcher, err := k8sClient.Watch(metav1.ListOptions{
|
||||||
Watch: true,
|
Watch: true,
|
||||||
TimeoutSeconds: &timeout,
|
TimeoutSeconds: &timeout,
|
||||||
ResourceVersion: list.GetResourceVersion(),
|
ResourceVersion: rev,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugf("stopping watch for %s: %v", schema.ID, err)
|
returnErr(errors2.Wrapf(err, "stopping watch for %s: %v", schema.ID), result)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer watcher.Stop()
|
defer watcher.Stop()
|
||||||
|
logrus.Debugf("opening watcher for %s", schema.ID)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-apiOp.Request.Context().Done()
|
||||||
|
watcher.Stop()
|
||||||
|
}()
|
||||||
|
|
||||||
for event := range watcher.ResultChan() {
|
for event := range watcher.ResultChan() {
|
||||||
data := event.Object.(*unstructured.Unstructured)
|
data := event.Object.(*unstructured.Unstructured)
|
||||||
result <- s.toAPIEvent(apiOp, schema, 0, 0, event.Type == watch.Deleted, data)
|
result <- s.toAPIEvent(apiOp, schema, event.Type, data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
|
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, w types.WatchRequest) (chan types.APIEvent, error) {
|
||||||
|
k8sClient, err := s.clientGetter.Client(apiOp, schema)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(chan types.APIEvent)
|
||||||
|
go func() {
|
||||||
|
s.listAndWatch(apiOp, k8sClient, schema, w, result)
|
||||||
|
logrus.Debugf("closing watcher for %s", schema.ID)
|
||||||
|
close(result)
|
||||||
|
}()
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) toAPIEvent(apiOp *types.APIRequest, schema *types.Schema, index, count int, remove bool, obj *unstructured.Unstructured) types.APIEvent {
|
func (s *Store) toAPIEvent(apiOp *types.APIRequest, schema *types.Schema, et watch.EventType, obj *unstructured.Unstructured) types.APIEvent {
|
||||||
name := "resource.change"
|
name := "resource.change"
|
||||||
if remove && obj.Object != nil {
|
switch et {
|
||||||
|
case watch.Deleted:
|
||||||
name = "resource.remove"
|
name = "resource.remove"
|
||||||
|
case watch.Added:
|
||||||
|
name = "resource.create"
|
||||||
}
|
}
|
||||||
|
|
||||||
s.fromInternal(apiOp, schema, obj.Object)
|
s.fromInternal(apiOp, schema, obj.Object)
|
||||||
|
|
||||||
return types.APIEvent{
|
return types.APIEvent{
|
||||||
Name: name,
|
Name: name,
|
||||||
Count: count,
|
|
||||||
Index: index,
|
|
||||||
Object: types.ToAPI(obj.Object),
|
Object: types.ToAPI(obj.Object),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
vendor/github.com/rancher/norman/pkg/store/schema/schema_store.go
generated
vendored
2
vendor/github.com/rancher/norman/pkg/store/schema/schema_store.go
generated
vendored
@@ -34,7 +34,7 @@ func (s *Store) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (
|
|||||||
return types.APIObject{}, httperror.NewAPIError(httperror.NotFound, "no such schema")
|
return types.APIObject{}, httperror.NewAPIError(httperror.NotFound, "no such schema")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
|
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
57
vendor/github.com/rancher/norman/pkg/subscribe/convert.go
generated
vendored
Normal file
57
vendor/github.com/rancher/norman/pkg/subscribe/convert.go
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package subscribe
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/rancher/norman/pkg/api/writer"
|
||||||
|
"github.com/rancher/norman/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Converter struct {
|
||||||
|
writer.EncodingResponseWriter
|
||||||
|
apiOp *types.APIRequest
|
||||||
|
obj interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MarshallObject(apiOp *types.APIRequest, event types.APIEvent) types.APIEvent {
|
||||||
|
if event.Error != nil {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Object.IsNil() {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := newConverter(apiOp).ToAPIObject(event.Object)
|
||||||
|
if err != nil {
|
||||||
|
event.Error = err
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
event.Data = data.Raw()
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConverter(apiOp *types.APIRequest) *Converter {
|
||||||
|
c := &Converter{
|
||||||
|
apiOp: apiOp,
|
||||||
|
}
|
||||||
|
c.EncodingResponseWriter = writer.EncodingResponseWriter{
|
||||||
|
ContentType: "application/json",
|
||||||
|
Encoder: c.Encoder,
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Converter) ToAPIObject(data interface{}) (types.APIObject, error) {
|
||||||
|
c.obj = nil
|
||||||
|
if err := c.VersionBody(c.apiOp, nil, data); err != nil {
|
||||||
|
return types.APIObject{}, err
|
||||||
|
}
|
||||||
|
return types.ToAPI(c.obj), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Converter) Encoder(w io.Writer, obj interface{}) error {
|
||||||
|
c.obj = obj
|
||||||
|
return nil
|
||||||
|
}
|
174
vendor/github.com/rancher/norman/pkg/subscribe/handler.go
generated
vendored
174
vendor/github.com/rancher/norman/pkg/subscribe/handler.go
generated
vendored
@@ -1,29 +1,23 @@
|
|||||||
package subscribe
|
package subscribe
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/rancher/norman/pkg/api/writer"
|
|
||||||
"github.com/rancher/norman/pkg/httperror"
|
|
||||||
"github.com/rancher/norman/pkg/parse"
|
|
||||||
"github.com/rancher/norman/pkg/types"
|
"github.com/rancher/norman/pkg/types"
|
||||||
"github.com/rancher/norman/pkg/types/convert"
|
|
||||||
"github.com/rancher/norman/pkg/types/slice"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{}
|
var upgrader = websocket.Upgrader{
|
||||||
|
HandshakeTimeout: 60 * time.Second,
|
||||||
|
EnableCompression: true,
|
||||||
|
}
|
||||||
|
|
||||||
type Subscribe struct {
|
type Subscribe struct {
|
||||||
ResourceTypes []string
|
Stop bool `json:"stop,omitempty"`
|
||||||
APIVersions []string
|
ResourceType string `json:"resourceType,omitempty"`
|
||||||
ProjectID string `norman:"type=reference[/v3/schemas/project]"`
|
ResourceVersion string `json:"resourceVersion,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Handler(apiOp *types.APIRequest) (types.APIObject, error) {
|
func Handler(apiOp *types.APIRequest) (types.APIObject, error) {
|
||||||
@@ -34,102 +28,46 @@ func Handler(apiOp *types.APIRequest) (types.APIObject, error) {
|
|||||||
return types.APIObject{}, err
|
return types.APIObject{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMatchingSchemas(apiOp *types.APIRequest) []*types.Schema {
|
|
||||||
resourceTypes := apiOp.Request.URL.Query()["resourceTypes"]
|
|
||||||
|
|
||||||
var schemas []*types.Schema
|
|
||||||
for _, schema := range apiOp.Schemas.Schemas() {
|
|
||||||
if !matches(resourceTypes, schema.ID) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if schema.Store != nil {
|
|
||||||
schemas = append(schemas, schema)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return schemas
|
|
||||||
}
|
|
||||||
|
|
||||||
func handler(apiOp *types.APIRequest) error {
|
func handler(apiOp *types.APIRequest) error {
|
||||||
schemas := getMatchingSchemas(apiOp)
|
|
||||||
if len(schemas) == 0 {
|
|
||||||
return httperror.NewAPIError(httperror.NotFound, "no resources types matched")
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := upgrader.Upgrade(apiOp.Response, apiOp.Request, nil)
|
c, err := upgrader.Upgrade(apiOp.Response, apiOp.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
cancelCtx, cancel := context.WithCancel(apiOp.Request.Context())
|
watches := NewWatchSession(apiOp)
|
||||||
readerGroup, ctx := errgroup.WithContext(cancelCtx)
|
defer watches.Close()
|
||||||
apiOp.Request = apiOp.Request.WithContext(ctx)
|
|
||||||
|
|
||||||
go func() {
|
events := watches.Watch(c)
|
||||||
for {
|
t := time.NewTicker(30 * time.Second)
|
||||||
if _, _, err := c.NextReader(); err != nil {
|
|
||||||
cancel()
|
|
||||||
c.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
events := make(chan types.APIEvent)
|
|
||||||
for _, schema := range schemas {
|
|
||||||
if apiOp.AccessControl.CanWatch(apiOp, schema) == nil {
|
|
||||||
streamStore(ctx, readerGroup, apiOp, schema, events)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
readerGroup.Wait()
|
|
||||||
close(events)
|
|
||||||
}()
|
|
||||||
|
|
||||||
capture := &Capture{}
|
|
||||||
captureWriter := writer.EncodingResponseWriter{
|
|
||||||
ContentType: "application/json",
|
|
||||||
Encoder: capture.Encoder,
|
|
||||||
}
|
|
||||||
t := time.NewTicker(60 * time.Second)
|
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
|
|
||||||
done := false
|
for {
|
||||||
for !done {
|
|
||||||
select {
|
select {
|
||||||
case item, ok := <-events:
|
case event, ok := <-events:
|
||||||
if !ok {
|
if !ok {
|
||||||
done = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
schema := apiOp.Schemas.Schema(convert.ToString(item.Object.Map()["type"]))
|
|
||||||
if schema != nil {
|
|
||||||
if err := captureWriter.VersionBody(apiOp, nil, item.Object); err != nil {
|
|
||||||
cancel()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
item.Object = types.ToAPI(capture.Object)
|
|
||||||
if err := writeData(c, item); err != nil {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case <-t.C:
|
|
||||||
if err := writeData(c, types.APIEvent{Name: "ping"}); err != nil {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no point in ever returning null because the connection is hijacked and we can't write it
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if err := writeData(apiOp, c, event); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case <-t.C:
|
||||||
|
if err := writeData(apiOp, c, types.APIEvent{Name: "ping"}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeData(apiOp *types.APIRequest, c *websocket.Conn, event types.APIEvent) error {
|
||||||
|
event = MarshallObject(apiOp, event)
|
||||||
|
if event.Error != nil {
|
||||||
|
event.Name = "resource.error"
|
||||||
|
event.Data = map[string]interface{}{
|
||||||
|
"error": event.Error.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func writeData(c *websocket.Conn, event types.APIEvent) error {
|
|
||||||
event.Data = event.Object.Raw()
|
|
||||||
messageWriter, err := c.NextWriter(websocket.TextMessage)
|
messageWriter, err := c.NextWriter(websocket.TextMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -138,51 +76,3 @@ func writeData(c *websocket.Conn, event types.APIEvent) error {
|
|||||||
|
|
||||||
return json.NewEncoder(messageWriter).Encode(event)
|
return json.NewEncoder(messageWriter).Encode(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
func watch(apiOp *types.APIRequest, schema *types.Schema, opts *types.QueryOptions) (chan types.APIEvent, error) {
|
|
||||||
c, err := schema.Store.Watch(apiOp, schema, opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return types.APIChan(c, func(data types.APIEvent) types.APIEvent {
|
|
||||||
data.Object = apiOp.FilterObject(nil, schema, data.Object)
|
|
||||||
return data
|
|
||||||
}), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func streamStore(ctx context.Context, eg *errgroup.Group, apiOp *types.APIRequest, schema *types.Schema, result chan types.APIEvent) {
|
|
||||||
eg.Go(func() error {
|
|
||||||
opts := parse.QueryOptions(apiOp, schema)
|
|
||||||
events, err := watch(apiOp, schema, &opts)
|
|
||||||
if err != nil || events == nil {
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("failed on subscribe %s: %v", schema.ID, err)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Debugf("watching %s", schema.ID)
|
|
||||||
|
|
||||||
for e := range events {
|
|
||||||
result <- e
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.New("disconnect")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func matches(items []string, item string) bool {
|
|
||||||
if len(items) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return slice.ContainsString(items, item)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Capture struct {
|
|
||||||
Object interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Capture) Encoder(w io.Writer, obj interface{}) error {
|
|
||||||
c.Object = obj
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
140
vendor/github.com/rancher/norman/pkg/subscribe/watcher.go
generated
vendored
Normal file
140
vendor/github.com/rancher/norman/pkg/subscribe/watcher.go
generated
vendored
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
package subscribe
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/rancher/norman/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WatchSession struct {
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
|
apiOp *types.APIRequest
|
||||||
|
watchers map[string]func()
|
||||||
|
wg sync.WaitGroup
|
||||||
|
ctx context.Context
|
||||||
|
cancel func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) stop(id string, resp chan<- types.APIEvent) {
|
||||||
|
s.Lock()
|
||||||
|
defer s.Unlock()
|
||||||
|
if cancel, ok := s.watchers[id]; ok {
|
||||||
|
cancel()
|
||||||
|
resp <- types.APIEvent{
|
||||||
|
Name: "resource.stop",
|
||||||
|
ResourceType: id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(s.watchers, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) add(resourceType, revision string, resp chan<- types.APIEvent) {
|
||||||
|
s.Lock()
|
||||||
|
defer s.Unlock()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(s.ctx)
|
||||||
|
s.watchers[resourceType] = cancel
|
||||||
|
|
||||||
|
s.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer s.wg.Done()
|
||||||
|
defer s.stop(resourceType, resp)
|
||||||
|
|
||||||
|
if err := s.stream(ctx, resourceType, revision, resp); err != nil {
|
||||||
|
sendErr(resp, err, resourceType)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) stream(ctx context.Context, resourceType, revision string, result chan<- types.APIEvent) error {
|
||||||
|
schema := s.apiOp.Schemas.Schema(resourceType)
|
||||||
|
if schema == nil {
|
||||||
|
return fmt.Errorf("failed to find schema %s", resourceType)
|
||||||
|
} else if schema.Store == nil {
|
||||||
|
return fmt.Errorf("schema %s does not support watching", resourceType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.apiOp.AccessControl.CanWatch(s.apiOp, schema); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := schema.Store.Watch(s.apiOp.WithContext(ctx), schema, types.WatchRequest{Revision: revision})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
result <- types.APIEvent{
|
||||||
|
Name: "resource.start",
|
||||||
|
ResourceType: resourceType,
|
||||||
|
}
|
||||||
|
|
||||||
|
for event := range c {
|
||||||
|
result <- event
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWatchSession(apiOp *types.APIRequest) *WatchSession {
|
||||||
|
ws := &WatchSession{
|
||||||
|
apiOp: apiOp,
|
||||||
|
watchers: map[string]func(){},
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.ctx, ws.cancel = context.WithCancel(apiOp.Request.Context())
|
||||||
|
return ws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) Watch(conn *websocket.Conn) <-chan types.APIEvent {
|
||||||
|
result := make(chan types.APIEvent, 100)
|
||||||
|
go func() {
|
||||||
|
defer close(result)
|
||||||
|
|
||||||
|
if err := s.watch(conn, result); err != nil {
|
||||||
|
sendErr(result, err, "")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) Close() {
|
||||||
|
s.cancel()
|
||||||
|
s.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WatchSession) watch(conn *websocket.Conn, resp chan types.APIEvent) error {
|
||||||
|
defer s.wg.Wait()
|
||||||
|
defer s.cancel()
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, r, err := conn.NextReader()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var sub Subscribe
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r).Decode(&sub); err != nil {
|
||||||
|
sendErr(resp, err, "")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if sub.Stop {
|
||||||
|
s.stop(sub.ResourceType, resp)
|
||||||
|
} else if _, ok := s.watchers[sub.ResourceType]; !ok {
|
||||||
|
s.add(sub.ResourceType, sub.ResourceVersion, resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendErr(resp chan<- types.APIEvent, err error, resourceType string) {
|
||||||
|
resp <- types.APIEvent{
|
||||||
|
ResourceType: resourceType,
|
||||||
|
Error: err,
|
||||||
|
}
|
||||||
|
}
|
4
vendor/github.com/rancher/norman/pkg/types/definition/definition.go
generated
vendored
4
vendor/github.com/rancher/norman/pkg/types/definition/definition.go
generated
vendored
@@ -3,7 +3,7 @@ package definition
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
convert2 "github.com/rancher/norman/pkg/types/convert"
|
"github.com/rancher/norman/pkg/types/convert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsMapType(fieldType string) bool {
|
func IsMapType(fieldType string) bool {
|
||||||
@@ -41,5 +41,5 @@ func GetShortTypeFromFull(fullType string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetFullType(data map[string]interface{}) string {
|
func GetFullType(data map[string]interface{}) string {
|
||||||
return convert2.ToString(data["type"])
|
return convert.ToString(data["type"])
|
||||||
}
|
}
|
||||||
|
18
vendor/github.com/rancher/norman/pkg/types/server_types.go
generated
vendored
18
vendor/github.com/rancher/norman/pkg/types/server_types.go
generated
vendored
@@ -1,6 +1,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -112,6 +113,12 @@ type APIRequest struct {
|
|||||||
Response http.ResponseWriter
|
Response http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *APIRequest) WithContext(ctx context.Context) *APIRequest {
|
||||||
|
result := *r
|
||||||
|
result.Request = result.Request.WithContext(ctx)
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
func (r *APIRequest) GetUser() string {
|
func (r *APIRequest) GetUser() string {
|
||||||
user, ok := request.UserFrom(r.Request.Context())
|
user, ok := request.UserFrom(r.Request.Context())
|
||||||
if ok {
|
if ok {
|
||||||
@@ -160,6 +167,7 @@ type QueryOptions struct {
|
|||||||
Sort Sort
|
Sort Sort
|
||||||
Pagination *Pagination
|
Pagination *Pagination
|
||||||
Conditions []*QueryCondition
|
Conditions []*QueryCondition
|
||||||
|
Options map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReferenceValidator interface {
|
type ReferenceValidator interface {
|
||||||
@@ -189,14 +197,18 @@ type Store interface {
|
|||||||
Create(apiOp *APIRequest, schema *Schema, data APIObject) (APIObject, error)
|
Create(apiOp *APIRequest, schema *Schema, data APIObject) (APIObject, error)
|
||||||
Update(apiOp *APIRequest, schema *Schema, data APIObject, id string) (APIObject, error)
|
Update(apiOp *APIRequest, schema *Schema, data APIObject, id string) (APIObject, error)
|
||||||
Delete(apiOp *APIRequest, schema *Schema, id string) (APIObject, error)
|
Delete(apiOp *APIRequest, schema *Schema, id string) (APIObject, error)
|
||||||
Watch(apiOp *APIRequest, schema *Schema, opt *QueryOptions) (chan APIEvent, error)
|
Watch(apiOp *APIRequest, schema *Schema, w WatchRequest) (chan APIEvent, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type WatchRequest struct {
|
||||||
|
Revision string
|
||||||
}
|
}
|
||||||
|
|
||||||
type APIEvent struct {
|
type APIEvent struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Count int `json:"count,omitempty"`
|
ResourceType string `json:"resourceType,omitempty"`
|
||||||
Index int `json:"index,omitempty"`
|
|
||||||
Object APIObject `json:"-"`
|
Object APIObject `json:"-"`
|
||||||
|
Error error `json:"-"`
|
||||||
// Data should be used
|
// Data should be used
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
@@ -36,6 +36,8 @@ github.com/modern-go/concurrent
|
|||||||
github.com/modern-go/reflect2
|
github.com/modern-go/reflect2
|
||||||
# github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f
|
# github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f
|
||||||
github.com/mxk/go-flowrate/flowrate
|
github.com/mxk/go-flowrate/flowrate
|
||||||
|
# github.com/pkg/errors v0.8.1
|
||||||
|
github.com/pkg/errors
|
||||||
# github.com/rancher/norman v0.0.0-20190704000224-043a1c919df3 => ../norman
|
# github.com/rancher/norman v0.0.0-20190704000224-043a1c919df3 => ../norman
|
||||||
github.com/rancher/norman/pkg/authorization
|
github.com/rancher/norman/pkg/authorization
|
||||||
github.com/rancher/norman/pkg/types
|
github.com/rancher/norman/pkg/types
|
||||||
|
Reference in New Issue
Block a user