File tree 3 files changed +43
-0
lines changed
3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 9
9
10
10
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11
11
*/
12
+
12
13
use Hyperf \GoTask \GoTask ;
13
14
use Hyperf \GoTask \IPC \SocketIPCSender ;
14
15
use Swoole \Process ;
Original file line number Diff line number Diff line change @@ -70,10 +70,14 @@ func Run() error {
70
70
71
71
if * address != "" {
72
72
network , addr := parseAddr (* address )
73
+ if err := clearAddr (network , addr ); err != nil {
74
+ return errors .Wrap (err , "Cannot remove existing unix socket" )
75
+ }
73
76
ln , err := net .Listen (network , addr )
74
77
if err != nil {
75
78
return errors .Wrap (err , "Unable to listen" )
76
79
}
80
+
77
81
g .Add (func () error {
78
82
for {
79
83
conn , err := ln .Accept ()
@@ -121,6 +125,16 @@ func Run() error {
121
125
return g .Run ()
122
126
}
123
127
128
+ func clearAddr (network string , addr string ) error {
129
+ if network != "unix" {
130
+ return nil
131
+ }
132
+ if _ , err := os .Stat (addr ); os .IsNotExist (err ) {
133
+ return nil
134
+ }
135
+ return os .Remove (addr )
136
+ }
137
+
124
138
// Add an actor (function) to the group. Each actor must be pre-emptable by an
125
139
// interrupt function. That is, if interrupt is invoked, execute should return.
126
140
// Also, it must be safe to call interrupt even after execute has returned.
Original file line number Diff line number Diff line change
1
+ package gotask
2
+
3
+ import (
4
+ "log"
5
+ "os"
6
+ "testing"
7
+ )
8
+
9
+ func TestClearAddr (t * testing.T ) {
10
+ if err := clearAddr ("unix" , "/tmp/non-exist.sock" ); err != nil {
11
+ t .Errorf ("clearAddr should not return error for non-exist files" )
12
+ }
13
+ if err := clearAddr ("tcp" , "127.0.0.1:6000" ); err != nil {
14
+ t .Errorf ("clearAddr should not return error for tcp ports" )
15
+ }
16
+ file , err := os .Create ("/tmp/temp.sock" )
17
+ if err != nil {
18
+ log .Fatal (err )
19
+ }
20
+ defer file .Close ()
21
+ if err := clearAddr ("unix" , "/tmp/temp.sock" ); err != nil {
22
+ t .Errorf ("clearAddr should be able to clear unix socket" )
23
+ }
24
+ _ , err = os .Stat ("/tmp/temp.sock" )
25
+ if ! os .IsNotExist (err ) {
26
+ t .Errorf ("unix socket are not cleared, %v" , err )
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments