Skip to content

Commit 5f41aa6

Browse files
authored
v2.1.0: add MongoDB package. (#17)
* feat: add mongo_client package * feat: add mongo_client package * test: add mongo_client package tests * remove binary * style fix * remove mongo entrypoint * add build script * refactor config * refactor config mechanism * change default port * delete go sum * add go doc * fix bad naming * remove useless bin * fix flag * change config priority * delay flag parsing * delay flag parsing * reflect only print to stdout
1 parent a935564 commit 5f41aa6

32 files changed

+1406
-110
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ composer.lock
44
*.log
55
.idea/
66
/app
7+
/mongo
8+
/example/app
9+
/example/go2php/app
10+
/example/config/app

.travis.yml

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ sudo: required
55
matrix:
66
include:
77
- php: 7.2
8-
env: SW_VERSION="4.5.0"
8+
env: SW_VERSION="4.5.2"
99
- php: 7.3
10-
env: SW_VERSION="4.5.0"
10+
env: SW_VERSION="4.5.2"
1111
- php: 7.4
12-
env: SW_VERSION="4.5.0"
13-
- php: 7.2
14-
env: SW_VERSION="4.4.18"
15-
- php: 7.3
16-
env: SW_VERSION="4.4.18"
17-
- php: 7.4
18-
env: SW_VERSION="4.4.18"
19-
services: {}
12+
env: SW_VERSION="4.5.2"
13+
14+
services:
15+
- mongodb
2016

2117
before_install:
2218
- export PHP_MAJOR="$(`phpenv which php` -r 'echo phpversion();' | cut -d '.' -f 1)"

bin/build.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
package=../example/mongo_client/sidecar.go
3+
package_name=mongo-proxy
4+
5+
#the full list of the platforms: https://golang.org/doc/install/source#environment
6+
platforms=(
7+
"darwin/amd64"
8+
"linux/amd64"
9+
)
10+
11+
for platform in "${platforms[@]}"
12+
do
13+
platform_split=(${platform//\// })
14+
GOOS=${platform_split[0]}
15+
GOARCH=${platform_split[1]}
16+
output_name=$package_name'-'$GOOS'-'$GOARCH
17+
if [ $GOOS = "windows" ]; then
18+
output_name+='.exe'
19+
fi
20+
echo GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
21+
GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
22+
if [ $? -ne 0 ]; then
23+
echo 'An error has occurred! Aborting the script execution...'
24+
exit 1
25+
fi
26+
done

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@
2626
},
2727
"require-dev": {
2828
"friendsofphp/php-cs-fixer": "^2.14",
29+
"hyperf/command": "^1.1",
2930
"hyperf/config": "^1.1",
3031
"hyperf/di": "^1.1",
3132
"hyperf/testing": "1.1.*",
3233
"mockery/mockery": "^1.3",
3334
"phpstan/phpstan": "^0.12",
34-
"swoft/swoole-ide-helper": "dev-master"
35+
"swoole/ide-helper": "^4.5"
3536
},
3637
"config": {
3738
"sort-packages": true
3839
},
3940
"scripts": {
40-
"test": "go build -o app example/*.go && phpunit -c phpunit.xml --colors=always",
41+
"test": "go build -o app example/*.go && go build -o mongo example/mongo_client/*.go && phpunit -c phpunit.xml --colors=always",
4142
"start-test-server": "php tests/TestServer.php",
4243
"test-go": "/bin/bash -c 'php tests/TestServer.php & sleep 5 && go test ./...'",
4344
"analyse": "phpstan analyse --memory-limit 300M -l 0 ./src",
44-
"cs-fix": "php-cs-fixer fix $1"
45+
"cs-fix": "php-cs-fixer fix $1",
46+
"binary": "go build -o mongo example/mongo_client/*.go"
4547
},
4648
"extra": {
4749
"hyperf": {

example/main.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @contact [email protected]
1010
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1111
*/
12-
1312
use Hyperf\GoTask\GoTask;
1413
use Hyperf\GoTask\IPC\SocketIPCSender;
1514
use Swoole\Process;

example/mongo_client/main.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf/GoTask.
6+
*
7+
* @link https://www.github.com/hyperf/gotask
8+
* @document https://www.github.com/hyperf/gotask
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
use Reasno\GoTask\GoTask;
13+
use Reasno\GoTask\IPC\SocketIPCSender;
14+
use Swoole\Process;
15+
use function Swoole\Coroutine\run;
16+
17+
require __DIR__ . '/../../vendor/autoload.php';
18+
19+
const ADDR = '127.0.0.1:6001';
20+
21+
exec('go build -o ' . __DIR__ . '/app ' . __DIR__ . '/sidecar.go');
22+
$process = new Process(function (Process $process) {
23+
$process->exec(__DIR__ . '/app', ['-address', ADDR]);
24+
});
25+
$process->start();
26+
27+
sleep(1);
28+
29+
run(function () {
30+
$task = new SocketIPCSender(ADDR);
31+
for ($i = 0; $i < 5; ++$i) {
32+
$task->call('MongoProxy.InsertOne', ['Database' => 'testing', 'Collection' => 'colors', 'Record' => ['Blue' => 'Red', 'number' => $i]]);
33+
}
34+
var_dump($task->call('MongoProxy.Find', ['Database' => 'testing', 'Collection' => 'colors', 'Filter' => ['Blue' => 'Red'], 'Opts' => [['Skip' => 1, 'Limit' => 2]]]));
35+
});

example/mongo_client/sidecar.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hyperf/gotask/v2/pkg/gotask"
8+
"github.com/hyperf/gotask/v2/pkg/mongo_client"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
11+
)
12+
13+
func main() {
14+
mongoConfig := mongo_client.LoadConfig()
15+
ctx, cancel := context.WithTimeout(context.Background(), mongoConfig.ConnectTimeout)
16+
defer cancel()
17+
18+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoConfig.Uri))
19+
if err != nil {
20+
log.Fatalln(err)
21+
}
22+
23+
if err := gotask.Register(mongo_client.NewMongoProxyWithTimeout(client, mongoConfig.ReadWriteTimeout)); err != nil {
24+
log.Fatalln(err)
25+
}
26+
27+
if err := gotask.Run(); err != nil {
28+
log.Fatalln(err)
29+
}
30+
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ go 1.13
44

55
require (
66
github.com/fatih/pool v3.0.0+incompatible
7-
github.com/json-iterator/go v1.1.9
87
github.com/oklog/run v1.1.0
98
github.com/pkg/errors v0.9.1
9+
github.com/reasno/gotask v1.0.2
1010
github.com/spiral/goridge/v2 v2.3.3
11+
go.mongodb.org/mongo-driver v1.3.3
1112
)

pkg/gotask/flag.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gotask
22

33
import (
44
"flag"
5-
"testing"
65
)
76

87
var (
@@ -14,11 +13,9 @@ var (
1413
)
1514

1615
func init() {
17-
testing.Init()
1816
standalone = flag.Bool("standalone", false, "if set, ignore parent process status")
1917
address = flag.String("address", "127.0.0.1:6001", "must be a unix socket or tcp address:port like 127.0.0.1:6001")
2018
listenOnPipe = flag.Bool("listen-on-pipe", false, "listen on stdin/stdout pipe")
2119
go2phpAddress = flag.String("go2php-address", "127.0.0.1:6002", "must be a unix socket or tcp address:port like 127.0.0.1:6002")
2220
reflection = flag.Bool("reflect", false, "instead of running the service, provide a service definition to os.stdout using reflection")
23-
flag.Parse()
2421
}

pkg/gotask/generator.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gotask
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -105,19 +106,24 @@ func body(namespace *string, class *Class) string {
105106
func generatePHP(receiver interface{}) error {
106107
namespace := property(receiver, "PHPNamespace", "App\\GoTask")
107108
class := reflectStruct(receiver)
108-
dirPath := property(receiver, "PHPPath", "./../app/GoTask")
109-
err := os.MkdirAll(dirPath, os.FileMode(0755))
110-
if err != nil {
111-
return errors.Wrap(err, "cannot create dir for php files")
112-
}
113-
fullPath, err := filepath.Abs(filepath.Clean(dirPath) + "/" + class.Name + ".php")
114-
if err != nil {
115-
return errors.Wrap(err, "invalid file path")
116-
}
117109
out := body(&namespace, class)
118-
err = ioutil.WriteFile(fullPath, []byte(out), os.FileMode(0755))
119-
if err != nil {
120-
return errors.Wrap(err, "failed to generate PHP file")
110+
dirPath := property(receiver, "PHPPath", "")
111+
if dirPath != "" {
112+
err := os.MkdirAll(dirPath, os.FileMode(0755))
113+
if err != nil {
114+
return errors.Wrap(err, "cannot create dir for php files")
115+
}
116+
fullPath, err := filepath.Abs(filepath.Clean(dirPath) + "/" + class.Name + ".php")
117+
if err != nil {
118+
return errors.Wrap(err, "invalid file path")
119+
}
120+
121+
err = ioutil.WriteFile(fullPath, []byte(out), os.FileMode(0755))
122+
if err != nil {
123+
return errors.Wrap(err, "failed to generate PHP file")
124+
}
125+
} else {
126+
fmt.Print(out)
121127
}
122128
return nil
123129
}

pkg/gotask/server.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package gotask
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
6-
"github.com/oklog/run"
7-
"github.com/pkg/errors"
8-
"github.com/spiral/goridge/v2"
97
"net"
108
"net/rpc"
119
"os"
1210
"os/signal"
1311
"syscall"
1412
"time"
13+
14+
"github.com/oklog/run"
15+
"github.com/pkg/errors"
16+
"github.com/spiral/goridge/v2"
1517
)
1618

1719
var g run.Group
@@ -33,6 +35,9 @@ func checkProcess(pid int, quit chan bool) {
3335

3436
// Register a net/rpc compatible service
3537
func Register(receiver interface{}) error {
38+
if !flag.Parsed() {
39+
flag.Parse()
40+
}
3641
if !*reflection {
3742
return rpc.Register(receiver)
3843
}
@@ -51,6 +56,10 @@ func GetAddress() string {
5156

5257
// Run the sidecar, receive any fatal errors.
5358
func Run() error {
59+
if !flag.Parsed() {
60+
flag.Parse()
61+
}
62+
5463
if *reflection {
5564
return nil
5665
}

pkg/mongo_client/config.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package mongo_client
2+
3+
import (
4+
"flag"
5+
"os"
6+
"time"
7+
)
8+
9+
type Config struct {
10+
Uri string
11+
ConnectTimeout time.Duration
12+
ReadWriteTimeout time.Duration
13+
}
14+
15+
var (
16+
globalMongoUri *string
17+
globalMongoConnectTimeout *time.Duration
18+
globalMongoReadWriteTimeout *time.Duration
19+
)
20+
21+
func init() {
22+
uri, ok := os.LookupEnv("MONGODB_URI")
23+
if !ok {
24+
uri = "mongodb://127.0.0.1:27017"
25+
}
26+
ct := getTimeout("MONGODB_CONNECT_TIMEOUT", 3*time.Second)
27+
rwt := getTimeout("MONGODB_READ_WRITE_TIMEOUT", time.Minute)
28+
29+
globalMongoUri = flag.String("mongodb-uri", uri, "the default mongodb uri")
30+
globalMongoConnectTimeout = flag.Duration("mongodb-connect-timeout", ct, "mongodb connect timeout")
31+
globalMongoReadWriteTimeout = flag.Duration("mongodb-read-write-timeout", rwt, "mongodb read write timeout")
32+
flag.Parse()
33+
}
34+
35+
func getTimeout(env string, fallback time.Duration) (result time.Duration) {
36+
env, ok := os.LookupEnv(env)
37+
if !ok {
38+
return fallback
39+
}
40+
result, err := time.ParseDuration(env)
41+
if err != nil {
42+
return fallback
43+
}
44+
return result
45+
}
46+
47+
// LoadConfig loads Configurations from environmental variables or config file in PHP.
48+
// Environmental variables takes priority.
49+
func LoadConfig() Config {
50+
return Config{
51+
*globalMongoUri,
52+
*globalMongoConnectTimeout,
53+
*globalMongoReadWriteTimeout,
54+
}
55+
}

0 commit comments

Comments
 (0)