Compare commits
19 Commits
7e929189e1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 56cf9ba23c | |||
| 289594716c | |||
| 90faa7d681 | |||
| 2d81202b6c | |||
| e67009522d | |||
| 0392966760 | |||
| c73ffda016 | |||
| 085d0bb740 | |||
| 59fa0cc2ee | |||
| c652c2a311 | |||
| 892392466f | |||
| e6d8cb6c5a | |||
| 54eab23eb2 | |||
| a434b9bf84 | |||
| 2681c7313b | |||
| 626819209f | |||
| 8e691a4174 | |||
| 887a28aef5 | |||
| 4aae3704e7 |
@ -1,17 +1,17 @@
|
||||
package opensearch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
osg "github.com/opensearch-project/opensearch-go/v4"
|
||||
@ -19,6 +19,8 @@ import (
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
|
||||
const logbulksize = 512 * 1024
|
||||
|
||||
type Config struct {
|
||||
osg.Config `json:",inline"`
|
||||
IndexPrefix string `json:"IndexPrefix"`
|
||||
@ -32,7 +34,11 @@ type Client struct {
|
||||
indexTemplatePattern string
|
||||
bulkHeader http.Header
|
||||
singleHeader http.Header
|
||||
sendingCount int32
|
||||
bulkChan chan *LogDocument
|
||||
singleLogPrepend []byte
|
||||
singleLogMidpend []byte
|
||||
singleLogAppend []byte
|
||||
singleLogFixedSize int
|
||||
}
|
||||
|
||||
type LogDocument struct {
|
||||
@ -62,67 +68,252 @@ func (c *Client) Send(ld *LogDocument) {
|
||||
return
|
||||
}
|
||||
|
||||
serialized, _ := json.Marshal(ld)
|
||||
go func(serialized []byte) {
|
||||
sending := atomic.AddInt32(&c.sendingCount, 1)
|
||||
defer atomic.AddInt32(&c.sendingCount, -1)
|
||||
|
||||
if sending > 100 {
|
||||
logger.Println("sending log bottleneck :", sending)
|
||||
logger.Println(string(serialized))
|
||||
return
|
||||
}
|
||||
|
||||
reader := bytes.NewBuffer(serialized)
|
||||
req := osapi.IndexReq{
|
||||
Index: c.indexTemplatePattern + ld.Type,
|
||||
Body: reader,
|
||||
Header: c.singleHeader,
|
||||
}
|
||||
|
||||
resp, err := c.Do(context.Background(), req, nil)
|
||||
if err != nil {
|
||||
logger.Println("log send failed :", err)
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
}(serialized)
|
||||
c.bulkChan <- ld
|
||||
}
|
||||
|
||||
func (c *Client) SendBulk(ds map[string]*LogDocument) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var contents string
|
||||
|
||||
for _, d := range ds {
|
||||
b, _ := json.Marshal(d)
|
||||
contents += fmt.Sprintf(`{"create":{"_index":"%s%s"}}`+"\n"+`%s`+"\n", c.indexTemplatePattern, d.Type, string(b))
|
||||
c.bulkChan <- d
|
||||
}
|
||||
}
|
||||
|
||||
type singleLogMarshaller struct {
|
||||
singleLogPrepend []byte
|
||||
singleLogMidpend []byte
|
||||
singleLogAppend []byte
|
||||
|
||||
logtype []byte
|
||||
content []byte
|
||||
length int
|
||||
}
|
||||
|
||||
type logSliceReader struct {
|
||||
src []*singleLogMarshaller
|
||||
cursor int
|
||||
}
|
||||
|
||||
func newLogSliceReader(in []singleLogMarshaller) *logSliceReader {
|
||||
src := make([]*singleLogMarshaller, len(in))
|
||||
for i, v := range in {
|
||||
copylog := new(singleLogMarshaller)
|
||||
*copylog = v
|
||||
src[i] = copylog
|
||||
}
|
||||
|
||||
go func(contents string) {
|
||||
sending := atomic.AddInt32(&c.sendingCount, 1)
|
||||
defer atomic.AddInt32(&c.sendingCount, -1)
|
||||
return &logSliceReader{
|
||||
src: src,
|
||||
cursor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
if sending > 100 {
|
||||
logger.Println("sending log bottleneck :", sending)
|
||||
logger.Println(contents)
|
||||
func (b *logSliceReader) Read(p []byte) (n int, err error) {
|
||||
n = 0
|
||||
err = nil
|
||||
|
||||
advance := func(in []byte) []byte {
|
||||
if len(in) == 0 {
|
||||
return in
|
||||
}
|
||||
|
||||
copied := copy(p, in)
|
||||
p = p[copied:]
|
||||
n += copied
|
||||
return in[copied:]
|
||||
}
|
||||
|
||||
for b.cursor < len(b.src) {
|
||||
sbt := b.src[b.cursor]
|
||||
|
||||
if sbt.singleLogPrepend = advance(sbt.singleLogPrepend); len(sbt.singleLogPrepend) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
reader := bytes.NewBuffer([]byte(contents))
|
||||
if sbt.logtype = advance(sbt.logtype); len(sbt.logtype) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if sbt.singleLogMidpend = advance(sbt.singleLogMidpend); len(sbt.singleLogMidpend) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if sbt.content = advance(sbt.content); len(sbt.content) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if sbt.singleLogAppend = advance(sbt.singleLogAppend); len(sbt.singleLogAppend) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
b.cursor++
|
||||
}
|
||||
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
|
||||
func (b *logSliceReader) printSent() {
|
||||
for _, r := range b.src {
|
||||
fmt.Print(string(r.content))
|
||||
}
|
||||
fmt.Print("\n")
|
||||
}
|
||||
|
||||
func (c *Client) sendLoop(ctx context.Context) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r != nil {
|
||||
logger.Error(r)
|
||||
}
|
||||
}()
|
||||
|
||||
failChan := make(chan []singleLogMarshaller)
|
||||
var logMarshallers []singleLogMarshaller
|
||||
sendTick := time.After(time.Minute)
|
||||
|
||||
sendfunc := func(logs []singleLogMarshaller) {
|
||||
if len(logs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r != nil {
|
||||
logger.Println(r)
|
||||
}
|
||||
}()
|
||||
|
||||
reader := newLogSliceReader(logs)
|
||||
req := osapi.BulkReq{
|
||||
Body: reader,
|
||||
Header: c.bulkHeader,
|
||||
}
|
||||
resp, err := c.Do(context.Background(), req, nil)
|
||||
|
||||
if err != nil {
|
||||
logger.Println("log send bulk failed :", err)
|
||||
if netoperr, ok := err.(*net.OpError); ok && netoperr.Op == "dial" {
|
||||
// 접속 안됨. 재시도 안함
|
||||
logger.Println("[LogStream] send bulk failed. no retry :", err)
|
||||
reader.printSent()
|
||||
} else {
|
||||
// 재시도
|
||||
logger.Println("[LogStream] send bulk failed. retry :", err)
|
||||
failChan <- logs
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
}(contents)
|
||||
if resp.Body == nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var respbody struct {
|
||||
Errors bool `json:"errors"`
|
||||
Items []struct {
|
||||
Create struct {
|
||||
Status int `json:"status"`
|
||||
} `json:"create"`
|
||||
} `json:"items"`
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
if err := decoder.Decode(&respbody); err != nil {
|
||||
errbody, _ := io.ReadAll(decoder.Buffered())
|
||||
logger.Println("[LogStream] decode response body failed and retry :", err, string(errbody), len(logs))
|
||||
// 전체 재시도 필요
|
||||
failChan <- logs
|
||||
return
|
||||
}
|
||||
|
||||
if !respbody.Errors {
|
||||
// 성공
|
||||
return
|
||||
}
|
||||
|
||||
var retry []singleLogMarshaller
|
||||
for i, item := range respbody.Items {
|
||||
if item.Create.Status < 300 {
|
||||
continue
|
||||
}
|
||||
|
||||
if item.Create.Status == 429 || item.Create.Status >= 500 {
|
||||
logger.Println("[LogStream] send bulk failed but retry. status :", item.Create.Status)
|
||||
retry = append(retry, logs[i])
|
||||
} else if item.Create.Status == 400 {
|
||||
// 구문 오류. 재시도 불가
|
||||
if i < len(logs) {
|
||||
logger.Println("[LogStream] send bulk failed. status 400 :", string(logs[i].content))
|
||||
} else {
|
||||
logger.Println("[LogStream] send bulk failed. status 400 but out of index :", i, len(logs))
|
||||
}
|
||||
} else {
|
||||
// 일단 로그만
|
||||
logger.Println("[LogStream] send bulk failed but no retry. status :", item.Create.Status)
|
||||
}
|
||||
}
|
||||
|
||||
if len(retry) > 0 {
|
||||
failChan <- retry
|
||||
}
|
||||
}
|
||||
|
||||
totalsize := 0
|
||||
appendLog := func(newlog singleLogMarshaller) bool {
|
||||
if totalsize+newlog.length > logbulksize {
|
||||
go sendfunc(logMarshallers)
|
||||
totalsize = newlog.length
|
||||
logMarshallers = []singleLogMarshaller{newlog}
|
||||
return true
|
||||
}
|
||||
|
||||
totalsize += newlog.length
|
||||
logMarshallers = append(logMarshallers, newlog)
|
||||
return false
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
||||
case ret := <-failChan:
|
||||
// 순서는 중요하지 않음.
|
||||
sent := false
|
||||
for _, newlog := range ret {
|
||||
sent = sent || appendLog(newlog)
|
||||
}
|
||||
|
||||
if sent {
|
||||
sendTick = time.After(time.Minute)
|
||||
}
|
||||
|
||||
case <-sendTick:
|
||||
if len(logMarshallers) > 0 {
|
||||
go sendfunc(logMarshallers)
|
||||
totalsize = 0
|
||||
logMarshallers = nil
|
||||
} else {
|
||||
sendTick = time.After(time.Minute)
|
||||
}
|
||||
|
||||
case logDoc := <-c.bulkChan:
|
||||
b, _ := json.Marshal(logDoc)
|
||||
logtype := []byte(logDoc.Type)
|
||||
|
||||
if appendLog(singleLogMarshaller{
|
||||
singleLogPrepend: c.singleLogPrepend,
|
||||
singleLogMidpend: c.singleLogMidpend,
|
||||
singleLogAppend: c.singleLogAppend,
|
||||
logtype: logtype,
|
||||
content: b,
|
||||
length: len(logtype) + len(b) + c.singleLogFixedSize,
|
||||
}) {
|
||||
sendTick = time.After(time.Minute)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var jwtHeader string
|
||||
@ -142,7 +333,7 @@ func (c *Client) MakeJWT(subject string, role string, ttl time.Duration) string
|
||||
}
|
||||
|
||||
now := time.Now().Add(ttl).Unix()
|
||||
src := []byte(fmt.Sprintf(`{"exp":%d,"sub":"%s","roles":"%s"}`, now, subject, role))
|
||||
src := fmt.Appendf(nil, `{"exp":%d,"sub":"%s","roles":"%s"}`, now, subject, role)
|
||||
payload := make([]byte, encoding.EncodedLen(len(src)))
|
||||
encoding.Encode(payload, src)
|
||||
|
||||
@ -199,11 +390,13 @@ func (c *Client) VerifyJWT(token string) (subject string, role string) {
|
||||
return src.Sub, src.Roles
|
||||
}
|
||||
|
||||
func NewClient(cfg Config) (Client, error) {
|
||||
func NewClient(ctx context.Context, cfg Config) (Client, error) {
|
||||
if len(cfg.Addresses) == 0 {
|
||||
return Client{}, nil
|
||||
}
|
||||
|
||||
// retry는 수동으로
|
||||
cfg.Config.DisableRetry = true
|
||||
client, err := osg.NewClient(cfg.Config)
|
||||
if err != nil {
|
||||
return Client{}, err
|
||||
@ -224,20 +417,42 @@ func NewClient(cfg Config) (Client, error) {
|
||||
indexPrefix = "ds-logs-" + indexPrefix
|
||||
}
|
||||
|
||||
authHeader := fmt.Sprintf("Basic %s", base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", cfg.Username, cfg.Password))))
|
||||
|
||||
logger.Println("[LogStream] stream indexPrefix :", indexPrefix)
|
||||
bulkHeader := make(http.Header)
|
||||
bulkHeader.Set("Authorization", authHeader)
|
||||
|
||||
singleHeader := make(http.Header)
|
||||
singleHeader.Set("Authorization", authHeader)
|
||||
if len(cfg.Username) > 0 && len(cfg.Password) > 0 {
|
||||
authHeader := fmt.Sprintf("Basic %s", base64.RawURLEncoding.EncodeToString(fmt.Appendf(nil, "%s:%s", cfg.Username, cfg.Password)))
|
||||
bulkHeader.Set("Authorization", authHeader)
|
||||
singleHeader.Set("Authorization", authHeader)
|
||||
}
|
||||
|
||||
return Client{
|
||||
singleLogPrepend := fmt.Appendf(nil, `{"create":{"_index":"%s`, indexPrefix)
|
||||
singleLogMidpend := []byte("\"}}\n")
|
||||
singleLogAppend := []byte("\n")
|
||||
singleLogFixedSize := len(singleLogPrepend) + len(singleLogMidpend) + len(singleLogAppend)
|
||||
|
||||
out := Client{
|
||||
Client: client,
|
||||
cfg: cfg,
|
||||
signingKey: signingKey,
|
||||
indexTemplatePattern: indexPrefix,
|
||||
bulkHeader: bulkHeader,
|
||||
singleHeader: singleHeader,
|
||||
}, nil
|
||||
bulkChan: make(chan *LogDocument, 1000),
|
||||
|
||||
singleLogPrepend: singleLogPrepend,
|
||||
singleLogMidpend: singleLogMidpend,
|
||||
singleLogAppend: singleLogAppend,
|
||||
singleLogFixedSize: singleLogFixedSize,
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
out.sendLoop(ctx)
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@ -352,8 +352,13 @@ func ConvertInterface(from interface{}, toType reflect.Type) reflect.Value {
|
||||
return convslice
|
||||
|
||||
case reflect.Bool:
|
||||
val, _ := strconv.ParseBool(from.(string))
|
||||
return reflect.ValueOf(val)
|
||||
if fromstr, ok := from.(string); ok {
|
||||
val, _ := strconv.ParseBool(fromstr)
|
||||
return reflect.ValueOf(val)
|
||||
} else if frombool, ok := from.(bool); ok {
|
||||
return reflect.ValueOf(frombool)
|
||||
}
|
||||
return reflect.ValueOf(false)
|
||||
|
||||
case reflect.String:
|
||||
if toType == reflect.TypeOf(primitive.ObjectID{}) {
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -13,59 +14,60 @@ import (
|
||||
)
|
||||
|
||||
type Authorization struct {
|
||||
Account primitive.ObjectID `bson:"a" json:"a"`
|
||||
invalidated string
|
||||
Account primitive.ObjectID `bson:"a" json:"a"`
|
||||
|
||||
// by authorization provider
|
||||
Platform string `bson:"p" json:"p"`
|
||||
Uid string `bson:"u" json:"u"`
|
||||
Alias string `bson:"al" json:"al"`
|
||||
CreatedTime primitive.DateTime `bson:"ct" json:"ct"`
|
||||
Platform string `bson:"p" json:"p"`
|
||||
Uid string `bson:"u" json:"u"`
|
||||
Alias string `bson:"al" json:"al"`
|
||||
CreatedTime int64 `bson:"ct" json:"ct"`
|
||||
}
|
||||
|
||||
func (auth *Authorization) ToStrings() []string {
|
||||
ct, _ := auth.CreatedTime.MarshalJSON()
|
||||
return []string{
|
||||
"a", auth.Account.Hex(),
|
||||
"p", auth.Platform,
|
||||
"u", auth.Uid,
|
||||
"al", auth.Alias,
|
||||
"inv", auth.invalidated,
|
||||
"ct", string(ct),
|
||||
"ct", strconv.FormatInt(auth.CreatedTime, 10),
|
||||
}
|
||||
}
|
||||
|
||||
func (auth *Authorization) Valid() bool {
|
||||
return len(auth.invalidated) == 0 && !auth.Account.IsZero()
|
||||
return !auth.Account.IsZero()
|
||||
}
|
||||
|
||||
func MakeAuthrizationFromStringMap(src map[string]string) Authorization {
|
||||
accid, _ := primitive.ObjectIDFromHex(src["a"])
|
||||
var datetime primitive.DateTime
|
||||
datetime.UnmarshalJSON([]byte(src["ct"]))
|
||||
ct, _ := strconv.ParseInt(src["ct"], 10, 0)
|
||||
return Authorization{
|
||||
Account: accid,
|
||||
Platform: src["p"],
|
||||
Uid: src["u"],
|
||||
Alias: src["al"],
|
||||
invalidated: src["inv"],
|
||||
CreatedTime: datetime,
|
||||
CreatedTime: ct,
|
||||
}
|
||||
}
|
||||
|
||||
type Provider interface {
|
||||
New(*Authorization) (string, error)
|
||||
RevokeAll(primitive.ObjectID) error
|
||||
RevokeAll(primitive.ObjectID, bool) ([]string, error)
|
||||
Query(string) (Authorization, error)
|
||||
Touch(string) (bool, error)
|
||||
}
|
||||
|
||||
type InvalidatedSession struct {
|
||||
Account primitive.ObjectID
|
||||
SessionKeys []string
|
||||
Infinite bool
|
||||
}
|
||||
|
||||
type Consumer interface {
|
||||
Query(string) Authorization
|
||||
Touch(string) (Authorization, error)
|
||||
IsRevoked(primitive.ObjectID) bool
|
||||
Revoke(string)
|
||||
RegisterOnSessionInvalidated(func(primitive.ObjectID))
|
||||
RegisterOnSessionInvalidated(func(InvalidatedSession))
|
||||
}
|
||||
|
||||
type storagekey string
|
||||
@ -121,10 +123,6 @@ var errInvalidScheme = errors.New("storageAddr is not valid scheme")
|
||||
var errSessionStorageMissing = errors.New("session_storageis missing")
|
||||
|
||||
func NewConsumer(ctx context.Context, storageAddr string, ttl time.Duration) (Consumer, error) {
|
||||
if strings.HasPrefix(storageAddr, "mongodb") {
|
||||
return newConsumerWithMongo(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(storageAddr, "redis") {
|
||||
return newConsumerWithRedis(ctx, storageAddr, ttl)
|
||||
}
|
||||
@ -144,10 +142,6 @@ func NewConsumerWithConfig(ctx context.Context, cfg SessionConfig) (Consumer, er
|
||||
}
|
||||
|
||||
func NewProvider(ctx context.Context, storageAddr string, ttl time.Duration) (Provider, error) {
|
||||
if strings.HasPrefix(storageAddr, "mongodb") {
|
||||
return newProviderWithMongo(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(storageAddr, "redis") {
|
||||
return newProviderWithRedis(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type cache_stage[T any] struct {
|
||||
@ -26,7 +24,7 @@ type consumer_common[T any] struct {
|
||||
ctx context.Context
|
||||
stages [2]*cache_stage[T]
|
||||
startTime time.Time
|
||||
onSessionInvalidated []func(primitive.ObjectID)
|
||||
onSessionInvalidated []func(InvalidatedSession)
|
||||
}
|
||||
|
||||
func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
|
||||
|
||||
@ -1,383 +0,0 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
session_collection_name = gocommon.CollectionName("session")
|
||||
)
|
||||
|
||||
type provider_mongo struct {
|
||||
mongoClient gocommon.MongoClient
|
||||
}
|
||||
|
||||
type sessionMongo struct {
|
||||
Id primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Auth *Authorization `bson:"auth"`
|
||||
Key storagekey `bson:"key"`
|
||||
Ts primitive.DateTime `bson:"_ts"`
|
||||
}
|
||||
|
||||
func newProviderWithMongo(ctx context.Context, mongoUrl string, ttl time.Duration) (Provider, error) {
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = mc.MakeUniqueIndices(session_collection_name, map[string]bson.D{
|
||||
"key": {{Key: "key", Value: 1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := mc.MakeExpireIndex(session_collection_name, int32(ttl.Seconds())); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &provider_mongo{
|
||||
mongoClient: mc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *provider_mongo) New(input *Authorization) (string, error) {
|
||||
sk := make_storagekey(input.Account)
|
||||
|
||||
_, _, err := p.mongoClient.Update(session_collection_name, bson.M{
|
||||
"_id": input.Account,
|
||||
}, bson.M{
|
||||
"$set": sessionMongo{
|
||||
Auth: input,
|
||||
Key: sk,
|
||||
Ts: primitive.NewDateTimeFromTime(time.Now().UTC()),
|
||||
},
|
||||
}, options.Update().SetUpsert(true))
|
||||
|
||||
return string(storagekey_to_publickey(sk)), err
|
||||
}
|
||||
|
||||
func (p *provider_mongo) RevokeAll(acc primitive.ObjectID) error {
|
||||
_, err := p.mongoClient.Delete(session_collection_name, bson.M{
|
||||
"_id": acc,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *provider_mongo) Query(pk string) (Authorization, error) {
|
||||
sk := publickey_to_storagekey(publickey(pk))
|
||||
var auth Authorization
|
||||
err := p.mongoClient.FindOneAs(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
}, &auth)
|
||||
|
||||
return auth, err
|
||||
}
|
||||
|
||||
func (p *provider_mongo) Touch(pk string) (bool, error) {
|
||||
sk := publickey_to_storagekey(publickey(pk))
|
||||
worked, _, err := p.mongoClient.Update(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
}, bson.M{
|
||||
"$currentDate": bson.M{
|
||||
"_ts": bson.M{"$type": "date"},
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
|
||||
if err != nil {
|
||||
logger.Println("provider Touch :", err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return worked, nil
|
||||
}
|
||||
|
||||
type consumer_mongo struct {
|
||||
consumer_common[*sessionMongo]
|
||||
ids map[primitive.ObjectID]storagekey
|
||||
mongoClient gocommon.MongoClient
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
type sessionPipelineDocument struct {
|
||||
OperationType string `bson:"operationType"`
|
||||
DocumentKey struct {
|
||||
Id primitive.ObjectID `bson:"_id"`
|
||||
} `bson:"documentKey"`
|
||||
Session *sessionMongo `bson:"fullDocument"`
|
||||
}
|
||||
|
||||
func newConsumerWithMongo(ctx context.Context, mongoUrl string, ttl time.Duration) (Consumer, error) {
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
consumer := &consumer_mongo{
|
||||
consumer_common: consumer_common[*sessionMongo]{
|
||||
ttl: ttl,
|
||||
ctx: ctx,
|
||||
stages: [2]*cache_stage[*sessionMongo]{make_cache_stage[*sessionMongo](), make_cache_stage[*sessionMongo]()},
|
||||
startTime: time.Now(),
|
||||
},
|
||||
ids: make(map[primitive.ObjectID]storagekey),
|
||||
ttl: ttl,
|
||||
mongoClient: mc,
|
||||
}
|
||||
|
||||
go func() {
|
||||
matchStage := bson.D{
|
||||
{
|
||||
Key: "$match", Value: bson.D{
|
||||
{Key: "operationType", Value: bson.D{
|
||||
{Key: "$in", Value: bson.A{
|
||||
"delete",
|
||||
"insert",
|
||||
"update",
|
||||
}},
|
||||
}},
|
||||
},
|
||||
}}
|
||||
projectStage := bson.D{
|
||||
{
|
||||
Key: "$project", Value: bson.D{
|
||||
{Key: "documentKey", Value: 1},
|
||||
{Key: "operationType", Value: 1},
|
||||
{Key: "fullDocument", Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var stream *mongo.ChangeStream
|
||||
nextswitch := time.Now().Add(ttl)
|
||||
for {
|
||||
if stream == nil {
|
||||
stream, err = mc.Watch(session_collection_name, mongo.Pipeline{matchStage, projectStage})
|
||||
if err != nil {
|
||||
logger.Error("watchAuthCollection watch failed :", err)
|
||||
time.Sleep(time.Minute)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
changed := stream.TryNext(ctx)
|
||||
if ctx.Err() != nil {
|
||||
logger.Error("watchAuthCollection stream.TryNext failed. process should be restarted! :", ctx.Err().Error())
|
||||
break
|
||||
}
|
||||
|
||||
if changed {
|
||||
var data sessionPipelineDocument
|
||||
if err := stream.Decode(&data); err == nil {
|
||||
ot := data.OperationType
|
||||
switch ot {
|
||||
case "insert":
|
||||
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
|
||||
case "update":
|
||||
if data.Session == nil {
|
||||
if old := consumer.deleteById(data.DocumentKey.Id); old != nil {
|
||||
for _, f := range consumer.onSessionInvalidated {
|
||||
f(old.Auth.Account)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
consumer.add(data.Session.Key, data.DocumentKey.Id, data.Session)
|
||||
}
|
||||
case "delete":
|
||||
if old := consumer.deleteById(data.DocumentKey.Id); old != nil {
|
||||
for _, f := range consumer.onSessionInvalidated {
|
||||
f(old.Auth.Account)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.Error("watchAuthCollection stream.Decode failed :", err)
|
||||
}
|
||||
} else if stream.Err() != nil || stream.ID() == 0 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logger.Println("watchAuthCollection is done")
|
||||
stream.Close(ctx)
|
||||
return
|
||||
|
||||
case <-time.After(time.Second):
|
||||
logger.Error("watchAuthCollection stream error :", stream.Err())
|
||||
stream.Close(ctx)
|
||||
stream = nil
|
||||
}
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
for now.After(nextswitch) {
|
||||
consumer.changeStage()
|
||||
nextswitch = nextswitch.Add(ttl)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return consumer, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) query_internal(sk storagekey) (*sessionMongo, bool, error) {
|
||||
if _, deleted := c.stages[0].deleted[sk]; deleted {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
if _, deleted := c.stages[1].deleted[sk]; deleted {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
found, ok := c.stages[0].cache[sk]
|
||||
if !ok {
|
||||
found, ok = c.stages[1].cache[sk]
|
||||
}
|
||||
|
||||
if ok {
|
||||
return found, false, nil
|
||||
}
|
||||
|
||||
var si sessionMongo
|
||||
err := c.mongoClient.FindOneAs(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
}, &si)
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(si.Key) > 0 {
|
||||
siptr := &si
|
||||
c.add_internal(sk, siptr)
|
||||
return siptr, true, nil
|
||||
}
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Query(pk string) Authorization {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
sk := publickey_to_storagekey(publickey(pk))
|
||||
si, _, err := c.query_internal(sk)
|
||||
if err != nil {
|
||||
return Authorization{}
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return Authorization{}
|
||||
}
|
||||
|
||||
if time.Now().After(si.Ts.Time().Add(c.ttl)) {
|
||||
return Authorization{}
|
||||
}
|
||||
|
||||
return *si.Auth
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Touch(pk string) (Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
sk := publickey_to_storagekey(publickey(pk))
|
||||
worked, _, err := c.mongoClient.Update(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
}, bson.M{
|
||||
"$currentDate": bson.M{
|
||||
"_ts": bson.M{"$type": "date"},
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Touch :", err)
|
||||
return Authorization{}, err
|
||||
}
|
||||
|
||||
if !worked {
|
||||
// 이미 만료되서 사라짐
|
||||
return Authorization{}, nil
|
||||
}
|
||||
|
||||
si, added, err := c.query_internal(sk)
|
||||
if err != nil {
|
||||
return Authorization{}, err
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return Authorization{}, nil
|
||||
}
|
||||
|
||||
if !added {
|
||||
var doc sessionMongo
|
||||
err := c.mongoClient.FindOneAs(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
}, &doc)
|
||||
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return Authorization{}, err
|
||||
}
|
||||
|
||||
if len(si.Key) > 0 {
|
||||
c.add_internal(sk, &doc)
|
||||
c.ids[doc.Id] = sk
|
||||
|
||||
return *doc.Auth, nil
|
||||
}
|
||||
}
|
||||
|
||||
return *si.Auth, nil
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) Revoke(pk string) {
|
||||
sk := publickey_to_storagekey(publickey(pk))
|
||||
_, err := c.mongoClient.Delete(session_collection_name, bson.M{
|
||||
"key": sk,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
for id, v := range c.ids {
|
||||
if v == sk {
|
||||
delete(c.ids, id)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) IsRevoked(id primitive.ObjectID) bool {
|
||||
_, ok := c.ids[id]
|
||||
return !ok
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) add(sk storagekey, id primitive.ObjectID, si *sessionMongo) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.consumer_common.add_internal(sk, si)
|
||||
c.ids[id] = sk
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) deleteById(id primitive.ObjectID) (old *sessionMongo) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
if sk, ok := c.ids[id]; ok {
|
||||
old = c.consumer_common.delete_internal(sk)
|
||||
delete(c.ids, id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *consumer_mongo) RegisterOnSessionInvalidated(cb func(primitive.ObjectID)) {
|
||||
c.onSessionInvalidated = append(c.onSessionInvalidated, cb)
|
||||
}
|
||||
@ -2,8 +2,10 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@ -43,31 +45,18 @@ func newProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
}
|
||||
|
||||
func (p *provider_redis) New(input *Authorization) (string, error) {
|
||||
newsk := make_storagekey(input.Account)
|
||||
prefix := input.Account.Hex()
|
||||
sks, err := p.redisClient.Keys(p.ctx, prefix+"*").Result()
|
||||
sks, err := p.RevokeAll(input.Account, false)
|
||||
if err != nil {
|
||||
logger.Println("session provider delete :", sks, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
p.redisClient.Del(p.ctx, sks...)
|
||||
for _, sk := range sks {
|
||||
p.redisClient.Publish(p.ctx, p.deleteChannel, sk).Result()
|
||||
}
|
||||
|
||||
var newsk storagekey
|
||||
for {
|
||||
duplicated := false
|
||||
for _, sk := range sks {
|
||||
if sk == string(newsk) {
|
||||
duplicated = true
|
||||
break
|
||||
}
|
||||
}
|
||||
newsk = make_storagekey(input.Account)
|
||||
duplicated := slices.Contains(sks, string(newsk))
|
||||
if !duplicated {
|
||||
break
|
||||
}
|
||||
newsk = make_storagekey(input.Account)
|
||||
}
|
||||
|
||||
_, err = p.redisClient.HSet(p.ctx, string(newsk), input.ToStrings()).Result()
|
||||
@ -82,20 +71,28 @@ func (p *provider_redis) New(input *Authorization) (string, error) {
|
||||
return string(pk), err
|
||||
}
|
||||
|
||||
func (p *provider_redis) RevokeAll(account primitive.ObjectID) error {
|
||||
func (p *provider_redis) RevokeAll(account primitive.ObjectID, infinite bool) ([]string, error) {
|
||||
prefix := account.Hex()
|
||||
sks, err := p.redisClient.Keys(p.ctx, prefix+"*").Result()
|
||||
if err != nil {
|
||||
logger.Println("session provider delete :", sks, err)
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, sk := range sks {
|
||||
p.redisClient.HSet(p.ctx, sk, "inv", "true")
|
||||
p.redisClient.Publish(p.ctx, p.deleteChannel, sk).Result()
|
||||
if len(sks) > 0 {
|
||||
p.redisClient.Del(p.ctx, sks...)
|
||||
|
||||
invsess := InvalidatedSession{
|
||||
SessionKeys: sks,
|
||||
Account: account,
|
||||
Infinite: infinite,
|
||||
}
|
||||
data, _ := json.Marshal(invsess)
|
||||
|
||||
p.redisClient.Publish(p.ctx, p.deleteChannel, string(data)).Result()
|
||||
}
|
||||
|
||||
return nil
|
||||
return sks, nil
|
||||
}
|
||||
|
||||
func (p *provider_redis) Query(pk string) (Authorization, error) {
|
||||
@ -181,12 +178,18 @@ func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
|
||||
switch msg.Channel {
|
||||
case deleteChannel:
|
||||
sk := storagekey(msg.Payload)
|
||||
old := consumer.delete(sk)
|
||||
if old != nil {
|
||||
for _, f := range consumer.onSessionInvalidated {
|
||||
f(old.Account)
|
||||
}
|
||||
var invsess InvalidatedSession
|
||||
if err := json.Unmarshal([]byte(msg.Payload), &invsess); err != nil {
|
||||
logger.Println("redis consumer deleteChannel unmarshal failed :", err)
|
||||
break
|
||||
}
|
||||
|
||||
for _, sk := range invsess.SessionKeys {
|
||||
consumer.delete(storagekey(sk))
|
||||
}
|
||||
|
||||
for _, f := range consumer.onSessionInvalidated {
|
||||
f(invsess)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -366,6 +369,6 @@ func (c *consumer_redis) IsRevoked(accid primitive.ObjectID) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *consumer_redis) RegisterOnSessionInvalidated(cb func(primitive.ObjectID)) {
|
||||
func (c *consumer_redis) RegisterOnSessionInvalidated(cb func(InvalidatedSession)) {
|
||||
c.onSessionInvalidated = append(c.onSessionInvalidated, cb)
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func TestExpTable(t *testing.T) {
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
pv.RevokeAll(au1.Account)
|
||||
pv.RevokeAll(au1.Account, false)
|
||||
|
||||
cs.Touch(sk1)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
@ -334,8 +334,8 @@ func (ws *WebsocketHandler) LeaveRoom(room string, accid primitive.ObjectID) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WebsocketHandler) onSessionInvalidated(accid primitive.ObjectID) {
|
||||
ws.forceCloseChan <- accid
|
||||
func (ws *WebsocketHandler) onSessionInvalidated(invsess session.InvalidatedSession) {
|
||||
ws.forceCloseChan <- invsess.Account
|
||||
}
|
||||
|
||||
func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
|
||||
|
||||
@ -176,9 +176,9 @@ func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux gocommon.ServerMuxI
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *websocketPeerHandler[T]) onSessionInvalidated(accid primitive.ObjectID) {
|
||||
func (ws *websocketPeerHandler[T]) onSessionInvalidated(invsess session.InvalidatedSession) {
|
||||
ws.peerDtorChannel <- peerDtorChannelValue{
|
||||
accid: accid,
|
||||
accid: invsess.Account,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user