@@ -2,129 +2,29 @@ package main
22
33import (
44 "context"
5- "net"
65 "os"
7- "time"
86
97 "github.com/threatwinds/go-sdk/catcher"
108 "github.com/threatwinds/go-sdk/plugins"
119 "github.com/threatwinds/go-sdk/utils"
1210
1311 "github.com/tidwall/gjson"
1412 "github.com/tidwall/sjson"
15- "google.golang.org/grpc"
1613)
1714
18- type parsingServer struct {
19- plugins.UnimplementedParsingServer
20- }
21-
2215func main () {
23- // Retry logic for creating socket directory
24- maxRetries := 3
25- retryDelay := 2 * time .Second
26-
27- var filePath utils.Folder
28- var err error
29-
30- for retry := 0 ; retry < maxRetries ; retry ++ {
31- filePath , err = utils .MkdirJoin (plugins .WorkDir , "sockets" )
32- if err == nil {
33- break
34- }
35-
36- _ = catcher .Error ("cannot create directory, retrying" , err , map [string ]any {
37- "process" : "plugin_com.utmstack.geolocation" ,
38- "retry" : retry + 1 ,
39- "maxRetries" : maxRetries ,
40- })
41-
42- if retry < maxRetries - 1 {
43- time .Sleep (retryDelay )
44- // Increase delay for next retry
45- retryDelay *= 2
46- } else {
47- // If all retries failed, log the error and return
48- _ = catcher .Error ("all retries failed when creating directory" , err , map [string ]any {"process" : "plugin_com.utmstack.geolocation" })
49- return
50- }
51- }
52-
53- socketPath := filePath .FileJoin ("com.utmstack.geolocation_parsing.sock" )
54- _ = os .Remove (socketPath )
55-
56- // Retry logic for resolving unix address
57- retryDelay = 2 * time .Second
58- var unixAddress * net.UnixAddr
59-
60- for retry := 0 ; retry < maxRetries ; retry ++ {
61- unixAddress , err = net .ResolveUnixAddr ("unix" , socketPath )
62- if err == nil {
63- break
64- }
65-
66- _ = catcher .Error ("cannot resolve unix address, retrying" , err , map [string ]any {
67- "process" : "plugin_com.utmstack.geolocation" ,
68- "retry" : retry + 1 ,
69- "maxRetries" : maxRetries ,
70- })
71-
72- if retry < maxRetries - 1 {
73- time .Sleep (retryDelay )
74- // Increase delay for next retry
75- retryDelay *= 2
76- } else {
77- // If all retries failed, log the error and return
78- _ = catcher .Error ("all retries failed when resolving unix address" , err , map [string ]any {"process" : "plugin_com.utmstack.geolocation" })
79- return
80- }
81- }
82-
83- // Retry logic for listening to unix socket
84- retryDelay = 2 * time .Second
85- var listener * net.UnixListener
86-
87- for retry := 0 ; retry < maxRetries ; retry ++ {
88- listener , err = net .ListenUnix ("unix" , unixAddress )
89- if err == nil {
90- break
91- }
92-
93- _ = catcher .Error ("cannot listen to unix socket, retrying" , err , map [string ]any {
94- "process" : "plugin_com.utmstack.geolocation" ,
95- "retry" : retry + 1 ,
96- "maxRetries" : maxRetries ,
97- })
98-
99- if retry < maxRetries - 1 {
100- time .Sleep (retryDelay )
101- // Increase delay for next retry
102- retryDelay *= 2
103- } else {
104- // If all retries failed, log the error and return
105- _ = catcher .Error ("all retries failed when listening to unix socket" , err , map [string ]any {"process" : "plugin_com.utmstack.geolocation" })
106- return
107- }
108- }
109-
110- grpcServer := grpc .NewServer ()
111- plugins .RegisterParsingServer (grpcServer , & parsingServer {})
112-
11316 go loadGeolocationData ()
11417
115- // Serve with error handling and retry logic
116- for {
117- err := grpcServer .Serve (listener )
118- if err == nil {
119- break
120- }
121-
122- _ = catcher .Error ("cannot serve grpc, retrying" , err , map [string ]any {"process" : "plugin_com.utmstack.geolocation" })
123- time .Sleep (5 * time .Second )
18+ err := plugins .InitParsingPlugin ("com.utmstack.geolocation" , parseLog )
19+ if err != nil {
20+ _ = catcher .Error ("com.utmstack.geolocation" , err , map [string ]any {
21+ "process" : "plugin_com.utmstack.geolocation" ,
22+ })
23+ os .Exit (1 )
12424 }
12525}
12626
127- func ( p * parsingServer ) ParseLog (_ context.Context , transform * plugins.Transform ) (* plugins.Draft , error ) {
27+ func parseLog (_ context.Context , transform * plugins.Transform ) (* plugins.Draft , error ) {
12828 source , ok := transform .Step .Dynamic .Params ["source" ]
12929 if ! ok {
13030 return transform .Draft , catcher .Error ("'source' parameter required" , nil , map [string ]any {"process" : "plugin_com.utmstack.geolocation" })
0 commit comments