-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* record polling history * fix bugs and use chan get body * add unit test * rename by comment * use context instead of chan * use const instead string * modify as comment
- Loading branch information
Showing
10 changed files
with
195 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package iputil | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
//ClientIP try to get ip from http header | ||
func ClientIP(r *http.Request) string { | ||
xForwardedFor := r.Header.Get("X-Forwarded-For") | ||
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0]) | ||
if ip != "" { | ||
return ip | ||
} | ||
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip")) | ||
if ip != "" { | ||
return ip | ||
} | ||
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil { | ||
return ip | ||
} | ||
return "" | ||
} |
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"testing" | ||
) | ||
|
||
// | ||
func TestService_CreateOrUpdate(t *testing.T) { | ||
var err error | ||
config.Configurations = &config.Config{DB: config.DB{URI: "mongodb://kie:[email protected]:27017/kie"}} | ||
|
@@ -43,7 +44,7 @@ func TestService_CreateOrUpdate(t *testing.T) { | |
"service": "cart", | ||
}, | ||
Domain: "default", | ||
Project: "test", | ||
Project: "kv-test", | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, kv.ID) | ||
|
@@ -58,9 +59,9 @@ func TestService_CreateOrUpdate(t *testing.T) { | |
"version": "1.0.0", | ||
}, | ||
Domain: "default", | ||
Project: "test", | ||
Project: "kv-test", | ||
}) | ||
oid, err := kvsvc.Exist(context.TODO(), "default", "timeout", "test", service.WithLabels(map[string]string{ | ||
oid, err := kvsvc.Exist(context.TODO(), "default", "timeout", "kv-test", service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
"service": "cart", | ||
"version": "1.0.0", | ||
|
@@ -78,10 +79,10 @@ func TestService_CreateOrUpdate(t *testing.T) { | |
"app": "mall", | ||
}, | ||
Domain: "default", | ||
Project: "test", | ||
Project: "kv-test", | ||
}) | ||
assert.NoError(t, err) | ||
kvs1, err := kvsvc.FindKV(context.Background(), "default", "test", | ||
kvs1, err := kvsvc.FindKV(context.Background(), "default", "kv-test", | ||
service.WithKey("timeout"), | ||
service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
|
@@ -95,15 +96,15 @@ func TestService_CreateOrUpdate(t *testing.T) { | |
"app": "mall", | ||
}, | ||
Domain: "default", | ||
Project: "test", | ||
Project: "kv-test", | ||
}) | ||
assert.Equal(t, "3s", afterKV.Value) | ||
savedKV, err := kvsvc.Exist(context.Background(), "default", "timeout", "test", service.WithLabels(map[string]string{ | ||
savedKV, err := kvsvc.Exist(context.Background(), "default", "timeout", "kv-test", service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
})) | ||
assert.NoError(t, err) | ||
assert.Equal(t, afterKV.Value, savedKV.Value) | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "test", | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "kv-test", | ||
service.WithKey("timeout"), | ||
service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
|
@@ -117,7 +118,7 @@ func TestService_CreateOrUpdate(t *testing.T) { | |
func TestService_FindKV(t *testing.T) { | ||
kvsvc := &kv.Service{} | ||
t.Run("exact find by kv and labels with label app", func(t *testing.T) { | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "test", | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "kv-test", | ||
service.WithKey("timeout"), | ||
service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
|
@@ -127,7 +128,7 @@ func TestService_FindKV(t *testing.T) { | |
assert.Equal(t, 1, len(kvs)) | ||
}) | ||
t.Run("greedy find by labels,with labels app ans service ", func(t *testing.T) { | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "test", | ||
kvs, err := kvsvc.FindKV(context.Background(), "default", "kv-test", | ||
service.WithLabels(map[string]string{ | ||
"app": "mall", | ||
"service": "cart", | ||
|
@@ -146,20 +147,20 @@ func TestService_Delete(t *testing.T) { | |
"env": "test", | ||
}, | ||
Domain: "default", | ||
Project: "test", | ||
Project: "kv-test", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
err = kvsvc.Delete(context.TODO(), kv1.ID, "default", "test") | ||
err = kvsvc.Delete(context.TODO(), kv1.ID, "default", "kv-test") | ||
assert.NoError(t, err) | ||
|
||
}) | ||
t.Run("miss id", func(t *testing.T) { | ||
err := kvsvc.Delete(context.TODO(), "", "default", "test") | ||
err := kvsvc.Delete(context.TODO(), "", "default", "kv-test") | ||
assert.Error(t, err) | ||
}) | ||
t.Run("miss domain", func(t *testing.T) { | ||
err := kvsvc.Delete(context.TODO(), "2", "", "test") | ||
err := kvsvc.Delete(context.TODO(), "2", "", "kv-test") | ||
assert.Equal(t, session.ErrMissingDomain, err) | ||
}) | ||
} |
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,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 record | ||
|
||
import ( | ||
"context" | ||
"github.com/apache/servicecomb-kie/pkg/model" | ||
"github.com/apache/servicecomb-kie/server/service/mongo/session" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
//CreateOrUpdate create a record or update exist record | ||
func CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.PollingDetail, error) { | ||
collection := session.GetDB().Collection(session.CollectionPollingDetail) | ||
queryFilter := bson.M{"domain": detail.Domain, "session_id": detail.SessionID} | ||
res := collection.FindOne(ctx, queryFilter) | ||
if res.Err() != nil { | ||
if res.Err() == mongo.ErrNoDocuments { | ||
_, err := collection.InsertOne(ctx, detail) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return detail, nil | ||
} | ||
return nil, res.Err() | ||
} | ||
_, err := collection.UpdateOne(ctx, queryFilter, detail) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return detail, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters